16 Functions 1 Code
16 Functions 1 Code
md 7/21/2022
Functions 1
Functions (Basics)
def
return
Example exercise
Functions
Syntax
creating a function (def)
How to call a function in python?
return statement
scope and life time of variables
Function arguments
Built in functions (Examples)
Problem Statement / Example 1
$$ new_rating = \frac{(current_rating*total_number_of_ratings)+current_ participant_rating}
{total_number_of_ratings+1 }$$
The variable total_number_of_ratings stores the total number of ratings given for this course
video/lecture til now.
The variable current_rating stores current rating of the course/video.
The variable current_participant_rating is the rating which is entered by the current
participant for the course/video.
current_rating = 4.5
total_number = 10
# Mayank
current_participant = 5
total_number += 1
1 / 13
Functions_1.md 7/21/2022
print(current_rating)
print(total_number)
4.545454545454546
11
# Rahul - 4
current_participant = 4
total_number += 1
print(current_rating)
print(total_number)
4.500000000000001
12
total_number += 1
print(current_rating)
print(total_number)
2 / 13
Functions_1.md 7/21/2022
4.461538461538463
13
total_number += 1
print(current_rating)
print(total_number)
4.42857142857143
14
4.545454545454546
4.500000000000001
4.538461538461539
4.428571428571429
Syntax of a function
def function_name(parameters):
"""
Doc String
"""
statement(s)
res = add(3, 5)
print(res)
4 / 13
Functions_1.md 7/21/2022
9.0
hello world
6.5
Quiz
def add_2_nums_with_return(n1, n2):
return n1 + n2
y = add_2_nums_with_return(5, 6)
print(y)
11
5 / 13
Functions_1.md 7/21/2022
add_2_numbers(5, 7)
add_2_numbers(5.7, 9.6)
add_2_numbers(-1, -2)
12
15.3
-3
Flow of function
Important note:
In python, function call should always be present after the function definition
The return statement
The return statement is used to exit a function and go back to the place from where it was called
def foo_bar():
# inside the function
print('Foo')
6 / 13
Functions_1.md 7/21/2022
return 1
print('Bar')
foo_bar()
print('done with function')
Foo
done with function
int(5.6)
print('Hello') # no return so 5 is not printed
Hello
Quiz
def foo_bar():
# inside the function
print('Foo')
return 1
print('Bar')
res = foo_bar()
print(res)
Foo
1
7 / 13
Functions_1.md 7/21/2022
Quiz
def square(a):
return a*a
x = square(3)
y = square(5)
print(x + y)
34
Quiz
def print_max(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
# if no return is there, it stops at the last line in the function
print('We printed the max !! YAY!!')
print("WE are still inside the function,.....yaaaay!!")
print_max(3, 4)
4 is maximum
We printed the max !! YAY!!
WE are still inside the function,.....yaaaay!!
print_max(50, 22)
50 is maximum
We printed the max !! YAY!!
WE are still inside the function,.....yaaaay!!
Quiz
8 / 13
Functions_1.md 7/21/2022
def check(a):
if a % 2 == 0:
print("Even")
else:
print("Odd")
check(12)
Even
x = 12
print("Even" if x % 2 == 0 else "Odd") # ternary operators in other
language
Even
x = 11
print("Even" if x % 2 == 0 else "Odd")
Odd
print(1.232425)
1.232425
print(abs(3))
9 / 13
Functions_1.md 7/21/2022
print(abs(-3))
print(round(3.1234))
print(round(3.1234, 1))
3.1
print(round(3.1234, 2))
3.12
help(round)
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
10 / 13
Functions_1.md 7/21/2022
1.) Write a function to covert the input temperature on fahrenheit scale to celcius scale using the following
$$C=\frac{5}{9}*(F-32)$$
Your function has to return the value rounded to 2 decimals
def fahrenheit_to_celsius(f):
c = (5/9) * (f - 32)
return round(c, 2) # expression will get computed
# in your assignment, you don't need to call the function or take input
# unless mentioned
res = fahrenheit_to_celsius(32)
print(res)
0.0
res = fahrenheit_to_celsius(100)
print(res)
37.78
Doubts
def fun():
return 1
return 2
return 3
print(fun())
11 / 13
Functions_1.md 7/21/2022
def fun():
print(1)
print(2)
print(3)
fun()
1
2
3
def fahrenheit_to_celsius(f):
c = (5/9) * (f - 32)
# print(round(c, 2))
return round(c, 2) # expression will get computed
res = fahrenheit_to_celsius(100)
print(res)
37.78
def f():
return 1
f()
print('') # jupyter prints the last return value
def foo_bar():
print('Foo') # 1st print that runs
12 / 13
Functions_1.md 7/21/2022
return 1
print('Bar')
res = foo_bar()
print(res) # 2nd print that runs
Foo
1
nice
def foo_bar():
print('Foo') # 1st print that runs
return 1
print('Bar')
res = foo_bar()
print(res) # 2nd print that runs
Foo
1
nice
13 / 13