How To Use Functions In Python (Python Tutorial #3)

Sharing buttons:

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


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


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


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