Conditional statements and operators

Introduzione a Python per sviluppatori

Jasmin Ludolf

Senior Data Science Content Developer

Booleans

# Boolean variable
the_truth = True
print(the_truth)
True
  • Used to make comparisons
Introduzione a Python per sviluppatori

Operators

  • Comparison operators

    • Symbols or combinations of symbols
    • Used to compare values
  • Check if two things are equal

    • ==
Introduzione a Python per sviluppatori

Checking for equality

# Compare if 2 is equal to 3
2 == 3
False
# Check that 2 is not equal to 3
2 != 3
True
  • Common use-case: checking login details
Introduzione a Python per sviluppatori

Numeric comparison operators

# Is 5 less than 7?
5 < 7
True
# Is 5 less than or equal to 7?
5 <= 7
True
# Is 5 greater than 7?
5 > 7
False
# Is 5 greater or equal to 7?
5 >= 7
False
Introduzione a Python per sviluppatori

Other comparisons

# Is James greater than Brian
"James" > "Brian"
True
  • Strings are evaluated in alphabetical order
Introduzione a Python per sviluppatori

Conditional statements

  • if condition is met, then perform action, otherwise skip
# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity
Introduzione a Python per sviluppatori

Conditional statements

  • if condition is met, then perform action, otherwise skip
# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity:
Introduzione a Python per sviluppatori

Conditional statements

  • if condition is met, then perform action, otherwise skip
# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!")
Introduzione a Python per sviluppatori

Indentation

# Check pasta quantities
required_quantity = 500
pasta_quantity = 200


# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!") # This line is not indented
    print("You have enough pasta!")
    ^
IndentationError: expected an indented block
Introduzione a Python per sviluppatori

Elif statement

# Check pasta quantities
required_quantity = 500
pasta_quantity = 200

# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!")
elif pasta_quantity >= 300: print("Nearly enough pasta. Try a smaller portion.")
  • Can use as many elif keywords as we like!
Introduzione a Python per sviluppatori

Else statement

# Check pasta quantities
required_quantity = 500
pasta_quantity = 200

# Compare pasta quantities if pasta_quantity >= required_quantity:
print("You have enough pasta!")
elif pasta_quantity >= 300: print("Nearly enough pasta. Try a smaller portion.")
# Otherwise... else: print("Not enough pasta.")
Not enough pasta.
Introduzione a Python per sviluppatori

Comparison operators cheat sheet

Operator Function
== Equal to
!= Not equal to
> More than
>= More than or equal to
< Less than
<= Less than or equal to
Keyword Function Use
if If condition is met First in the workflow
elif Else check if condition is met After if
else Else perform this action After elif
Introduzione a Python per sviluppatori

Let's practice!

Introduzione a Python per sviluppatori

Preparing Video For Download...