Table of Contents

What Will You Learn
In this material, you will get acquainted with the basic syntax of Python — from indentation and variables to control structures. The lesson helps you understand how to write clean and readable Python code, even if you're just getting started.


Python syntax is considered one of the most readable and easiest to learn. But that doesn’t mean it can be ignored. On the contrary — the faster you understand how command structures are built, the easier your learning journey will be.

Basic Principles of Syntax

In the early stages of learning any programming language, it's important not just to memorize commands but to understand how they should be structured. Python syntax is designed to make code look clean, readable, and logical. This is achieved through strict, yet intuitive formatting rules.

Most languages use curly braces to define code blocks. In Python, instead, indentation is used to define structure. This means the entire logical block must be visually separated by spaces from the outer level. If you're writing an if statement, everything belonging to it must be indented to the right. This is not just a matter of style — it's a strict requirement. If the indentation is off, you’ll get an error.

Besides indentation, Python avoids redundancy. You don’t need to put a semicolon at the end of each line, unlike in JavaScript or C++. You simply write one instruction per line. If you really need to write two instructions on one line, you can separate them with a semicolon — but this is rare and discouraged.

Pay special attention to case sensitivity. The language distinguishes variables, functions, and classes by case. So total, Total, and TOTAL are three different identifiers. Case errors are one of the most common beginner mistakes.

It's also important to understand how comments work. A single # at the beginning of a line or after a command tells the interpreter that the rest is an explanation and should not be executed. Comments do not affect the execution of the program, but they make your code more understandable for yourself and others.

Python supports different ways to define strings: single or double quotes. They are interchangeable, but you should use the opposite type inside the string if you include a quote. For example: 'He said "hello"' or "It's working".

Code is read from top to bottom, with no exceptions. So the order of variables, functions, and imports matters. All external libraries should be imported at the beginning of the file using the import keyword. Your file should follow a logical structure: imports first, then global variables, followed by functions, and finally the main logic.

Finally, variable names should be meaningful, start with a letter (not a number), contain no spaces, and not match reserved keywords. This is not only a syntax rule, but also key to writing readable programs.

Below are key syntax features to remember:

  • Indentation is mandatory. Indentation defines the structure of the code. Typically, 4 spaces are used.
  • No semicolon is needed. Do not put it at the end of a line. Exception: multiple statements in one line.
  • Case sensitivity. print, Print, and PRINT are all different identifiers.
  • Comments start with #. Everything after # is ignored by the interpreter.
  • Strings can use single or double quotes.
  • Code is read from top to bottom. And executed in the same order.
  • Library imports must be at the top of the file.
  • Variable names can’t start with numbers or contain spaces.

Knowing these basic rules will save you dozens of hours in the future. Syntax errors may be simple, but they block your entire program from running. So the sooner you develop the habit of writing clean code, the easier it will be to move on to more complex topics.

Table: Syntax Elements and Their Meaning

Element Purpose Example
# Comment # This is a comment
: Start of a block (condition, loop, function) if x > 0:
" or ' String definition "Hello" or 'Hello'
def Function definition def my_func():
for / while Loops for i in range(10):
if / else / elif Conditions if x == 5:
= Assignment x = 10
import Module import import math

What Makes Python Syntax Different from Most Other Languages?

In most traditional languages like C, Java, or JavaScript, code blocks are defined using curly braces. When writing a condition or loop, you must open with { and close with }. In Python, this is not required. Instead of braces, a colon and mandatory indentation are used. As a result, the structure of the program is immediately visible without extra symbols.

The second difference is the absence of a semicolon at the end of the line. While in JavaScript or C every command ends with a ;, this is not the case in Python. A line simply ends with a newline character. This reduces the number of syntax errors, especially for beginners.

Additionally, in most other languages, variables must be declared in advance with a specified type — for example, stating that it's an integer or a string. In Python, it's enough to assign a value, and the type is determined automatically. This makes the code shorter and easier to write.

Python is also much stricter about indentation. In other languages, it can be ignored — it only affects readability. In Python, indentation is part of the syntax. A single extra space or incorrect indent will cause an error.

Finally, Python's syntax is designed with readability in mind. Even if you're seeing code for the first time, there's a high chance you'll understand what it does. Because of this, Python is often chosen for education, automation, and rapid prototyping.

As a result, compared to other languages, Python requires less "boilerplate code," removes unnecessary elements, and encourages writing simple and clear code from the very beginning.

Example of Basic Syntax

Here are Python syntax example:


# This is a comment
print("Hello, world!")  # Print a string to the console

# Variables
name = "Alice"
age = 30

# Conditional statements
if age > 18:
    print("Adult")
else:
    print("Minor")

# Loops
for i in range(5):
    print(i)

# Functions
def greet(user):
    return f"Hello, {user}"

print(greet("Bob"))

Each element in this code is a syntactically precise block. Even a single extra space can cause an error.

Common Beginner Mistakes

When starting to learn a programming language, most errors are not due to logic but to violations of basic syntax. These are not bugs but typical typos, inaccuracies, and misunderstandings that hinder the execution of even the simplest scripts. Below is a list of common mistakes encountered by many beginners.

  • Missing or extra indentation. If there is no indentation after lines with if, for, while, def, or class, the interpreter will immediately raise an error. Even worse is inconsistent indentation: 4 spaces in one place and a tab in another. Python strictly checks each character at the beginning of a line. Use either only spaces or only tabs (preferably spaces).
  • Mixing quotation marks. An error occurs when a string starts with a double quote " and ends with a single quote ', or vice versa. The interpreter doesn't understand where the string ends and throws a SyntaxError. Always open and close strings with the same type of quotation marks.
  • Assignment instead of comparison. Beginners often confuse = (assignment) with == (equality check). For example:
if x = 5: # error

This line will raise an exception. Always use a double equals sign == for conditions.

  • Using undefined variables. If a variable is used before it has been assigned a value, the code will not execute. The variable name must match exactly as it was declared. The program is case-sensitive and sensitive to extra characters and spaces.
  • Forgotten colons. After constructs like if, for, while, def, and class, there should always be a colon :. Its absence leads to a syntax error.
if age > 18 # error — missing colon
  • Violation of block structure. When one block is nested within another (e.g., a condition inside a loop), it's essential to maintain consistent indentation levels. Misplacing a line at the wrong depth will cause an IndentationError or disrupt the logic.
  • Attempting to use reserved words as variable names. For example, you cannot create a variable named class, def, for, or import. These words are reserved by the language. Using them will lead to an error during compilation.
  • Missing parentheses when calling functions. If you write print without (), Python won't understand your intent. In modern versions of the language, all function calls require parentheses.
print "Hello" # error

These mistakes may seem minor, but they significantly hinder progress. Beginners often spend hours trying to understand why their code "won't run," only to find the issue is a single space or character.

The recommendation is simple: type code manually, carefully, without copying and pasting. Use editors with syntax highlighting and automatic indentation checking (such as VS Code). Don't be afraid of errors—they are not signs of failure but tools for learning.

What Should a Beginner Programmer Remember?

To confidently use the language, it's essential to clearly understand its structure. A beginner programmer should master the following aspects:

  • Respect indentation. Execution of the program depends on it.
  • Write concisely, readably, and consistently.
  • Avoid "magic" values—use variables with meaningful names.
  • Adhere to PEP 8—the official style guide.
  • Test each line, especially in the early stages.

Frequently Asked Questions About Syntax

Where can I check Python syntax correctness?

You can check Python syntax using various tools and environments. Here are the most accessible options:

  • Online python syntax checker: Platforms like Online IDE or Pythonium allow you to write and run Python code directly in your browser with syntax validation.
  • Visual Studio Code: With the Python extension installed, VS Code highlights syntax errors in real time and offers suggestions to fix them.
  • Command line: Use python -m py_compile your_file.py to check for syntax errors without running the file.
  • Linters: Tools like flake8, pylint, and black help identify syntax issues and formatting problems automatically.

If you're a beginner, start with an online compiler or Visual Studio Code — they’re user-friendly and provide immediate feedback.

Why is Python so strict about indentation?

Indentation is not just visual formatting but a structural element. The language doesn't use curly braces like others but defines code blocks through indentation. This simplifies reading but requires discipline. A single misplaced space can prevent the program from running. This approach encourages writing clean and structured code from the start.

How many spaces should be used for one level of indentation?

It is recommended to use 4 spaces. Not tabs, not 2 spaces, not a mixed approach. This standard is specified in PEP 8. Most IDEs and editors automatically insert the correct indentation if configured properly. It's better to disable tabs altogether and use only spaces.

Can you write multiple commands on one line?

Technically yes, but it's not recommended. Commands can be separated by a semicolon:

x = 1; y = 2; print(x + y)

However, this style reduces readability and goes against best practices. It's better to write one instruction per line — it makes the code easier to debug and maintain.

What should I do if the code looks correct but still throws an error?

Check the following:

  • Are there any mixed indentations (spaces + tabs)?
  • Are all colons placed correctly after if, for, def, and other constructs?
  • Did you forget parentheses when calling functions?
  • Did you confuse = (assignment) with == (comparison)?
  • Are variable names spelled correctly (no typos and correct case)?

Even one of these issues can stop your program from running. Use linters and editor hints to catch such mistakes quickly.

Why do examples use print()? Is it required?

Yes, it is required. In Python versions 3.x and above, print is a proper function and must be called with parentheses. The syntax print "Hello" was only valid in Python 2, which is now outdated and no longer used in education or development. Always use print() — it’s the correct and current standard.