Conditional Statements
Conditional statements are used to make decisions in your Python programs. They are used to check a condition and execute different code based on the condition. The most common conditional statements are if, elif, and else.
Basic If Statement
The simplest form of conditional execution:
age = 18if age >= 18: print("You are an adult")If the condition is True, the code inside the if block is executed. If the condition is False, the code inside the if block is not executed.
Notice how we pushed the code inside the if block to the next line by indenting it. This is a convention in Python to make the code more readable. Other languages like JavaScript, Java, C++, etc. requires the use curly braces {} to delimit the code block, and explicitly mark the start and end of the block with { and }. However, Guido chose to use indentation to delimit the code block. We will talk more about the indentation rules in the next chapter, but for now just remember that we indent the code inside the if block to the next line.
If-Else Statement
The if-else statement is used to execute different code based on the condition with two possible outcomes, where the else block is executed if the condition is False.
age = 16if age >= 18: print("You are an adult")else: print("You are a minor")Note
Similarly, the code inside the
elseblock is also indented to the next line.
If-Elif-Else Statement
The if-elif-else statement is used to execute different code based on the condition with multiple possible outcomes. The first condition is checked and if it is True, the code inside the if block is executed. If the first condition is False, the next condition is checked and if it is True, the code inside the elif block is executed. If all conditions are False, the code inside the else block is executed.
age = 16if age >= 18: print("You are an adult")elif age >= 13: print("You are a teenager")else: print("You are a minor")Note
We can mix and match between
if,elif, andelsestatements. We must start with anifstatement and then can have any number ofelifstatements and optionally end with oneelsestatement.
Nesting Conditional Statements
We can nest conditional statements inside other conditional statements. This is useful when we need to check multiple conditions.
a = 10b = 5if a > b: if a > 0: print("a is positive and greater than b") else: print("a is negative and greater than b")else: print("a is less than b")Of course this could have been easily written as:
a = 10b = 5if a > b and a > 0: print("a is positive and greater than b")elif a > b and a < 0: print("a is negative and greater than b")else: print("a is less than b")However, we would have needed to evaluate the condition a > b twice, furthermore this code adds a little bit of complexity (which would be more evident in larger programs).
Conditional Expression (Ternary Operator)
The conditional expression (ternary operator) is a shorthand way to write an if-else statement. It is a single line expression that can be used to evaluate a condition and return a value. Some languages use the syntax condition ? expression1 : expression2, but Python uses the syntax expression1 if condition else expression2. It is written as:
expression1 if condition else expression2Where condition is the condition to evaluate, expression1 is the expression to return if the condition is True, and expression2 is the expression to return if the condition is False.
a = 10b = 5result = a if a > b else bprint(result)Note
Here the
elsepart is not optional.
Exercises
Exercise 1: Fill in the blanks
Fill in the blanks to print “Hello” if a is greater than b.
a = 10b = 5 a b print("Hello")a = 10b = 5if a > b: print("Hello")Exercise 2: Fill in the blanks
Fill in the blanks to print Yes, if a is equal to b otherwise print No
a = 10b = 5 a b print( )
print( )a = 10b = 5if a > b: print("Yes")else: print("No")Exercise 3: Even or Odd
Create a program that checks if a number is even or odd, it should print “Even” if the number is even and “Odd” if the number is odd.
n = int(input("Enter a number: "))if n % 2 == 0: print("Even")else: print("Odd")Exercise 4: Maximum between two numbers
Create a program which reads from the user two numbers, and print the maximum between them
a = int(input("Enter the first number: "))b = int(input("Enter the second number: "))if a > b: print(a)else: print(b)