Ad space (banner)
🐍Python Lessons
Lesson 3 / 69

Conditionals (if statements)

This lesson covers how to write if statements in Python, including the language's distinctive rule of using indentation to structure blocks of code. It's written for anyone searching "Python if statement syntax" or "Python indentation rules".

Python's if statement uses a colon (:) followed by indentation to mark a block of code — a major difference from languages that use curly braces {}. The depth of indentation determines what belongs "inside" the if block, a rule unique to Python. This forced structure is exactly why Python code tends to look consistent and readable no matter who wrote it.

The sample code prints one of three messages depending on the value of score, using if / elif / else. elif is Python's version of else if in other languages — conditions are evaluated top to bottom, and only the first block whose condition is True runs. Getting the indentation depth wrong by even one level changes the meaning of the code.

A common beginner mistake is mixing spaces and tabs in indentation, which causes errors — Python is strict about indentation consistency, so it's worth configuring your editor to convert tabs to spaces automatically. Forgetting the trailing : is another frequent slip.

In real development, if statements are used everywhere — switching what's displayed based on login state, validating user input, and more. Handling indentation correctly is a foundational skill for writing any Python code at all.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)