title: Python basic syntax tutorial description: Python is a high-level, interpreted, general-purpose programming language with the following characteristics:
Introduction to Python Basic Syntax (2024 Best Practice Edition)
1. Get to know Python in five minutes: Why is it worth learning?
Python is a high-level, interpreted, general-purpose programming language. It was born in 1991 and now ranks among the top three in the TIOBE programming language rankings. Some people call it the "glue language", while others call it the "first language of artificial intelligence" - these are not exaggerations.
Summarize its advantages in human terms:
- The syntax is like writing pseudocode: There is no need to type a semicolon after each line, and there is no need to fight with curly braces. Indentation is logic, and it reads close to natural language.
- Comes with a powerful "Swiss Army Knife Standard Library": processing files, sending emails, parsing JSON, writing websites... many functions can be used directly after installing Python.
- Extremely rich ecosystem: From web backends (django, FastAPI), data science (NumPy, Pandas), to machine learning (PyTorch, TensorFlow), you can almost always find ready-made wheels on PyPI.
- Cross-platform, no compilation: Can run on Windows/macOS/Linux, execute the script on the spot after writing, and debugging feedback is extremely fast.
If you want to "quickly get started with a programming language that can solve practical problems", Python will be the most comfortable starting point.
2. Modern Python syntax basics: start with the soul design of “indentation”
2.1 The most unique design: forced indentation
Students who have studied C, Java or JavaScript are accustomed to using curly braces{}Frame code blocks. Python does it completely differently: it uses indentation to indicate the level of code blocks. This means that indentation is not a "recommendation", but part of the syntax - if you write the wrong indentation, the program will directly report an error (IndentationError)。
Let’s look at a simple example:
In this function,ifandelseThe following codes are all indented by 4 spaces. One more space, one less space, or accidentally mixing in the Tab character may cause the program to show a red light. But it is precisely because of this mandatory specification that Python code is naturally neat and tidy. The code styles written by different people tend to be consistent, and the reading experience is very unified.
💡 Tips The industry gold standard for indent width is 4 spaces (as specified by the official Python style guide PEP 8). All modern editors can automatically convert the Tab key to 4 spaces, once and for all.
2.2 Three-minute quick overview: basic grammar points + best practices
✅ Indentation rules (remember this one)
- Use 4 spaces uniformly, do not mix tabs.
- Editors such as VS Code and PyCharm all support "automatically convert to spaces when entering Tab", which can be configured once.
- If you take over the old code and find that the indentation is messy, just use the formatting tool (will be introduced later).
✅ Comments and documentation: let the code speak
Python provides a hierarchical annotation system:
- Single line comments:
# 这里是注释内容, can be placed above the code or at the end of the code line (note to keep the distance, at least two spaces). - Multi-line documentation string (Docstring): use triple quotes
"""or'''Surroundings are usually written at the beginning of functions, classes, and modules to describe functions. This isn't just for people to see - you can use it in the terminalhelp(print_absolute)View it and modern IDEs will automatically grab it and display it in a tooltip too. - Type Hints (Python 3.5+): For example
num: intand-> None. Although Python does not force type checking when running, the editor (especially VS Code + Pylance) can prompt errors in real time, greatly reducing the tragedy of crashing as soon as the code is run.
✅ Case sensitive + keywords must be all lowercase
age、Age、AGEThey are three completely different variables, don't confuse them.- All keywords of Python (
if、else、def、class、import...) are all lowercase, and keywords cannot be used as variable names. - Variable naming recommendations
snake_caseUnderline style (e.g.user_name), which is also from PEP 8.
3. Tool chain evolution history: Say goodbye to the indentation nightmare completely
The pain of learning Python in the early years was most likely "the indentation was messed up after copying and pasting the code", or "the mixed use of tabs and spaces resulted in an error that was invisible to the naked eye." The current tool chain can completely take care of it for you:
You can memorize it like this: **The editor helps you troubleshoot while writing; Black helps you perform beauty treatments with one click after finishing work; Pre-commit helps you maintain the last line of defense before the code enters the warehouse. **
4. Common errors reported by novices & solutions
Most frequently reported error:IndentationError: expected an indented block
This error usually looks like this:
🔧 Three-step quick fix
-
One-click formatting
- VS Code:
Shift + Alt + F - PyCharm:
Ctrl + Alt + L
Formatting can automatically correct most indentation problems.
- VS Code:
-
Quick self-check using command line Execute in terminal:
If there is no output, it means that the syntax and indentation are OK; if there is an error, it will tell you exactly which line the problem is.
- Check whether tabs and spaces are mixed
This command will issue a warning if tabs and spaces are mixed in the code. Turn on the display of whitespace characters in the editor (VS Code: MenuView → Render Whitespace), you can see the problem more intuitively.
5. “Three things to do and two things not to do” list for beginners
✅ stick to 4 spaces indentation ✅ Python 3.6+ projects should try to add type hints + concise Docstring ✅ Configure automatic formatting tool (Black) and Pre-commit ❌ Never mix Tab and 4 spaces ❌ Don’t ignore the PEP 8 specification (even a temporary small script will be easier to debug after formatting)
📌 The ultimate lazy trick If you open a file and find that the indentation is all messed up, VS Code users press
Ctrl+Shift+P,enterConvert Indentation to Spaces, press Enter - the world becomes instantly clear.
Embracing these modern tools and specifications from the start will make Python’s learning curve a hundred times smoother than it was a decade ago. I wish you happy coding, avoid pitfalls, and write more good code that works!

