The if
statement allows you to execute a block of code only if a specified condition is true. It is a fundamental decision-making structure in Python.
if condition:
# code to execute if condition is True
age = 18
if age >= 18:
print("You are eligible to vote.")
The condition inside the if
statement is evaluated. If it is true, the indented block of code under it runs. Otherwise, it is skipped.
# Example 1
num = 10
if num > 0:
print("Positive number")
# Example 2
temperature = 35
if temperature > 30:
print("It's a hot day")
# Example 3
name = "Alice"
if name == "Alice":
print("Hello, Alice!")
# Example 4
score = 75
if score >= 50:
print("You passed!")
# Example 5
is_raining = False
if not is_raining:
print("Let's go for a walk!")