The following 10 common mistakes made by Python beginners are not real coding errors. The code can still run, but it is not Pythonic because Python has its own language philosophy and is simple in code processing that traditional languages cannot match. sex and convenience.
Beginners often use the == comparison operator in if statements to determine whether an expression is true.
#This is a redundant way of writing. In fact, you can check the variable directly.
In many beginner tutorials, they will tell you that string concatenation uses the operator directly.
A more elegant way of writing is to use the f-string method and place the variables in {}. This method makes the code clear and easy to read, because you don’t need to target Non-string variables are forced to convert.
When operating files, the following code looks very readable. Open File, edit file, close file. But in the end you always have to manually call the close method to close the file.
In fact, python provides a built-in context manager. When operating a file, use the with keyword to operate the file, so that you do not need to handle the closing operation of the file yourself. After leaving The context front manager will automatically close the file for you.
Suppose you want to do some operations based on the list. Before operating, you need to determine whether the list is empty. Beginners usually The len method will be used to determine whether the length of the list is 0, and subsequent operations will be performed.
Experienced developers will tell you that a better way is to directly use the list itself to determine whether the list is empty
If you want to list and print all the elements in the dictionary, first you need to get the key of each element, and then get the corresponding value through the key value. Beginners usually treat the two steps separately.
A more convenient method is that the dictionary's items() method will return an iterable object consisting of (key, value) tuples,
Tuple is a fixed-length data structure in python. When novices assign the elements in the tuple to multiple different variables, they The usual approach is to assign values to variables one by one from the tuple
. A better approach is to use destructuring directly in one line of code to deconstruct the tuples. Groups are assigned to multiple different variables at the same time.
For novices, when using a for loop to iterate elements in a list, their usual approach is to get the list first length, and then get the value by the subscript index of the element.
In fact, a more professional approach is to directly iterate the list itself. The value of each iteration is the element in the list
Suppose you want to add the values in two lists. Usually, you will loop from index 0 to the end of the list, and add the list each time Add items to form a new list
Python provides a better method zip(), which recombines two list objects into a tuple that appears in pairs Element list
Before python3.8, when a variable acts on the if judgment statement, it must be assigned a value first Then process
The walrus operator of python3.8 allows your code to reduce duplication and look more concise. You can process variable assignment and if judgment in one line.
If you have a text list and you want to print it together with the subscript index, you can define a separate numeric variable for each Increment by 1 for each iteration itself
The above is the detailed content of Ten common mistakes Python beginners make. For more information, please follow other related articles on the PHP Chinese website!