0% found this document useful (0 votes)
15 views

16 Functions 1 Code

The document discusses functions in Python. It provides examples of defining functions using the def keyword, calling functions, and returning values from functions. It also discusses scope, arguments, built-in functions, updating variables inside and outside of functions, and flow of execution within functions.

Uploaded by

Rahul B. Waghale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

16 Functions 1 Code

The document discusses functions in Python. It provides examples of defining functions using the def keyword, calling functions, and returning values from functions. It also discusses scope, arguments, built-in functions, updating variables inside and outside of functions, and flow of execution within functions.

Uploaded by

Rahul B. Waghale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Functions_1.

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

Updating rating of Mayank


current_sum = current_rating * total_number
current_rating = (current_sum + current_participant) / (total_number + 1)

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

Updating rating of Rahul


current_sum = current_rating * total_number
current_rating = (current_sum + current_participant) / (total_number + 1)

total_number += 1

print(current_rating)
print(total_number)

4.500000000000001
12

Update rating of Chayan


current_rating = 5

current_sum = current_rating * total_number


current_rating = (current_sum + current_participant) / (total_number + 1)

total_number += 1

print(current_rating)
print(total_number)

2 / 13
Functions_1.md 7/21/2022

4.461538461538463
13

Update rating of person Y


current_rating = 3

current_sum = current_rating * total_number


current_rating = (current_sum + current_participant) / (total_number + 1)

total_number += 1

print(current_rating)
print(total_number)

4.42857142857143
14

Functions to the rescue - no need to repeat code


def update_rating(current_rating, total_ratings, current_participant):
"""This function updates the rating of the class"""

current_sum = current_rating * total_ratings


current_rating = (current_sum + current_participant)/ (total_ratings +
1)
total_ratings += 1
return current_rating

current_rating = update_rating(4.5, 10, 5)


print(current_rating)

4.545454545454546

current_rating = update_rating(current_rating, 11, 4)


print(current_rating)
3 / 13
Functions_1.md 7/21/2022

4.500000000000001

current_rating = update_rating(current_rating, 12, 5)


print(current_rating)

4.538461538461539

current_rating = update_rating(current_rating, 13, 3)


print(current_rating)

4.428571428571429

Syntax of a function
def function_name(parameters):
"""
Doc String
"""
statement(s)

def add(x, y):


"""
Function adds two numbers x and y
"""
return x + y

res = add(3, 5)
print(res)

4 / 13
Functions_1.md 7/21/2022

res = add(3.5, 5.5)


print(res)

9.0

res = add("hello ", "world")


print(res)

hello world

res = add(3, 3.5)


print(res)

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

Printing inside function


add_2_numbers(5, 7)
add_2_numbers(5.7, 9.6)
add_2_numbers(-1, -2)

5 / 13
Functions_1.md 7/21/2022

def add_2_numbers(x, y):


print(x + y)

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) # jupyter will automatically print it

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

Final problem for the day


1.) Write a function to covert the input temperature on fahrenheit scale to celcius scale using the following
$$C=\frac{5}{9}*(F-32)$$
Round the result to 2 decimal places

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)

Help on built-in function round in module builtins:

round(number, ndigits=None)
Round a number to a given precision in decimal digits.

The return value is an integer if ndigits is omitted or None.


Otherwise
the return value has the same type as the number. ndigits may be
negative.

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

print('nice') # 3rd 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

print('nice') # 3rd print that runs

Foo
1
nice

13 / 13

You might also like