02 Conditional Statements
02 Conditional Statements
Conditional statements
Sequential flow
Statement 1
Python read and execute each statement
in the script file sequentially = line by line
Until the end of the file
Statement 2
....
Statement n
if statements
Example:
x=2
y=0
z=0
z=x/y
print ('z=',z)
if statements
Example:
x=2
y=0 ERROR:
z=0 ZeroDivisionError
z=x/y
print ('z=',z)
if statements
x=2
y=0 To avoid this error we have to check:
z=0 if y is not equal to 0
z=x/y before to execute the statement: z=x/y
print ('z=',z)
if statement constructs - general syntax
if condition is True:
Statement 1
…
Statement N
if statement constructs - general syntax
if condition is True:
Statement 1
…
Statement N
if statement constructs - general syntax
Indentation
if condition is True: Could be space/tab
Statement 1
At least 1 space or 1 tab
…
Statement N Every Statement in the block
Must start at the same column
All the statements indented by the same number of character spaces/tab after a
programming construct are considered to be part of a single block of code.
Example:
a == b
a<b
Operators - Logical Operations
x = 4
y = 3
if condition is True:
Statement 1
Statement 2
else:
Statement 3
Statement 4
if - else statements
x=2
y=0
z=0
if y != 0:
z=x/y
else:
print ('div by 0 not possible')
Print('z=',z)
Gives:
div by 0 not possible
if - elif - else statements
elif Allows you to check multiple expressions for truth value and
execute a block of code as soon as one of the conditions
evaluates to true.
if condition1 is True:
Statement 1
Statement 2
elif condition2 is True:
Statement 3
Statement 4
elif condition3 is True:
Statement 5
Statement 6
else:
Statement 7
Some notes about the code
x=10
if x>30:
print ('x is big')
elif x==10:
print ('x equal to 10')
elif x<=0:
print ('x is 0 or negative')
else:
print ('other')
print ('bye')
Gives: x equal to 10
bye
if - elif - else statements
if x>30:
print ('x is big')
elif x==10:
print ('x equal to 10')
elif x<=0:
print ('x is 0 or negative')
else:
print ('other')
print ('bye')
x = 35
What is the result in
x = 7 each case?
x = -15
if within an if – Nested ifs
if condition1 is True:
Statement 1
if condition2 is True:
Statement 2
if within an if – Nested ifs
x = 100
if x < 200:
print ('x value is less than 200')
if x == 100:
print ('Which is 100')
elif x == 50:
print ('Which is 50')
elif x < 50:
print ('x value is less than 50')
else:
print ('Could not find true expression')
print ('bye')
if within an if – Nested ifs
x = 100
if x < 200:
The if block
print ('x value is less than 200')
if x == 100:
print ('Which is 100')
elif x == 50:
print ('Which is 50')
elif x < 50:
print ('x value is less than 50')
else:
print ('Could not find true expression')
print ('bye')
if within an if – Nested ifs
x = 100
if x < 200:
The nested
print ('x value is less than 200')
if x == 100:
if block
print ('Which is 100')
elif x == 50:
print ('Which is 50')
elif x < 50:
print ('x value is less than 50')
else:
print ('Could not find true expression')
print ('bye')
Gives:
x value is less than 200
Which is 100
bye!
if statements characters based (string)
If str1 == str2:
Print 'str1 equal str2'
if str2 in str1:
print (str2+' is in the string:'+str1)
else:
print ('is not..')
print ('bye!')
Gives:
spam is in the string:Monty Python spam script
bye!