1. If-Else Statements
The if-else statement allows you to make decisions in your code. If a
condition is true, the code inside the if block is executed; otherwise, the
code inside the else block is executed.
Syntax:
if condition:
# Execute this block if the condition is True
else:
# Execute this block if the condition is False
Example:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Output: You are an adult.
Elif (Else If):
Sometimes you need to check multiple conditions. In such cases, you can
use the elif (else if) statement to test multiple conditions.
Syntax:
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
, # Code if neither condition1 nor condition2 is True
Example:
age = 15
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
Output: You are a teenager.
2. Logical Operators
Logical operators allow you to combine multiple conditions to make more
complex decisions.
o and: Returns True if both conditions are True.
o or: Returns True if at least one condition is True.
o not: Reverses the truth value of a condition.
Example:
x=5
y = 10
if x > 3 and y < 15:
print("Both conditions are True")
else:
print("At least one condition is False")
Output: Both conditions are True