Programming often involves situations where the program must be able to distinguish between different situations. In these cases we use conditional statements. The simplest conditional statement looks like this:
number = int(input("Please type in a number: "))
if number > 100:
print("The number was greater than one hundred")
number = 0
print("Thank you!")
Here the condition is number > 100. The code block following this header line is executed only if the condition is true.
📊 Comparison Operators
Operator
Purpose
Example
==
Equal to
a == b
!=
Not equal to
a != b
>
Greater than
a > b
>=
Greater than or equal to
a >= b
<
Less than
a < b
<=
Less than or equal to
a <= b
2. If Statements
The keyword if starts a conditional statement. It is followed by a condition and a colon. The block of code that should be executed if the condition is true comes after the colon, indented.
temperature = int(input("Please type in a temperature: "))
if temperature > 0:
print("The temperature is above zero")
print("Done!")
Sample output (with input 15):
Please type in a temperature: 15
The temperature is above zero
Done!
Sample output (with input -20):
Please type in a temperature: -20
Done!
If statement flow chart
3. Indentation
Python uses indentation to define code blocks. The indented code under an if statement forms a block that is executed only when the condition is true.
⚠️ Important Indentation Rules
Many text editors will automatically indent the following line when the Enter key is pressed after a colon character. When you want to end an indented code block you can use the Backspace key to return to the beginning of the line.
Use Backspace to unindent code blocks
4. Else Statements
Often we want the program to execute some other code if the condition is false. This can be achieved with an else statement.
temperature = int(input("Please type in a temperature: "))
if temperature > 0:
print("The temperature is above zero")
else:
print("The temperature is below zero")
print("Done!")
Sample output (with input 15):
Please type in a temperature: 15
The temperature is above zero
Done!
Sample output (with input -20):
Please type in a temperature: -20
The temperature is below zero
Done!
If-else statement flow chart
5. Elif Statements
Often there are more than two options the program should account for. For example, the temperature could be exactly zero. This can be achieved with an elif statement.
temperature = int(input("Please type in a temperature: "))
if temperature > 0:
print("The temperature is above zero")
elif temperature < 0:
print("The temperature is below zero")
else:
print("The temperature is exactly zero")
print("Done!")
Sample output:
Please type in a temperature: 0
The temperature is exactly zero
Done!
The keyword elif is short for "else if". There can be any number of elif statements in a conditional statement.
grade = int(input("Please type in your grade: "))
if grade == 5:
print("Excellent!")
elif grade == 4:
print("Very good")
elif grade == 3:
print("Good")
elif grade == 2:
print("Satisfactory")
elif grade == 1:
print("Pass")
else:
print("Fail")
6. Combining Conditions
Conditions can be combined with the logical operators and and or.
The and Operator
The keyword and combines two conditions. The combined condition is true only if both of the component conditions are true.
number = int(input("Please type in a number: "))
if number >= 5 and number <= 10:
print("The number is between 5 and 10")
The or Operator
The keyword or also combines two conditions, but it returns true if either one or both of the conditions are true.
number = int(input("Please type in a number: "))
if number < 5 or number > 10:
print("The number is not between 5 and 10")
The not Operator
The keyword not negates a condition.
number = int(input("Please type in a number: "))
if not (number >= 5 and number <= 10):
print("The number is not between 5 and 10")
7. Nested Conditionals
Conditional statements can also be nested within each other.
username = input("Please type in your username: ")
if username == "jerry":
password = input("Please type in your password: ")
if password == "swordfish":
print("Welcome!")
else:
print("Incorrect password!")
else:
print("Incorrect username!")
⚠️ Important: While nested conditionals can be useful, too much nesting can make code difficult to read. Consider reorganizing your code if it becomes too deeply nested.
8. Programming Exercises
Complete the following exercises to practice conditional statements in Python!
📝 Exercise 1: Absolute Value (1 point)
Write a program that asks for an integer number and then prints its absolute value.
Please type in a number: -7
The absolute value is 7
# Write your code here
📝 Exercise 2: Soup or No Soup (1 point)
Write a program that asks for a temperature and then prints "Soup time!" if it's 10 degrees or colder.
Please type in a temperature: 8
Soup time!
# Write your code here
📝 Exercise 3: Orwell (1 point)
Write a program that asks for an integer number. If the number is 1984, print "Orwell".
Please type in a number: 1984
Orwell
# Write your code here
📝 Exercise 4: Greater or Equal (1 point)
Write a program that asks for two integers. Print which is greater, or if they are equal.
Please type in the first number: 5
Please type in the second number: 3
The greater number was: 5
# Write your code here
📝 Exercise 5: The Elder (1 point)
Write a program that asks for the names and ages of two persons and prints the name of the elder.
Person 1:
Name: Alan
Age: 26
Person 2:
Name: Ada
Age: 27
The elder is Ada
# Write your code here
📝 Exercise 6: Alphabetically in the Middle (1 point)
Write a program that asks for three words and prints the middle one alphabetically.
Please type in the 1st word: first
Please type in the 2nd word: second
Please type in the 3rd word: third
The word in the middle is second
# Write your code here
📝 Exercise 7: Grade and Points (1 point)
Write a program that translates points to grades: 0-49 points = fail, 50-59 = 1, 60-69 = 2, 70-79 = 3, 80-89 = 4, 90-100 = 5.
Please type in the amount of points [0-100]: 75
Grade: 3
# Write your code here
📝 Exercise 8: FizzBuzz (1 point)
Write a program that asks for a number. If divisible by 3, print "Fizz". If by 5, print "Buzz". If by both, print "FizzBuzz". Otherwise print the number.
Number: 15
FizzBuzz
# Write your code here
🎉 Summary
You've completed Conditional Statements! You now know how to: