Programs need to make decisions. 'If' checks a condition: if it's True, do something. 'Else' does something different if it's False. 'Elif' (else-if) checks another condition if the first was False. Comparison operators: == (equal), != (not equal), >, <, >=, <=
# If-else statements
# Example 1: Basic if-else
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult!")
else:
print("You are a minor!")
# Example 2: Multiple conditions with elif
temperature = float(input("Enter temperature in Celsius: "))
if temperature > 30:
print("It's hot! Stay hydrated.")
elif temperature > 20:
print("Nice weather!")
elif temperature > 10:
print("It's cool. Wear a jacket.")
else:
print("It's cold! Bundle up.")
# Example 3: Checking text
password = input("Enter password: ")
if password == "python123":
print("Access granted!")
else:
print("Access denied!")