Programming languages have a series of constructs that allow a programmer to control what portions of their program execute, and if necessary, how many times to repeat that code. The first type of control structure to learn is the if statement.
An if statement will execute a section of code, known as a code block, if a boolean expression, known as a condition has a true result.
number = 100
if number == 100:
print("Code inside a code block.")
print("More code inside a code block...")
If the condition is true, the code block, which is the code indented under the if statement, will execute. If the condition is false, the code block is skipped over, and does not execute.
Example Video
# Age Checker Program
age = int(input("Enter your age: "))
if age >= 13:
print("You're old enough to join Cobra Kai.\n")
if age >= 16:
print("You're old enough to get your license!\n")
if age >= 18:
print("You're old enough to live on your own.")
print("So... Get out of your parents' basement!\n")
if age >= 35:
print("You're old enough to run for president.")
name = input("Enter your full name: ")
year = input("What year will you run? ")
nickname = input("What's your nickname (adjective): ")
print(f"\n{nickname} {name} for president! Coming {year}")
Enter your age: 35
You're old enough to join Cobra Kai.
You're old enough to get your license!
You're old enough to live on your own.
So... Get out of your parents' basement!
You're old enough to run for president.
Enter your full name: Anthony Mortimer
What year will you run? 2020
What's your nickname (adjective): Sleepy
Sleepy Anthony Mortimer for president! Coming 2020