What Is an if Statement in Python?

An if statement in Python lets your program make a decision.

It checks a condition. If that condition is True, Python runs the code inside the if block. If the condition is False, Python skips that code.

This is one of the most important ideas in Python because it helps programs respond to different situations.

Quick example

age = 18

if age >= 18:
    print("You can vote")

Output:

You can vote

Use if when you want Python to make a decision based on a condition.

Simple definition

An if statement lets Python decide whether to run some code.

Here is the basic idea:

  • An if statement lets Python make a decision.
  • It checks a condition.
  • If the condition is True, the indented code runs.
  • If the condition is False, Python skips that block.

For example:

temperature = 30

if temperature > 25:
    print("It is a warm day")

Because temperature > 25 is True, Python prints the message.

What a condition means

A condition is an expression that becomes either True or False.

This matters because an if statement only works with something that can be tested in that way.

Common comparison operators are:

  • == equal to
  • != not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

Example:

score = 75

print(score >= 50)

Output:

True

That result can be used inside an if statement:

score = 75

if score >= 50:
    print("You passed")

Conditions often compare:

  • numbers
  • strings
  • variables

They also rely on the Boolean values True and False. If you are new to those values, see Python booleans explained: True and False.

Basic structure

A basic if statement looks like this:

if condition:
    do_something()

The structure is:

  • Start with the keyword if
  • Write a condition after if
  • End the line with a colon :
  • Indent the code that should run when the condition is True

Here is a real runnable example:

name = "Maya"

if name == "Maya":
    print("Hello, Maya")

Output:

Hello, Maya

Indentation is very important in Python. It tells Python which lines belong to the if block. If you want a fuller explanation, read Python indentation rules and why they matter.

Small real example

Here is a simple example that checks whether a number is positive:

number = 5

if number > 0:
    print("The number is positive")

Output:

The number is positive

Now look at this version:

number = -2

if number > 0:
    print("The number is positive")

Output:

# No output

Nothing is printed because the condition number > 0 is False.

This shows the main job of an if statement: run code only when a test passes.

Why beginners use if statements

Beginners use if statements all the time because they make programs more useful.

Common uses include:

  • checking user input
  • making a program react differently in different situations
  • validating data before using it
  • preventing errors by checking first

Example:

username = "sam"

if username != "":
    print("Welcome,", username)

This kind of check helps make sure a value is usable before the program continues.

If you want to learn how Python handles more than one choice, the next step is Python if, else, and elif explained.

What this page does not cover

This page explains only the basic meaning of an if statement.

It does not fully cover:

  • if with else
  • elif for multiple conditions
  • complex Boolean logic

For a full beginner lesson with more branching examples, see Python if statements explained and Python if, else, and elif explained.

Common mistakes

Here are some common beginner mistakes with if statements.

Forgetting the colon

This is wrong:

age = 18

if age >= 18
    print("You can vote")

Python needs a colon at the end of the if line.

If you get an error, see SyntaxError: missing colon.

Not indenting the code block

This is also wrong:

age = 18

if age >= 18:
print("You can vote")

The line inside the if block must be indented.

If needed, read IndentationError: expected an indented block.

Using = instead of ==

Use == when you want to compare values.

Correct:

age = 18

if age == 18:
    print("Age is 18")

= is for assigning a value. == is for checking whether two values are equal.

Expecting the block to run when the condition is False

An if block runs only when the condition is True.

If the condition is False, Python skips the block and moves on.

Simple debugging checks

If your if statement is not doing what you expect, print the condition result:

age = 16

print(age >= 18)
print(type(age >= 18))

Output:

False
<class 'bool'>

You can also test a condition directly:

age = 20
print(age >= 18)

Output:

True

These small print() checks can help you see what Python is actually evaluating.

FAQ

What does an if statement do in Python?

It checks a condition and runs code only if that condition is True.

Does an if statement always need else?

No. An if statement can be used by itself.

What kind of value goes inside an if statement?

A condition that becomes True or False.

Why is indentation important in an if statement?

Indentation tells Python which lines belong to the if block.

See also