Skip to main content

How to Use an if Statement in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Programs make decisions: warn when it's hot, skip a step when a file is missing. Python's if runs its indented block only when its condition is true, and elif and else handle the other cases.

Syntax​

Syntax
if <condition>:
<runs when the condition is true>
elif <other condition>:
<runs when the first is false and this one is true>
else:
<runs when none were true>
PartWhat it does
if <condition>:Tests the condition. When it's true, its block runs and the rest is skipped.
elif <other condition>:Optional, and you can have several. Tested only when everything above was false.
else:Optional. Runs when no condition was true.
IndentationMarks which lines belong to each branch. Four spaces is the convention.

The Python language reference: "The if statement is used for conditional execution". It runs the block of the first condition that's true; if none is, it runs the else block, when there is one.

The Example​

The example describes the weather from a temperature: hot above 30, warm above 20, cool otherwise. The temperature is 24.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which sets temperature to 24 and prints It is hot. when it is above 30, It is warm. when it is above 20, and It is cool. otherwise.

main.py
temperature = 24

if temperature > 30:
print("It is hot.")
elif temperature > 20:
print("It is warm.")
else:
print("It is cool.")

Step 2: Run It​

Terminal
uv run python main.py
Output
It is warm.

A zsh terminal where uv run python main.py prints It is warm.

24 > 30 was false, so Python moved on; 24 > 20 was true, so it printed It is warm. and skipped the else. The command is the same in zsh, bash and PowerShell.

Comparing Values​

OperatorMeansExample that's True
==Equal5 == 5
!=Not equal5 != 6
> and <Greater than, less than24 > 20
>= and <=Greater or equal, less or equal20 >= 20

Combine conditions with and and or, and turn one around with not.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the colon, as in if t > 30SyntaxError: expected ':'End the if, elif and else lines with :
Not indenting the blockIndentationError: expected an indented block after 'if' statement on line 2Indent the block by four spaces
Writing = instead of ==, as in if t = 30:SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?Use == to compare

Troubleshooting​

SymptomLikely causeFix
A branch you expected never runsA condition above it already matchedPut the narrowest condition first
TabError: inconsistent use of tabs and spaces in indentationTabs and spaces are mixed in one blockIndent with spaces only

FAQ​

Can the condition be something other than a comparison?​

Yes. Any value works: empty strings, 0, None and empty lists count as false, and almost everything else as true.

Is there a switch statement?​

Python has match, which compares one value against several patterns. For a few conditions, if and elif are simpler.

Conclusion​

if runs its block when the condition is true, elif tries the next condition, and else catches the rest. End each line with a colon and indent the block.

Additional Resources​