Session 3
Session 3
Abbas Mahbod
Fall-2022
1
Previous Session
• Python Operator Precedence
• Input
• Comment
• Booleans
• if
• functions
2
Outline
3
Outline
• Functions
4
Outline
• Functions
• IDE
5
Outline
• Functions
• IDE
• While Loop
6
Outline
• Functions
• IDE
• While Loop
• For Loop
7
Functions
8
Functions
• Build-in functions
9
Functions
• Build-in functions
• Print- type, int (5.4), …
10
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions
• import math
11
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions
• import math
12
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions
• import math
• Our functions
13
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions def function’s name (Condition):
• import math ….Statement 1
• Our functions ….Statement 2
….Statement 3
⋮
14
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions def function’s name (Condition):
• import math ….Statement 1
• Our functions ….Statement 2
….Statement 3
⋮
15
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions def function’s name ():
• import math ….Statement 1
• Our functions ….Statement 2
….Statement 3
⋮
16
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions def function’s name ():
• import math ….Statement 1
• Our functions ….Statement 2
….Statement 3
⋮
17
Functions
• Build-in functions
• Print- type, int (5.4), …
• Library functions def function’s name ():
• import math ….Statement 1
• Our functions 4 spaces recommended ….Statement 2
….Statement 3
⋮
18
def function’s name ():
Functions ….Statement 1
4 spaces recommended ….Statement 2
….Statement 3
⋮
• Define a function that prints (‘hello’)
19
def function’s name ():
Functions ….Statement 1
4 spaces recommended ….Statement 2
….Statement 3
⋮
• Define a function that prints (‘hello’)
20
def function’s name ():
Functions ….Statement 1
4 spaces recommended ….Statement 2
….Statement 3
⋮
• Define a function that prints (‘hello’)
21
Functions
• Functions can get input
22
Functions
• Functions can get input
def function’s name (inputs):
….Statement 1
4 spaces recommended ….Statement 2
….Statement 3
⋮
23
Functions
• Functions can get input
24
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets the name as input and prints (hello
+name)
25
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets the name as input and prints (hello
+name)
26
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets the name as input and prints (hello
+name)
27
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets the name as input and prints (hello
+name)
28
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets the name as input and prints (hello
+name)
29
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets two numbers and returns the summation.
30
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets two numbers and returns the summation.
31
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets two numbers and returns the summation.
32
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets two numbers and returns the summation.
33
def function’s name (inputs):
Functions ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
return (output)
• Define a function that gets two numbers and returns their difference.
34
Library functions
35
Library functions
• import random
• random.randint (1, 99)
36
Library functions
• import random
• random.randint (1, 99)
37
Library functions
• import random
• random.randint (1, 99)
38
Library functions
• import random
• random.randint (1, 99)
39
Library functions
• import random
• random.randint (1, 99)
40
Library functions
• import random
• random.randint (1, 99)
41
Library functions
• import random
• random.randint (1, 99)
42
Library functions
• import random
• random.randint (1, 99)
43
IDE
44
IDE
45
IDE
46
Some Examples
• Define a Function that takes two numbers as the input and returns
the lower one.
47
Some Examples
• Define a Function that takes two numbers as the input and returns
the lower one.
48
Some Examples
• Define a Function that takes a number from user and if he/she is
older than 90, prints (‘U R OverQualified!’), if he/she is between 10
to 90, prints (‘Accpted!’), and if he/she is less than 10 years old,
prints (‘U R too Young!’)
49
Some Examples
• Define a Function that takes a number from user and if he/she is
older than 90, prints (‘U R OverQualified!’), if he/she is between 10
to 90, prints (‘Accpted!’), and if he/she is less than 10 years old,
prints (‘U R too Young!’)
50
While Loops
51
While Loops
• Iterations are one of the main components of each programming
language.
52
While Loops
• Iterations are one of the main components of each programming
language.
While (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
53
While Loops
• Iterations are one of the main components of each programming
language.
While (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
54
While Loops
• Iterations are one of the main components of each programming
language.
While (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
55
While Loops
• Iterations are one of the main components of each programming
language.
While (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
56
While Loops
• Iterations are one of the main components of each programming
language.
While (Condition):
….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
57
While (Condition):
While Loops ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
58
While (Condition):
While Loops ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
59
While (Condition):
While Loops ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
60
While (Condition):
While Loops ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
61
While (Condition):
While Loops ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
• break
62
While (Condition):
While Loops ….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
• break
63
While Loops
• How many number will be printed?
i = 3
while i>=0:
print(i)
i = i - 1
64
While Loops
• How many number will be printed?
i = 5
while True:
print(i)
i = i – 1
if i <= 2:
break
65
For Loops
66
For Loops
for (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
67
For Loops
for (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
68
For Loops
for (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
69
For Loops
for (Condition):
….Statement 1
….Statement 2
….Statement 3
⋮
70
For Loops
for (Condition):
….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
71
For Loops for (Condition):
….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
72
For Loops for (Condition):
….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
73
For Loops for (Condition):
….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
74
For Loops for (Condition):
….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
75
For Loops for (Condition):
….Statement 1
4 spaces recommended
….Statement 2
….Statement 3
⋮
76
For Loops
• Write a piece of code that gets numbers from the user. It should keep
doing that until -1 is entered by the user. Once the user enters -1 as
the input, the average of all numbers (excluding -1) should be
printed.
77
For Loops
• Write a piece of code that gets numbers from the user. It should keep
doing that until -1 is entered by the user. Once the user enters -1 as
the input, the average of all numbers (excluding -1) should be
printed.
78
Some Examples
79
Some Examples
• Write a piece of code that first takes the age of two different users
and then prints the average at the output.
80
Some Examples
• Write a piece of code that first takes the age of two different users
and then prints the average at the output.
• Write a piece of code that first takes two numbers, if the summation
of the numbers is more than 100, prints the average, otherwise prints
the multiplication of them.
81
Some Examples
• Write a piece of code that first takes the age of two different users
and then prints the average at the output.
• Write a piece of code that first takes two numbers, if the summation
of the numbers is more than 100, prints the average, otherwise prints
the multiplication of them.
• Write a piece of code that gets two numbers form the user and prints
the greater one. If the numbers are equal, prints ‘Equal’.
82
Some Examples
• Write a piece of code that first takes the age of two different users
and then prints the average at the output.
• Write a piece of code that first takes two numbers, if the summation
of the numbers is more than 100, prints the average, otherwise prints
the multiplication of them.
• Write a piece of code that gets two numbers form the user and prints
the greater one. If the numbers are equal, prints ‘Equal’.
• Define a function that takes two numbers, if the summation of the
numbers is more than 100, returns the average, otherwise returns the
multiplication of them.
83
Some Examples
• Define a function that gets the per hour rate, and total hours. Then
computes the salary.
84
Some Examples
• Define a function that gets the per hour rate, and total hours. Then
computes the salary.
85
Some Examples
• Define a function that gets the per hour rate, and total hours. Then
computes the salary.
86