HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›Conditions (if / elif / else)

Conditions (if / elif / else)

Comparison operators, decision-making with if-elif-else, and/or logic, and the importance of indentation.

Simple enough for a 5-year-old to understand

if: decide based on a condition

if runs the indented block below it when a condition is true. A colon (:) comes after the condition.

Example code
1
age = 20
A variable.
VariableA variable.
2
if age >= 18:
Is age greater than/equal to 18? If yes, the next line runs.
ConditionIs age greater than/equal to 18? If yes, the next line runs.
3
print("Adult")
The indentation (4 spaces) tells Python this line belongs to the if.
Output / InputThe indentation (4 spaces) tells Python this line belongs to the if.
VariableConditionOutput / Input
💡 Hint: Equality is checked with == (double equals). A single = is assignment, and is not used in a condition.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.In Python, which operator checks equality?

About this topic

Conditions give a program the ability to make decisions. Up to that point, the code you wrote ran the exact same way from top to down; with if, you branch the flow by saying "if this is true, do that". This is the step that turns a script into a real program.

In Python, the scope of a condition is determined by INDENTATION. While other languages use curly braces, Python looks at the number of spaces: every line written indented after the if line belongs to that condition. This is Python's most distinct feature and the most frequent source of errors.

What you will learn

  • branching the flow with if, elif, and else
  • Comparison operators: equal, not equal, less than, greater than
  • combining conditions with and, or, and not
  • how indentation determines the scope of a condition
  • Which values are considered "empty/false"

When to use?

You use conditions in input validation (is the age negative?), permission checks (is the user an admin?), limit checks (is the stock sufficient?), and error prevention (does the file exist?).

If you are comparing the same variable against many values, using a dictionary instead of long elif chains or switching to the match statement in Python 3.10 and above makes the code much more readable.

Common mistakes

Wrong

if age = 18:
    print("exact")

A single equals sign is for ASSIGNMENT, a double equals sign is for COMPARISON. Because you tried to do an assignment inside the condition, you get a "SyntaxError: invalid syntax".

Correct

if age == 18:
    print("exact")

Wrong

if age > 18
    print("greater")

A colon is mandatory at the end of the if line; Python understands that blocks are beginning from it. If missing, it throws a "SyntaxError: expected ':'" error.

Correct

if age > 18:
    print("greater")

Wrong

if score > 50:
print("passed")

The lines belonging to the condition must be indented. If there is no indentation, Python says "IndentationError: expected an indented block". To keep indentation consistent, always use 4 spaces, and never mix tabs and spaces.

Correct

if score > 50:
    print("passed")

Frequently asked questions

Is there a difference between using elif and writing separate if statements?

Yes, there is an important difference. In an elif chain, only the FIRST true condition runs, and the rest are skipped. If you write separate if's, they are all checked one by one, and if more than one is true, all of them run. In mutually exclusive situations like grade ranges, you must use elif.

Which values are considered false in a condition?

The number zero, empty strings, empty lists, empty dictionaries, and None are considered false; everything else is considered true. Therefore, to check if a list is empty, instead of comparing its length to zero, writing the list directly in the condition is enough.

How are and and or combined?

and requires all conditions to be true, while or requires at least one of them to be true. In mixed usage, and is evaluated first; using parentheses is the safest way to prevent confusion. Furthermore, Python uses short-circuit evaluation: in an and chain, once the first false value is found, the rest are never evaluated.

Summary

Program flow branches with if, elif, and else; scope is determined by INDENTATION, not curly braces. The three most common errors: trying to compare with a single equals sign, forgetting the colon, and skipping the indentation.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code