An if statement in Python lets a program make decisions. It checks a condition, and the indented code below it runs only when that condition is true. Almost every real program uses this, from checking a password to reading a sensor value. If you are still getting started with Python, this is one of the first control-flow tools to learn.
This guide covers the basic syntax, elif and else, nested if statements, the operators you will use in conditions, and the mistakes beginners make most often.
Note: the examples below use Python 3, where print is a function and needs parentheses. Older Python 2 code wrote print 'hai' without them, which no longer works in current versions.
How the if statement works
The basic form is the keyword if, a condition, a colon, and an indented block of code:
if condition:
# code that runs when the condition is trueTwo details matter here. The colon at the end of the line is required. The indentation is also required, because Python uses it to decide which lines belong to the if block. The usual convention is four spaces per level.
Simple if example
x = 3
if x == 3:
print("hai")
print("hai")Output
hai
haiHere x == 3 is true, so both indented lines run. If x were any other number, nothing would be printed. Note the double equals sign: == compares two values, while a single = assigns a value.
if, elif and else
Often you want to choose between several outcomes. Use elif (short for “else if”) to add more conditions, and else to catch everything that did not match. Python checks the conditions from top to bottom and runs only the first block whose condition is true.
x = 2
if x == 3:
print("hai")
elif x > 1:
print("hhh")Output
hhhThe first condition (x == 3) is false, so Python moves on. The second condition (x > 1) is true, so it prints hhh. You can add a final else to handle any other case:
x = 0
if x == 3:
print("hai")
elif x > 1:
print("hhh")
else:
print("x is 1 or less")With x = 0, both conditions fail, so the else block runs and prints x is 1 or less. An else block never has a condition of its own, and you can have any number of elif branches but only one else, which must come last.
Nested if statements
You can place an if statement inside another one. This is called nesting, and each level is indented further.
x = 2
if x == 3:
print("hai")
elif x > 1:
print("hhh")
if x > 0:
print("hhh")Output
hhh
hhhThe program enters the elif branch because x > 1 is true and prints hhh. Inside that branch, x > 0 is also true, so it prints hhh a second time. Python does not need semicolons at the end of lines, so they can be left out.
Deep nesting gets hard to read. If you find yourself three or four levels down, consider combining conditions with and instead (see below).
Operators used in conditions
Comparison operators
==equal to!=not equal to>and<greater than and less than>=and<=greater than or equal to, less than or equal to
Logical operators
Use and, or and not to combine or reverse conditions:
age = 20
if age >= 18 and age < 65:
print("Working-age adult")This is usually cleaner than nesting one if inside another when both checks lead to the same action.
Membership check
The in operator tests whether a value is inside a string, list or other collection:
colour = "red"
if colour in ["red", "green", "blue"]:
print("Primary colour")Truthy and falsy values
A condition does not have to be a comparison. Python treats some values as false: , an empty string "", an empty list [], and None. Most other values count as true. This allows short checks like:
name = ""
if not name:
print("Please enter a name")Using if with user input
If statements become more useful when the program reacts to what the user types. Input always arrives as text, so convert it to a number before comparing. Our guide to reading input in Python explains this in more detail.
number = int(input("Enter a number: "))
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")Conditions also control loops. Once you are comfortable with if, the while loop in Python uses the same kind of condition to repeat code. If you build messages from text in your conditions, see string concatenation in Python for ways to join strings.
One-line conditional expression
For a simple choice between two values, Python offers a compact form:
x = 5
label = "big" if x > 3 else "small"
print(label)This prints big. Use it only for short, simple choices; a regular if block is easier to read when the logic grows.
Common mistakes
- Using = instead of ==.
if x = 3:is a syntax error. Use==to compare. - Forgetting the colon. Every
if,elifandelseline must end with:. - Wrong indentation. Lines in the same block must be indented by the same amount. Mixing tabs and spaces causes an
IndentationError. - Comparing text to numbers. The value
"3"frominput()is not equal to the number3. Convert it withint()first. - Putting else before elif. The
elseblock must always be last.
More examples
For additional practice, see these more examples.
FAQ
What is the difference between if and elif?
if starts a decision. elif adds another condition that is checked only when the earlier ones were false. Python stops at the first branch that matches.
Is there an else if in Python?
Python uses the keyword elif instead of writing “else if” as two words.
Do I need parentheses around the condition?
No. Python does not require them, so if x == 3: is the normal style. You can use parentheses to group complex conditions, but they are optional.
Does Python have a switch statement?
Python 3.10 and later include match and case for pattern matching, which can replace long if/elif chains in some cases. For most beginner programs, if, elif and else are enough.