No history yet

Python Syntax

The Rules of Python

Python's syntax is famously clean and readable. The language was designed with a core philosophy that prioritizes developer experience and code clarity over raw machine performance. A key idea is that code is read far more often than it is written. Therefore, making it easy to read is paramount.

Python's design philosophy emphasizes code readability, allowing developers to express concepts in fewer lines of code than in other languages.

This focus on readability means Python's rules feel more like natural language and less like cryptic instructions for a computer. Three key aspects of its syntax stand out: indentation, whitespace, and the use of simple English keywords.

Indentation Defines Structure

In many programming languages, you use curly braces {} or keywords like begin and end to group lines of code into blocks. Python takes a different approach. It uses indentation—the spaces at the beginning of a line—to define the structure and grouping of your code.

A block of code starts with an increase in indentation and ends when the indentation decreases. Think of it like a formal outline, where sub-points are indented under a main point. This isn't just a style suggestion; it's a strict rule the interpreter enforces. Every line within a specific block must be indented by the same amount.

# In Python, indentation creates a code block.
# The standard is to use 4 spaces per indentation level.

access_level = 'admin'

if access_level == 'admin':
    # This block is executed only if the condition is true
    print('Welcome, administrator.')
    print('You have full access.')
else:
    # This block is executed if the condition is false
    print('Welcome, user.')
    print('Your access is limited.')

# This line is not indented, so it is outside the if/else blocks
print('Session complete.')

Incorrect indentation is one of the most common errors for new Python programmers. It will cause an IndentationError and prevent your code from running.

While indentation is rigid, other uses of whitespace are for readability. For example, adding spaces around operators like + or = doesn't change how the code works, but it makes it much easier for a human to read. The line x = 5 is functionally identical to x=5, but the first version is preferred.

Speaking Plain English

Python's readability is also enhanced by its use of simple, English-like keywords. Instead of using symbols like && for "and" or || for "or", Python just uses the words and and or. This makes conditional logic read almost like a plain sentence.

# Using English keywords for logic and loops

# Example 1: Logical operators
authorized = True
active = False

if authorized and not active:
    print('Account is authorized but not active.')

# Example 2: Checking for membership in a list
users = ['alice', 'bob', 'charlie']

if 'bob' in users:
    print('Bob is a valid user.')

# Example 3: A 'for' loop
for user in users:
    print(f'Processing user: {user}')

Keywords like if, for, in, and, or, and not make the code's intent clear without requiring the programmer to remember a set of symbolic operators. This simple, direct syntax is a core reason why Python is often recommended for beginners and used for rapid development.

Lesson image

Ready to test what you've learned about Python's syntax?

Quiz Questions 1/5

What is the primary philosophy behind Python's syntax design?

Quiz Questions 2/5

In Python, how are blocks of code (like the body of a function or a loop) defined?

By following these simple rules of indentation and using its clear keywords, you can write Python code that is both powerful and easy to understand.