Week 5 MAK
Week 5 MAK
DECISION STRUCTURES
19.03.2025
Dr. Merve AKBAŞ KAPLAN 1
Boolean expression
>>> False == 0
True
>>> True == 1
True
2
Boolean expression
>>> False == 0
True
>>> True == 1
True
3
Boolean expression
4
Boolean expression
5
Boolean expression
6
Boolean expression
7
Boolean expression
8
Boolean expression
9
Boolean expression
10
Boolean expression
11
Boolean expression
12
Boolean expression
>>> name = input('Please enter your name: ') or 'unknown'
Please enter your name:
>>> name
'unknown'
15
Comparison Operators
16
Comparison Operators
17
Comparison Operators
18
Comparison Operators
>>> x=1 >>>x=[1,2,3] >>>x=[1,2,3]
>>> y=2 >>> y=x >>> y=[1,2,3]
>>> x==y >>> id(y) >>> id(x)
False 1354915992328 1354915992456
>>> z=1 >>> id(x) >>> id(y)
>>> x==z 1354915992328 1354915992072
True >>> x is y >>> x is y
>>> y>=z True False
True
19
If Statement
20
If Statement
21
If Statement
Script Output
"""a script for using an if
statement first.py"""
x=5
if x> 10:
print('x is greater than ten!')
23
If Statement
Script Output
"""ifexample.py""" [0,1,2,3]
x=[0,1,2,3]
if x:
print(x)
➢Be sure about how
3 non-zero values
"""ifexample2.py""" behave in if
x,y=1,2 statements.
if x>0: ➢Any non-zero
if y>0: number in a logical
statement ----> true
x+y
24
If Statement
25
If Statement
26
Multiple If Statements
27
Multiple If Statements
28
29
The If-Else Statement
30
The If-Else Statement
31
The If-Else Statement
if condition
if condition: if condition:
action(s) action(s)
else: else:
action(s) action(s)
else:
action(s)
32
Poor Usage of If Statements
33
Poor Usage of If Statements
If
True False
end elif
True False
if condition1:
end elif action(s)
True False elif condition2:
action(s)
end else
else:
True
action(s)
end
38
If-Elif-Else Structure
39
If-Elif-Else Structure
40
Poor Usage of If Statements
42
Poor Usage of If Statements
43
Poor Usage of If Statements
44
Proper Use of If Statements
45
Proper Use of If Statements
46
Proper Use of If Statements
47
Even Better Use of Nested If-Else
Statement
"""This is an example of poor
usage of if statements badIf.py"""
x=7
if x>10: Output
print(x,'is greater than 10')
elif x>=5: 7 is between
5 and 10
print(x,'is between 5 and 10')
elif x>=0:
print(x,'is between 0 and 5')
else:
print(x,'is negative')
48
Even Better Use of Nested If-Else
Statement
49
Even Better Use of Nested If-Else
Statement
50
Proper Use of Nested If-Else Statement
Read temperature of the
day
FALSE TRUE
T < 10 ⁰C
FALSE TRUE
T < 25 ⁰C
52
Proper Use of Nested If-Else Statement
Read temperature Script
of the day """temperatureIf.py"""
T=input('read temperature values in Celcius : ')
T=int(T)
Print(‘It is else:
a little bit if (T<25):
cold’)
print('It is a nice day')
FALSE T< TRUE else:
25 ⁰C
print('It is a little bit hot')
Output
Print(‘It is a Print(‘It is a
read temperature values in Celcius : 30
little bit hot’) nice day’) It is a little bit hot 53
Proper Use of Nested If-Else Statement
"""temperatureIf.py""" """temperaturebetterIf.py"""
T=input('read temperature values in Celcius : ') T=input('read temperature values in Celcius :
')
T=int(T)
if(T<10): T=int(T)
Output
read temperature values in Celcius : 30 read temperature values in Celcius : 30
54
It is a little bit hot It is a little bit hot
Examples
x=[] x=[0,1,2,3]
if x: Output if x: Output
print(x) Empty List print(x) [0,1,2,3]
else: else:
print('Empty List') print('Empty List')
x=-6
if x<0:
print('It is a negative number')
elif x==0:
print('It is a zero') Output
else: It is a negative number
print('It is a positive number') 55
Examples
56
a=3
Examples b=2
if a==b:
print("a is equal to b")
else:
print("Sorry, a is not equal to b")
58
Short-circuit for conditional expressions
(Ternary
TRUE
Expression) FALSE
< action1(s)>if condition else < action2(s)>
age=15 Output
print('kid' if age<18 else 'adult') kid
age=15 Output
print('kid' if age<13 else
teenager
'teenager' if age<18 else 'adult')
age=15 Output
print('kid' if age<13 else teenager
'adult' if age>18 else 'teenager') 59
Short-circuit for conditional expressions
(Ternary Expression)
Examples
>>>x,y,z='a','','c' >>> x,y,z=1,2,3
>>> x if y else z >>> if(x and y and z):print(x+y+z)
'c' 6
>>> if x>y:print(x)
>>>x,y,z='a','-1','c'
>>> x if y else z
>>> if x<y:print(x)
'a'
1
>>> v=x if y else z
'a'
60
Short-circuit for conditional expressions
(Ternary Expression)
Examples
61
Recitation
1) Write down a program where you get as input the raise of the user and as output
what the user can buy. Print the output in a sentence. The condition is given as:
If the user’ gets $1000 per week raise, he or she can buy a new car;else,if the raise
is greater than $100, he or she can buy a new laptop; otherwise, he or she can only
put the raise into savings.
62
Recitation
63
2) Write down a program for the piecewise function y as follows:
y= x^2+3 if x>=5 , y=√x if 0<x<5 , y=0 if x<=0
64
65
3) Write down a program where you get a number from the user as input and
print out whether the number is even or odd.
66
67
4) Write a program where you print the grade for a score given by the user.
If the score is out of range, print an error message.
If the score is between 0.0 and 1.0, print a grade using the following table:
68
69