Conditionals allow a program to make decisions based on certain conditions. Python uses if
, elif
, and else
statements to execute different blocks of code based on Boolean expressions.
if
is used to check a condition. If the condition isTrue
, the code inside theif
block is executed.elif
(short for "else if") is used to check multiple conditions after an initialif
condition.else
is used to define a block of code that runs if none of the previous conditions are met.
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition1 is False and condition2 is True
else:
# Code to execute if none of the conditions are True
age = 18
if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult!")
else:
print("You are an adult.")
# Output: You just became an adult!
- Condition: Each condition is a Boolean expression that evaluates to either
True
orFalse
. - Block of Code: If the condition is
True
, the block of code under that condition is executed. - Indentation: In Python, code blocks are defined by indentation, not curly braces.
temperature = 25
if temperature > 30:
print("It's hot outside!")
elif temperature >= 20:
print("It's a nice day!")
else:
print("It's cold outside!")
# Output: It's a nice day!
You can nest conditional statements within each other to check multiple levels of conditions.
x = 10
y = 5
if x > 0:
if y > 0:
print("Both x and y are positive.")
else:
print("x is positive, but y is not.")
else:
print("x is not positive.")
# Output: Both x and y are positive.
- Boolean Expressions: Conditions are based on Boolean expressions, which evaluate to
True
orFalse
. - Chaining with
elif
: You can chain as manyelif
statements as needed to check multiple conditions. - Order Matters: Conditions are checked in the order they appear. Once a condition is
True
, the corresponding block of code is executed, and the rest are ignored. - Indentation: Make sure to maintain consistent indentation for code blocks.
Python allows you to write conditionals in one line for simple conditions. (But I will not going to use it in my code. It's hard to read π€―)
x = 5
y = 10
print("x is greater than y") if x > y else print("x is less than or equal to y")
# Output: x is less than or equal to y
β¬ οΈ Previous: String Manipulation and Methods
β‘οΈ Next: Loops (
for
,while
)π Back to Top
π Home