How To Use Functions In Python (Python Tutorial #3)
Skills:
Python for Data80%
Key Takeaways
Uses Python functions in a tutorial setting
Full Transcript
hey CS dojo its YK here in this video I'm gonna cover what functions are and how to use them in Python and using a function by the end of this video you'll be able to create a BMI calculator similar to the one we created in the last video but you'll be able to use this one repeatedly for many different people and many different variables I'm also gonna cover what comments are and how to use them and as usual I'll put the outline of this course below so you don't have to watch the whole thing let me first begin by answering a question though caliche I think says hello CS dojo although I downloaded Jupiter on my macbook I was unable to open your Jupiter notebook sample files so if you download a sample file for example from CS dojo dot io / Python - for the last video if you just click the file that you just downloaded this one on desktop you actually won't open it so what you need to do instead is you need to launch Jupiter notebook in any way you'd like to launch it I'm gonna use anaconda navigator here and once you launch Jupiter notebook within the Jupiter notebook UI you need to go to wherever you downloaded the file for example desktop and then click the file that you just downloaded within jupiter notebook and that way you'll be able to open the file and they use it and edit it and for this video you can just go to CH dojo dot io / Python 3 to download the sample files now with that out of the way let's dive into our main topic today which is what are functions just like usual I created a folder called Python tutorial 3 on desktop and a new notebook file called what are functions so there are actually a few different ways of looking at functions a function one way to look at it is that it's a collection of a structures or it's a collection of code and notice here that these lines I wrote here there are not pieces of Python code there's something called comments so comments are used to comment on the code that we're going to write below so you can use it to for example explain while variables you're defining and what functions we're going to define and so on and we can tell that these two lines are comments because each of these two lines begin with the pound sign or the hash tag so a function can be simply a collection of code let's see an example of that def function one parentheses colon print ah print double quotes are two and then print this is outside the function this whole block says def for define so define the following function and then give it a name which we can choose and we're gonna call it function one we can call it any name we want for example function two one two three or fun fun fun let's keep it at function one for now and then parentheses : so a combination of all of these things are important def the function name parentheses and colon 4 spaces print are and four spaces again print are two and these spaces in front of these two lines are really important just like with if-else statements to show that these two lines are part of this function if you had for example four spaces here and five spaces or three spaces it just wouldn't work and as you can see outside of this function we have a print statement that prints this is outside the function let's see what happens once we load or run this cell as you can see only this line has been printed this is outside the function what happened was this function called function 1 has been defined as a collection of code these two lines of code print are and print are two and after that's done this line has been executed so these two lines of code print are and print are two will not be executed will not be run until we use this function or we call as we say this function to use this function or to call this function you can just write function one open parentheses close parentheses and that's it when you run this cell what's gonna happen is these two lines of code we'll be executed for the first time so ah and our two are printed here and the nice thing about functions is that you can reuse them over and over again so you can write function one over here again and actually you can even use it twice in the same cell and so when you run this cell function one is executed twice and these two lines of code are executed twice as well so let's run the cell and let's see what's going to be printed as expected we see our two twice okay in addition to being a collection of instructions or a collection of code a function can also be a mapping so let's take a look at an example of that def function two parenthesis X colon return two times X or 2 star X this means define a function called function 2 which is going to take the input or an argument and that argument we're going to call it X and in return to whoever call this function we're going to return two times X so we're mapping the input X to the output 2 times X execute this cell and to use this function or to call this function you can write a equals function 2 parenthesis 3 and this says as an argument use 3 and call this function function 2 and once this expression is evaluated function 2 parenthesis 3 will return 2 times X in this case 2 times 3 which is 6 and then that number 6 will be assigned to the variable a and that number 6 is called a return value or output let's see if this expression works by running the cell and by printing a and we should see 6 and we do ok let's try using this function a few more times if you write B equals function 2 parenthesis 4 function 2 of parenthesis 4 should return 8 so once we print B we should see 8 and we do and if you write C equals function 2 of five and then if you print see you should see it 10 and what if you try to call this function without any arguments so if you write D equals function to parenthesis with nothing inside let's try running it it'll actually give us an error saying function two missing one required positional argument X so this was an example of a function that map's one argument to a return value is it possible to have multiple arguments in a single function the answer is yes for that you can write def function 3x comma space Y : and then let's write return x plus y this means let's define a function called function 3 and then this function is going to take two arguments x and y and return X plus y let's load this function by running the cell and let's write a equals function 3 parenthesis 1 comma space - so function 3 1 comma space - should be evaluated to 1 plus 2 which is 3 so once we print e we should see 3 okay so we saw two different ways of using a function so far the first one was as a set of code or a collection of code and then the second one was as a mapping now it's even possible to combine them both together let's see how that works with this function function 4 of X : 4 spaces as usual print X prints double quotes still in this function and then return 3 times X or 3 star X this function basically says take the argument X and then print X and print this string still in this function and then return 3 times X - whoever call this function let's run this cell and let's try calling this function with F equals function 4 this is for so what's going to happen here is when this expression is evaluated function for or for we go to this line so X is printed so that means for the number four will be printed and then this string still in this function will be printed and three times X which is 12 will be returned to this expression so that will be assigned to F so once we run this cell you see that four and steering this function or printed and once you print F you should see 12 okay let's see another example of a function deff define function five parentheses sum underscore argument : four spaces print some argument and then print we in double quotes let's run the cell to load the function and of course you can call this function with function five four let's run the cell and then we see that four is printed because we have some argument being printed and we this string is also printed so one thing to note here is that even though function five is given an argument we don't have a return value it's actually technically possible to say F equals function five of four to assign whatever is returned from function 5 - f but there isn't much of a point because we don't have any return statement here okay let's now create a BMI calculator here now let's say we have three people here we have a hypothetical person named YK here whose height is 2 meters and whose weight is 90 kilograms and here we have YK sister whose height is 1.8 meters and whose weight is 70 kilograms and YK its brother is 2.5 meters in height and 160 kilograms in weight what if we wanted to calculate the BMI for each person and determining if each person is overweight or not we can do this by writing a simple function let's call this function BMI and the score calculator and then this function is going to take three arguments named height in meters or height underscore em and then weight in kilograms or weight underscore kg in this function right BMI weight underscore kg divided by height and the score M double star sign - so this says of course height in meters squared and what we could do is we could return BMI here but let's do something a little bit different here we're gonna write prints double quotes BMI and then print BMI so this is going to print the BMI of the given person and then if the BMI is less than 25 this person is not overweight so we could return not overweight but to show this person's name you can just write name plus not overweight so the Assumption here is that name is a string and when you have two strings you can concatenate them together with name plus double quotes not overweight and then let's write else colon return name plus is overweight and note here that there are eight spaces here because this else statement is in the function BMI calculator and then this line is in the out statement within the BMI calculator function so let's run this cell and then let's write result 1 equals BMI calculator parentheses name one height underscore m1 weight underscore kg one so this is for the first person and let's do the same thing for the second person and the third person let's run this cell so here we see the BMI for the three people twenty two point five for the first person twenty twenty one point six for the second person and twenty five point six for the third person and to get the result we can print without one without two and result three and then we see why cane or overweight like a sister not overweight why kids brother is overweight actually we forgot to put is before not so let's fix that go back to the function and then put is here run this cell again to update the function and then run this cell below that and then let's try printing the results again okay it's fixed so YK is not overweight okay sister is not overweight and why case brother is overweight okay now I have a little task for you create a function called let's say convert that converts miles into kilometers so you should be able to call your function just like this with a function called convert which takes Mouse as the argument and returns kilometers as a return value and here you can use this formula kilometers equals one point six times miles okay if you want to get the answer to this question just go to CS dojo dot io / Python 3 to download the sample file and you can subscribe to my newsletter by going to CH dojo dot io / news to make sure you don't miss my future tutorials my key from CH dojo and I'll see you in the next video
Original Description
This entire series in a playlist: https://goo.gl/eVauVX
Keep in touch on Facebook: https://www.facebook.com/entercsdojo
Subscribe to my newsletter: https://www.csdojo.io/news
Support me on Patreon: https://www.patreon.com/csdojo
Tips:
1. Follow along as I explain to make sure you understand everything
2. Ideally, work with a friend so you can help each other when you’re stuck
3. If you want to learn faster than I talk, I’d recommend 1.25x or 1.5x speed :)
4. Check the outline in the comment section below if you want to skip around.
5. Download the sample files here to follow along (they are Jupyter Notebook files): https://www.csdojo.io/python3
6. To make sure you don’t miss my future tutorial videos, sign up to my newsletter: https://www.csdojo.io/news
7. Have fun! If anything is unclear, please let me know in a comment.
Keep in touch on Facebook: https://www.facebook.com/entercsdojo
Subscribe to my newsletter: https://www.csdojo.io/news
Support me on Patreon: https://www.patreon.com/csdojo
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 30 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
▶
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
I Stopped Writing Image Metadata Manually — Here’s What Changed
Medium · AI
ranslating the Infrared Universe: The World’s First Raw Thermal Data Sonification of Jupiter’s…
Medium · Python
AI Tools and Tutorials: The Beginner’s Guide to Working Smarter with Artificial Intelligence
Medium · ChatGPT
ChatGPT Plus vs Claude Pro (2026): Which AI Subscription Is Worth It?
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI