Python if statements for beginners

Once you understand variables, data types, user input, and operators, the next major step in Python is decision-making. This is where if statements become essential. An if statement allows a program to check a condition and then decide what to do next. Without this kind of logic, a program can only run the same way every time. With if statements, it can respond to values, compare input, and behave differently depending on the situation.

For beginners, this is one of the most important turning points in learning Python. Up to this point, you have mainly stored data, displayed output, and performed calculations. With if statements, your code starts making choices. That is what makes programs feel more intelligent, interactive, and useful.

This lesson explains Python if statements for beginners in a clear, practical way. You will learn how if, elif, and else work, how conditions are evaluated, how indentation controls structure, what common beginner mistakes look like, and how decision-making fits into real Python programs.

What is an if statement in Python

An if statement checks whether a condition is true. If the condition is true, Python runs a block of code. If the condition is false, Python skips that block.

Basic example:

age = 20

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

In this example, Python checks whether age >= 18.

  • if the condition is true, it prints the message
  • if the condition is false, it does nothing

This is the foundation of decision-making in Python.

Why if statements matter

Without if statements, a Python program cannot react to changing conditions. It can only execute a fixed sequence of instructions. That is very limiting.

With if statements, a program can:

  • check user input
  • test whether a number is valid
  • compare values
  • allow or deny access
  • choose between multiple outputs
  • respond differently in different situations

For example, if a user enters an age, the program can decide whether they are an adult or a minor. If a password is correct, the program can allow access. If a number is even, the program can say so.

This is why if statements are so important. They move Python from static output into logical behavior.

How Python evaluates a condition

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

For example:

print(5 > 3)

Output:

True

And:

print(2 == 7)

Output:

False

An if statement uses that kind of expression.

Example:

temperature = 30

if temperature > 25:
print(“It is warm today”)

Python checks the condition temperature > 25.

  • if it is True, the message is printed
  • if it is False, the message is skipped

This is the basic pattern behind every if statement.

Basic syntax of an if statement

The syntax of a simple if statement looks like this:

if condition:
code_to_run

Important details:

  • the line begins with if
  • the condition comes after if
  • the line ends with a colon :
  • the next line is indented

Example:

score = 85

if score >= 50:
print(“Passed”)

The colon and indentation are both essential. Without them, the syntax is wrong.

Why indentation matters in Python if statements

Indentation is part of Python syntax. In an if statement, the indented block shows which code belongs to the condition.

Example:

age = 18

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

Both indented lines belong to the if block. They run only if the condition is true.

If the code is not indented correctly, Python raises an error or changes the structure.

Incorrect example:

age = 18

if age >= 18:
print(“Adult”)

This fails because the print line should be indented.

For beginners, this is one of the most common early mistakes. In Python, indentation is not optional formatting. It is part of the language.

Simple if statement examples

Here are a few basic examples that show how if works.

Example 1: Positive number

number = 5

if number > 0:
print(“The number is positive”)

Example 2: Password length

password = “python123”

if len(password) >= 8:
print(“Password length is acceptable”)

Example 3: High score

score = 92

if score > 90:
print(“Excellent result”)

These are simple, but they already show the core idea: Python checks a condition and runs code only when that condition is true.

What happens when the condition is false

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

Example:

age = 15

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

In this case, nothing is printed, because the condition is false.

That is normal behavior. An if statement does not guarantee that something happens. It only says: run this block if the condition is true.

Using else in Python

Often, you want the program to do one thing if the condition is true and something else if it is false. That is what else is for.

Example:

age = 15

if age >= 18:
print(“You are an adult”)
else:
print(“You are a minor”)

Now the program always chooses one of the two outputs.

How it works:

  • if age >= 18 is true, Python runs the if block
  • otherwise, Python runs the else block

This makes the logic more complete.

Syntax of if and else

Basic structure:

if condition:
code_if_true
else:
code_if_false

Example:

number = 8

if number % 2 == 0:
print(“Even”)
else:
print(“Odd”)

This is one of the most common beginner patterns in Python.

Using elif in Python

Sometimes there are more than two possible cases. That is where elif comes in. The word elif means “else if”.

Example:

score = 75

if score >= 90:
print(“Grade A”)
elif score >= 75:
print(“Grade B”)
else:
print(“Grade C or lower”)

Python checks the conditions in order:

  1. is score >= 90 true
  2. if not, is score >= 75 true
  3. if not, run else

Since the score is 75, the second condition is true, so Python prints Grade B.

Why elif is useful

Without elif, you would often need more awkward logic. elif makes it possible to handle multiple possible outcomes in a clean sequence.

It is useful when checking:

  • score ranges
  • age groups
  • menu options
  • status levels
  • categories
  • price bands

For beginners, it is the natural next step after learning if and else.

Order matters in if, elif, else chains

Python checks if and elif conditions from top to bottom, and it stops at the first true condition.

Example:

score = 95

if score >= 50:
print(“Passed”)
elif score >= 90:
print(“Excellent”)

Output:

Passed

Even though the score is also above 90, Python never reaches the elif because the first condition was already true.

This means order matters.

Correct version:

score = 95

if score >= 90:
print(“Excellent”)
elif score >= 50:
print(“Passed”)
else:
print(“Failed”)

Now the logic works as intended.

This is a very common beginner issue. More specific conditions often need to appear before broader ones.

Comparison operators used in if statements

if statements usually rely on comparison operators such as:

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

Examples:

age = 20

if age == 20:
print(“Age is exactly 20”)

price = 100

if price != 0:
print(“Price is not zero”)

temperature = 30

if temperature > 25:
print(“Warm”)

These operators are what make conditions meaningful.

Using logical operators in if statements

Sometimes one condition is not enough. In that case, you can combine conditions using logical operators.

Using and

and means both conditions must be true.

age = 22
has_ticket = Trueif age >= 18 and has_ticket:
print(“Entry allowed”)

Using or

or means at least one condition must be true.

is_admin = False
is_editor = Trueif is_admin or is_editor:
print(“Access granted”)

Using not

not reverses a boolean value.

is_logged_in = False

if not is_logged_in:
print(“Please log in”)

Logical operators make if statements much more powerful.

Using if statements with user input

One of the most practical uses of if statements is checking input from the user.

Example:

age = int(input(“Enter your age: “))

if age >= 18:
print(“You may continue”)
else:
print(“You are too young”)

This combines several earlier concepts:

  • input
  • type conversion
  • variables
  • comparison
  • conditional logic

That combination is what makes beginner Python programs start to feel real.

String comparisons in if statements

if statements can also compare strings.

Example:

color = input(“Enter a color: “)

if color == “blue”:
print(“You chose blue”)
else:
print(“That is not blue”)

This works, but string comparisons are case-sensitive.

For example, if the user types Blue, the condition fails.

A common solution is to normalize the input:

color = input(“Enter a color: “).lower()

if color == “blue”:
print(“You chose blue”)
else:
print(“That is not blue”)

Now Blue, BLUE, and blue all behave the same.

Nested if statements

An if statement can appear inside another if statement. This is called nesting.

Example:

age = 20
has_id = Trueif age >= 18:
if has_id:
print(“Access granted”)

This means:

  • first check if the age is high enough
  • then check if ID is available

Nested if statements can be useful, but beginners should use them carefully. Too much nesting can make code harder to read.

In many simple cases, logical operators are cleaner:

age = 20
has_id = Trueif age >= 18 and has_id:
print(“Access granted”)

This version is shorter and often easier to understand.

Truthy and falsy values in Python

In beginner Python, conditions often use direct comparisons. But Python also treats some values as true or false automatically.

For example:

name = “Emma”

if name:
print(“Name exists”)

This works because a non-empty string is treated as true.

Example with an empty string:

name = “”

if name:
print(“Name exists”)
else:
print(“No name entered”)

An empty string is treated as false.

Similarly:

  • 0 is false
  • non-zero numbers are true
  • empty lists are false
  • non-empty lists are true
  • False is false
  • True is true
  • None is false

Beginners do not need to memorize all of this immediately, but it is useful to know that Python sometimes evaluates values directly in conditions.

Common beginner mistakes with if statements

Several mistakes appear often when beginners start using if.

Using = instead of ==

Incorrect:

age = 18

if age = 18:
print(“Match”)

This causes an error.

Correct:

age = 18

if age == 18:
print(“Match”)

Remember:

  • = assigns
  • == compares

Forgetting the colon

Incorrect:

age = 18

if age >= 18
print(“Adult”)

Correct:

age = 18

if age >= 18:
print(“Adult”)

The colon is required.

Wrong indentation

Incorrect:

age = 18

if age >= 18:
print(“Adult”)

Correct:

age = 18

if age >= 18:
print(“Adult”)

Comparing input as text instead of number

Incorrect:

age = input(“Enter your age: “)

if age >= 18:
print(“Adult”)

This is wrong because age is a string.

Correct:

age = int(input(“Enter your age: “))

if age >= 18:
print(“Adult”)

Poor order in elif chains

Incorrect order can produce wrong results:

score = 95

if score >= 50:
print(“Passed”)
elif score >= 90:
print(“Excellent”)

Correct order:

score = 95

if score >= 90:
print(“Excellent”)
elif score >= 50:
print(“Passed”)
else:
print(“Failed”)

Practical beginner examples

Example 1: Even or odd

number = int(input(“Enter a number: “))

if number % 2 == 0:
print(“The number is even”)
else:
print(“The number is odd”)

Example 2: Password check

password = input(“Enter the password: “)

if password == “python123”:
print(“Access granted”)
else:
print(“Access denied”)

Example 3: Temperature category

temperature = float(input(“Enter the temperature: “))

if temperature < 0:
print(“Below freezing”)
elif temperature < 20:
print(“Cool”)
else:
print(“Warm”)

Example 4: Login state

is_logged_in = True

if is_logged_in:
print(“Welcome back”)
else:
print(“Please log in”)

Example 5: Ticket check

age = int(input(“Enter your age: “))
has_ticket = input(“Do you have a ticket? “).lower()if age >= 18 and has_ticket == “yes”:
print(“Entry allowed”)
else:
print(“Entry denied”)

These examples show how if statements work with numbers, text, booleans, input, and logical operators.

Why if statements are essential before loops and functions

if statements are one of the central building blocks of programming. They appear in almost every larger topic that comes later.

  • loops often use conditions
  • functions often include decision-making
  • file handling checks whether something exists
  • error handling checks what went wrong
  • user interfaces depend on conditions
  • games, forms, and calculators all use decisions

That is why it is worth learning if statements carefully now. If this part is stable, many future lessons become easier.

Good beginner habits with if statements

A few habits can make your conditional code clearer and more reliable.

Keep conditions readable

Instead of writing confusing logic, try to make conditions easy to understand.

Good:

if age >= 18:
print(“Adult”)

Use meaningful variable names

Good names make the condition self-explanatory.

if has_access:
print(“Welcome”)

is clearer than:

if x:
print(“Welcome”)

Test both true and false cases

Do not only test the condition when it works. Also check what happens when it fails.

Use indentation carefully

Indentation mistakes are one of the most common beginner errors in Python.

Prefer clarity over cleverness

Short code is not always better. Clear code is better.

What you should understand before moving on

Before continuing to the next lesson, you should be comfortable with the following:

  • if checks whether a condition is true
  • else runs when the if condition is false
  • elif handles additional conditions
  • conditions return True or False
  • indentation defines the code block
  • == compares values, while = assigns values
  • if statements often use comparison and logical operators
  • order matters in if / elif chains
  • input-based programs often rely on if statements

If these ideas are clear, then the next lesson on logical and comparison operators, or on loops depending on your structure, will feel much easier.

Python if statements are the foundation of decision-making in code. They allow a program to check conditions and respond differently depending on whether something is true or false. Combined with else and elif, they make it possible to build logic that reacts to user input, compares numbers, checks text values, and controls program flow.

For beginners, if statements are one of the most important steps in learning Python because they transform simple scripts into responsive programs. Once you understand how conditions, indentation, and branching work, you are ready to build much more useful Python code.


Image(s) used in this article are either AI-generated or sourced from royalty-free platforms like Pixabay or Pexels.

This article may contain affiliate links. If you purchase through these links, we may earn a commission at no extra cost to you.

Weekly briefing

Get the weekly RF & IT briefing

Radio guides, RF calculators, AI, Windows, Linux and satellite communication explainers. One useful email per week. No spam.

Similar Posts