Using Conditional Statements Slides
Using Conditional Statements Slides
Programming Fundamentals
Using Conditional Statements
Mateo Prigl
Software Developer
Prerequisites
List Dictionary
Boolean Data Type
True False
What if we want to
conditionally control the
flow of the program?
Controlling the Program's Flow with an if Statement
The code block that belongs to the if statement is indented
script.py
bool_var = True
if bool_var:
print("It is true!")
print("It is really true!")
print("Moving on!")
Controlling the Program's Flow with an if Statement
The code block that belongs to the if statement is indented
script.py
bool_var = False
if bool_var:
print("It is true!")
print("It is really true!")
print("Moving on!")
Explicit Type Conversion
script.py
int_num = int(float_num)
print(int_num)
string_num = str(int_num)
print(string_num)
Boolean Type Conversion
script.py
n1 == n3 # 3 == 3 True t Equal
n1 != n2 # 3 != 5 True t Not equal
Using the else Statement
The else statement has to be defined after the code block of the if statement
script.py
script.py
script.py
True True
num > 20 and num < 50 True t The and operator checks if all of the
# 30 > 20 and 30 < 50 conditions are true
False True
num == 20 and num != 50 False t If any one of the conditions is false, the whole
# 30 == 20 and 30 != 50 expression is false
True True
num > 20 or num < 50 True t The or operator checks if any one of the
# 30 > 20 and 30 < 50 conditions is true
False True
num == 20 or num != 50 True t If just one of the conditions is true, the whole
# 30 == 20 and 30 != 50 expression is true
num = 30
not num == 30 False t The not operator applies to only one condition
# not 30 == 30 and it reverses its boolean value
Implementing Loops