LM | Conditionals |
Conditionals
Conditionals allow your program to make decisions based on certain conditions. In Python, you use:
if
statementselif
(else if) statementselse
statements
Conditionals help you create dynamic and responsive programs that can react to different inputs or situations.
Examples
x = 10
if x > 5:
print("x is greater than 5")
if x > 5:
print("x is greater than 5")
else:
print("x is less than 5 or equal to 5")
# Example with multiple conditions
temperature = 25
if temperature > 30:
print("It's hot outside.")
elif temperature > 20:
print("The weather is nice.")
elif temperature < -20:
print("The weather is perfect! :)")
else:
print("It's a bit cold.")
Using conditionals makes your code flexible and able to handle different cases in a clear and structured way.