Python Conditions
Conditional Statements in Python
Conditional statements allow you to run specific blocks of code depending on whether a condition is true or false. They are essential for decision-making in programs.
Syntax of if Statements
The most basic conditional in Python is the if
statement:
if condition:
code_block
condition
: an expression that evaluates toTrue
orFalse
.code_block
: runs only if the condition is true.
Basic Example
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
If the condition x > 5
is true, the message is printed.
Adding an else Clause
An else
clause allows you to run code when the condition is false:
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
Output:
x is 5 or less
Using elif (Else If)
The elif
keyword lets you test multiple conditions in order:
x = 7
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but 10 or less")
else:
print("x is 5 or less")
Output:
x is greater than 5 but 10 or less
Python checks each condition from top to bottom. Once one is true, it runs the corresponding block and skips the rest.
Comparison Operators
You’ll often use these in conditions:
==
: equal to!=
: not equal to<
: less than<=
: less than or equal to>
: greater than>=
: greater than or equal to
Example:
x = 5
if x == 5:
print("x is exactly 5")
Output:
x is exactly 5
Logical Operators
You can combine multiple conditions using:
and
: all conditions must be trueor
: at least one condition must be truenot
: inverts a condition
Example with and:
x = 8
if x > 5 and x < 10:
print("x is between 6 and 9")
Output:
x is between 6 and 9
Example with or:
x = 3
if x < 2 or x > 4:
print("x is outside the range 2–4")
else:
print("x is between 2 and 4")
Output:
x is outside the range 2–4
Nested if Statements
You can place an if
inside another if
:
x = 12
if x > 10:
if x < 20:
print("x is between 11 and 19")
Output:
x is between 11 and 19
While nesting is allowed, it's better to keep logic flat when possible for readability.
Practical Example: Grading System
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: D or lower")
Output:
Grade: B
Truthy and Falsy Values
In Python, certain values automatically evaluate as False
:
False
None
0
(any numeric type)""
(empty string)[]
,{}
,()
(empty collections)
Everything else is considered True
.
Example:
if []:
print("List is not empty")
else:
print("List is empty")
Output:
List is empty
Summary
- Use
if
,elif
, andelse
to control program flow based on conditions. - Comparison and logical operators help define complex conditions.
- Be aware of truthy and falsy values when testing variables.
- Keep logic readable—flatten conditions where possible.
Conditional statements are the foundation of decision-making in programming. Mastering them will make your programs more flexible and intelligent.
Continue Learning
Python Variables
Popular
### Understanding Variables and Literals in Python Variables and literals are the foundation of any
Personalized Recommendations
Log in to get more relevant recommendations based on your reading history.