Ten common mistakes Python beginners make

王林
Release: 2023-04-18 18:19:24
forward
1142 people have browsed it

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.

Ten common mistakes Python beginners make

1. Truth value comparison

Beginners often use the == comparison operator in if statements to determine whether an expression is true.

Ten common mistakes Python beginners make

#This is a redundant way of writing. In fact, you can check the variable directly.

Ten common mistakes Python beginners make

2. String concatenation

In many beginner tutorials, they will tell you that string concatenation uses the operator directly.

Ten common mistakes Python beginners make

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.

Ten common mistakes Python beginners make

3. open(), close() and context manager

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.

Ten common mistakes Python beginners make

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.

Ten common mistakes Python beginners make

4. Check whether the list is empty

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.

Ten common mistakes Python beginners make

Experienced developers will tell you that a better way is to directly use the list itself to determine whether the list is empty

Ten common mistakes Python beginners make

5. Get the key and value from the dictionary

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.

Ten common mistakes Python beginners make

A more convenient method is that the dictionary's items() method will return an iterable object consisting of (key, value) tuples,

Ten common mistakes Python beginners make

6. Destructuring assignment

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

Ten common mistakes Python beginners make

. 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.

Ten common mistakes Python beginners make

7. List iteration

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.

Ten common mistakes Python beginners make

In fact, a more professional approach is to directly iterate the list itself. The value of each iteration is the element in the list

Ten common mistakes Python beginners make

8. One index for multiple lists

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

Ten common mistakes Python beginners make

Python provides a better method zip(), which recombines two list objects into a tuple that appears in pairs Element list

Ten common mistakes Python beginners make

9. Walrus operator

Before python3.8, when a variable acts on the if judgment statement, it must be assigned a value first Then process

Ten common mistakes Python beginners make

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.

Ten common mistakes Python beginners make

10. Unnecessary indexes

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

Ten common mistakes Python beginners make

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!