0% found this document useful (0 votes)
2 views69 pages

Week 5 MAK

The document covers logical statements and decision structures in Python programming, focusing on Boolean expressions, comparison operators, and various conditional statements such as if, if-else, and if-elif-else. It provides examples of proper and poor usage of these statements, emphasizing the importance of using nested structures for clarity and efficiency. Additionally, it includes exercises for practicing conditional logic in programming.

Uploaded by

nixak34028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views69 pages

Week 5 MAK

The document covers logical statements and decision structures in Python programming, focusing on Boolean expressions, comparison operators, and various conditional statements such as if, if-else, and if-elif-else. It provides examples of proper and poor usage of these statements, emphasizing the importance of using nested structures for clarity and efficiency. Additionally, it includes exercises for practicing conditional logic in programming.

Uploaded by

nixak34028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

WEEK 5: Logical Statements and

DECISION STRUCTURES

INS 107E Introduction to Programming Languages (Python)

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

>>> not True


False

4
Boolean expression

>>> not True


False

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'

>>> name = input('Please enter your name: ') or 'unknown'


Please enter your name: Ahmet
>>> name
'Ahmet'
13
Comparison Operators
Expression Meaning of the Expression
== Equality
!= Non-Equality
> Greater Than
< Less Than
>= Greater Than or Equal
<= Less Than or Equal
in Membership in sequence
not in Lack of membership
is Equivalence
not is Non-Equivalence
The operators is and not is test for object identity: ‘x is y’ is true if and only if x and y are the
same object. Object identity is determined using the id() function. 14
Comparison Operators

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!')

"""a script for using an if


statement firstIf.py"""
x=25
if x> 10:
print('x is greater than ten!') 22
If Statement

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

"""a script for using multiple if statements multIf.py"""


x=24
Output
if x%2 == 0: 24 is divisible by 2
print(x,' is divisible by 2') 24 is divisible by 3
if x%3 == 0: 24 is divisible by 4
print(x,' is divisible by 3')
if x%4 == 0:
print(x,' is divisible by 4')
if x%5 == 0:
print(x,' is divisible by 5')
if x%2 != 0 and x%3 != 0 and x%4 != 0 and x%5 != 0:
print(x,' is not divisible by 2,3,4 and 5')

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

Use nested if-else statements! 34


Poor Usage of If Statements
"""This is an example of
poor usage of if
statements poorIf.py"""
x=14
Output
if x==15: 14 is not 15
print(x,'is 15')
if x!=15:
print(x,'is not 15')
Use nested if-else statements!
35
The If-Elif-Else Statement

Only executed if condition1 is true

Only executed if condition1 is if condition1:


FALSE and condition2 is TRUE action(s)
elif condition2:
action(s)
Only executed if condition1 and
else:
condition2 are BOTH FALSE
action(s)
36
If-Elif-Else Structure

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

"""This is an example of poor usage of


if statements badIf.py"""
x=7
if x>10:
print(x,'is greater than 10')
if x<=10 and x>=5:
print(x,'is between 5 and 10')
if x>=0 and x<5:
print(x,'is between 0 and 5')
if x<0: 7 is between
print(x,'is negative')
Output 5 and 10

Use nested if-elif-else statements!41


Poor Usage of If Statements

42
Poor Usage of If Statements

43
Poor Usage of If Statements

44
Proper Use of If Statements

"""This is an example of poor


usage of if statements badIf.py"""
x=7
if x>10:
Output
print(x,'is greater than 10') 7 is between
elif x<=10 and x>=5: 5 and 10
print(x,'is between 5 and 10')
elif x>=0 and x<5:
print(x,'is between 0 and 5')
elif x<0 :
print(x,'is negative')

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

Print(‘It is a little bit cold’)

FALSE TRUE
T < 25 ⁰C

Print(‘It is a little bit hot’) Print(‘It is a nice day’)


51
Proper Use of Nested If-Else Statement

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)

FALSE T< TRUE if(T<10):


10 ⁰C print('it is a little bit cold')

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)

print('it is a little bit cold') if(T<10):


else: print('it is a little bit cold')
if (T<25): elif(T<25):
print('It is a nice day') print('It is a nice day')
else: else:
print('It is a little bit hot') print('It is a little bit hot')

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")

Output Sorry, a is not equal to b


x=67
guess=int(input("make a guess(the number is between 0 and 100)"))
if guess==x:
print("You win")
else:
print("You lost")
make a guess(the number is between 0 and 100)90
Output You lost 57
Examples

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

You might also like