Python Data Essentials:
Programming Fundamentals
Using Conditional Statements
Mateo Prigl
Software Developer
Prerequisites
Run Python scripts
Set up Python on Basic data types and
from files and
your OS structures
notebooks
Who should be taking this course?
- You want to learn the basics of Python
Overview programming
- You want to get into data analysis with
Python (Pandas, NumPy...)
All code and slides are available to
download from the "Exercise Files" tab on
the course's page
Ask questions in the "Discussion" tab; don't
ask questions in the feedback form
Welcome to the course!
Python Data Types
Integer Float String
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
> python3 script.py
string_num = '5.5'
5.5
print(string_num)
5.5
5
float_num = float(string_num)
5
print(float_num)
int_num = int(float_num)
print(int_num)
string_num = str(int_num)
print(string_num)
Boolean Type Conversion
script.py
print(bool(None)) # None keyword (no value, null) > python3 script.py
False
print(bool(0)) # Numbers equal to zero False
False
print(bool([])) # Empty list False
False
print(bool({})) # Empty dictionary True
True
print(bool(())) # Empty tuple
print(bool(3)) # Numbers which are not zero
print(bool([1,3])) # Non-empty list
Comparison Operators
n1 = 3
n2 = 5
n3 = 3
n1 < n2 # 3 < 5 True t Less than
n1 > n2 # 3 > 5 False t Greater than
n1 <= n3 # 3 <= 3 True t Less than or equal to
n3 >= n2 # 3 >= 5 False t Greater than or equal to
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
> python3 script.py
Number is 4
number = 4 Done with checking the
True number
if number == 4:
print("Number is 4")
else:
print("Number is not 4")
print("Done with checking the number")
Using the else Statement
The else statement has to be defined after the code block of the if statement
script.py
> python3 script.py
Number is not 4
number = 2 Done with checking the
False number
if number == 4:
print("Number is 4")
else:
print("Number is not 4")
print("Done with checking the number")
Using the elif Statement
The elif statement has to be defined after the code block of the if statement
script.py
> python3 script.py
number = 3 Number is 3
Done with checking the
if number == 4: False number
print("Number is 4")
elif number == 3: True
print("Number is 3")
else:
print("Number is not 3 or 4")
print("Done with checking the number")
Logical Operators
num = 30
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
t If the condition results in false, the not
not num != 30 True
# not 30 != 30 operator will negate it to true
not False True
not True False
t If the value is true, the not operator will negate
it to false
not num False
# not True
Up Next:
Implementing Loops