Skip to content

Python Basic Syntax

Python's syntax is designed to be clear and concise. Understanding its basic syntax is the first step in learning Python. This chapter introduces the basic elements that make up a Python program.

Keywords (Keywords)

Keywords are words predefined and reserved by the Python interpreter. They have special meanings and cannot be used as variable names, function names, or any other identifiers.

You can view all Python's keywords with the following code:

python
import keyword
print(keyword.kwlist)

Common keywords include if, else, for, while, def, class, import, return, etc.

Identifiers (Identifiers)

Identifiers are names used to identify variables, functions, classes, modules, and other objects. Their naming rules are as follows:

  1. Can consist of letters (A-Z, a-z), numbers (0-9), and underscores (_).
  2. First character must be a letter or underscore, cannot be a digit.
  3. Case sensitive (e.g., myVar and myvar are two different identifiers).
  4. Cannot use Python keywords.

Valid identifier examples: my_variable, user_name, age, _internal_var, CarInvalid identifier examples: 2nd_var (starts with a digit), my-var (contains hyphen), class (is a keyword)

Indentation (Indentation)

This is one of Python's most unique syntax features. Python does not use curly braces {} to separate code blocks (like functions, loops, conditional statements), but instead enforces indentation.

  • All statements in the same code block must contain the same indentation amount.
  • Usually recommend 4 spaces as one indentation level.

Example:

python
if True:
    print("This is inside the if block.")  # Correct indentation
    print("This is also inside.")
else:
    print("This is inside the else block.")

print("This is outside the if/else block.")  # No indentation, doesn't belong to any block

Incorrect indentation leads to IndentationError, which is one of the most common errors encountered by beginners.

Statements (Statements)

Statements are instructions that make up a program. In Python, usually one statement per line.

python
name = "Alice"       # Assignment statement
print(name)         # Function call statement
import os           # Import statement

If a statement is too long, you can use backslash \ to continue on a new line:

python
total = 1 + 2 + 3 + \
        4 + 5 + 6

In data structures like lists, tuples, dictionaries, you can break lines directly without needing \ for better readability.

Comments (Comments)

Comments are text in code used for explanation and description. The interpreter ignores them. In Python, single-line comments start with the hash symbol #.

python
# This is a single-line comment
name = "Bob"  # This is also an inline comment

Multi-line comments can use three single quotes ''' or three double quotes """. These are actually multi-line strings, but are commonly used as comments.

python
'''
This is a multi-line comment,
it can span multiple lines.
'''
def my_function():
    """This is called a docstring (document string), used to explain the function's purpose."""
    pass

语句 (Statements)

Statements are instructions that make up a program. In Python, typically one statement per line.

python
name = "Alice"       # Assignment statement
print(name)         # Function call statement
import os           # Import statement

If a statement is too long, you can use backslash \ to continue on a new line:

python
total = 1 + 2 + 3 + \
        4 + 5 + 6

In data structures like lists, tuples, dictionaries, you can break lines directly without needing \.

Content is for learning and research only.