Frequently Asked Questions about Python Code Writing


Once you start writing Python code seriously, a bunch of pitfalls become even more dangerous - these are basic code errors that cross language features and often plague inattentive programmers.


Start with the first column

Make sure to put top-level, unnested code in Start with the first column on the far left. This includes code that is not nested in module files, and code that is not nested in the interactive prompt. Python uses indentation to distinguish between nested blocks of code, so spaces to the left of your code represent nested blocks of code. Except for indentation, whitespace is usually ignored.


Don’t write C code in Python

Here are some reminder tips for C programmers who are not familiar with Python:

  • When testing conditions in if and while, there is no need to enter parentheses (for example, if (X==1):). It's okay to add parentheses if you like, but they're completely redundant here.
  • Don’t end your statement with a semicolon. Technically this is legal in Python, but it's useless unless you want to put many statements on the same line (for example, x=1; y=2; z=3).
  • Do not embed assignment statements in conditional tests of while loops (for example, while ((x=next() != NULL)). In Python, expressions cannot be used where an expression is required statement occurs, and the assignment statement is not an expression.

Don’t forget the colon

This is the most common mistake that novice programmers make: don’t forget to place the colon in the starting statement of a compound statement (if, while, for, etc. Add a colon ":" at the end of the first line). You may forget about this at first, but soon it will become a subconscious habit. 75% of the students in the class can remember this on the same day.

Initializing variables

In Python, a name in an expression cannot be used until it is assigned a value. This is intentional: it avoids some typing errors and also avoids questions about what type the default should be (0, None, "", [], ?). Remember to initialize the counter to 0, the list to [], and so on.

Consistent indentation

Avoid mixing tabs and spaces for indentation in the same code block unless you know how the system running your code handles tabs of. Otherwise, indentations that look like tabs in your editor might look like spaces to Python. To be on the safe side, indent each code block with either all tabs or all spaces; it’s up to you to use more or less.

Use parentheses when calling functions

Whether a function requires parameters or not, you must add a pair of parentheses to call it. That is, use function(), not function. Python functions are simply objects with special functions (calls), and calls are triggered using parentheses. Like all objects, they can be assigned to variables and used indirectly: x=function:x().
In Python training, such errors often occur during file operations. It's common to see newbies use file.close to close an issue, rather than file.close(). Because it is legal in Python to reference a function without calling it, the operation without parentheses (file.close) succeeds silently, but does not close the file!

Do not use expressions or paths when importing

Use the folder path or file extension in the system command line, but do not use it in the import statement . That is, use import mod instead of import mod.py, or import dir/mod.py. In reality, this is probably the second biggest mistake beginners make. Because modules will have suffixes other than .py (for example, .pyc), forcing a certain suffix is ​​not only ungrammatical, but also meaningless.
The format of the system-related directory path comes from the setting of your module search path, not the import statement. You can use dots in file names to point to subdirectories of the package (e.g., import dir1.dir2.mod), but the leftmost directory must be found via the module search path, and there is no other path format in import. The incorrect statement import mod.py is considered by Python to be recorded in a package. It first loads a module mod, and then tries to find the module called py in a directory called mod. In the end, it may not find anything. A series of confusing error messages are reported.