Python if Statements Explained

An if statement lets Python make a decision.

It checks a condition. If that condition is True, Python runs the indented code underneath it. If the condition is False, Python skips that code.

This is one of the most important tools in Python because it controls the flow of your program.

Quick example

age = 18

if age >= 18:
    print("You are an adult")

Use if when you want code to run only when a condition is True.

Two important rules:

  • End the if line with a colon :
  • Indent the code inside the if block

What an if statement does

An if statement helps your program choose whether to do something.

It works like this:

  • Python checks a condition
  • If the condition is True, the block runs
  • If the condition is False, the block is skipped

You can think of it as:

“If this is true, do this.”

Example:

temperature = 30

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

Output:

It is a warm day

Because temperature > 25 is true, Python runs the print() line.

Basic if statement syntax

A basic if statement has four parts:

  • The keyword if
  • A condition
  • A colon :
  • An indented block of code

Example:

score = 80

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

The key line is:

if score >= 50:

This means:

  • check whether score is greater than or equal to 50
  • if it is, run the indented line below it

If you are still getting used to variables, see Python variables explained for beginners.

How conditions work

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

Python uses that result to decide whether to run the if block.

Common comparison operators:

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

Example:

age = 16

print(age >= 18)

Output:

False

Since age >= 18 is False, this if block will not run:

age = 16

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

Conditions often compare:

  • numbers
  • strings
  • variables

String example:

username = "admin"

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

If you want to understand True and False better, read Python booleans explained: True and False.

Simple examples beginners understand

Here are a few common beginner examples.

Check if a number is positive

number = 7

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

Check if a password matches

password = "python123"

if password == "python123":
    print("Access granted")

Check if a string is empty

name = ""

if name == "":
    print("The string is empty")

Check if a user is old enough

age = 20

if age >= 18:
    print("You are old enough")

These examples all follow the same pattern:

  • write a condition
  • let Python check it
  • run code only if it is true

Using if with boolean values

A boolean value is either True or False.

You can use a boolean variable directly in an if statement.

Example:

is_logged_in = True

if is_logged_in:
    print("Welcome back")

This works because is_logged_in already holds a boolean value.

You do not need to write:

if is_logged_in == True:

That works, but this is cleaner:

if is_logged_in:

Another example:

has_email = False

if has_email:
    print("Email found")

Since has_email is False, nothing is printed.

What happens when the condition is False

If the condition is False, Python skips the indented block.

Then the program continues with the next line after the block.

Example:

age = 15

if age >= 18:
    print("Adult")

print("Program finished")

Output:

Program finished

The line print("Adult") does not run because the condition is false.

This page focuses only on plain if statements.

If you want code to run when the condition is false, learn Python if-else and elif explained.

Indentation rules to remember

Indentation is very important in Python.

The code inside an if statement must be indented. This tells Python which lines belong to the block.

Correct:

age = 18

if age >= 18:
    print("Adult")

Incorrect:

age = 18

if age >= 18:
print("Adult")

The second example causes an error because the print() line is not indented.

Rules to remember:

  • indent every line inside the if block
  • use consistent spaces
  • do not mix indentation styles in the same file

If indentation is confusing, see Python indentation rules and why they matter.

Common beginner mistakes

Here are some common problems when writing if statements.

Forgetting the colon

This is wrong:

age = 18

if age >= 18
    print("Adult")

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

Correct version:

age = 18

if age >= 18:
    print("Adult")

If you see this error, read SyntaxError: missing colon fix.

Using = instead of ==

This is a very common mistake.

Wrong:

username = "admin"

if username = "admin":
    print("Welcome")

Correct:

username = "admin"

if username == "admin":
    print("Welcome")

Remember:

  • = assigns a value
  • == compares two values

Not indenting the block

Wrong:

age = 18

if age >= 18:
print("Adult")

Correct:

age = 18

if age >= 18:
    print("Adult")

If Python says it expected an indented block, see IndentationError: expected an indented block fix.

Expecting the block to run when the condition is False

An if block only runs when the condition is True.

If the condition is false, Python skips it.

To check what is happening, print the value and the condition result:

age = 16

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

Useful debugging checks:

print(age)
print(type(age))
print(age >= 18)
print(username == "admin")

These help you confirm:

  • the actual value
  • the data type
  • whether the condition is true or false

Comparing unexpected types

Sometimes a value looks correct but has the wrong type.

Example:

age = "18"

if age >= 18:
    print("Adult")

This does not work because "18" is a string, not a number.

You can inspect the type with:

age = "18"
print(type(age))

FAQ

What is an if statement in Python?

It is a control statement that runs code only when a condition is True.

Do I need a colon after if in Python?

Yes. The if line must end with a colon.

What is the difference between = and == in an if statement?

= assigns a value. == compares two values.

Can an if statement run more than one line?

Yes. Any indented lines under the if statement are part of the block.

Example:

age = 18

if age >= 18:
    print("Adult")
    print("Access allowed")

What if I want code to run when the condition is False?

Use if-else. That is covered on the Python if-else and elif explained page.

See also

Once you understand a single if statement, the next step is to learn how to build full decision logic with if, else, elif, and boolean expressions.