Home >Backend Development >Python Tutorial >Summary of several mistakes that Python novices often make
There are some small pitfalls in the python language, which are particularly easy to confuse and make mistakes. Beginners can easily fall into pitfalls if they are not careful. Below I will give you an in-depth analysis of some of these pitfalls. I hope it will be helpful to beginners. , friends in need can refer to it, let’s take a look below.
Preface
This article mainly summarizes several common mistakes that novices who learn Python make. There are four mistakes in total. Make a mistake, let’s take a look at the detailed introduction below.
1. i+=1 is not equal to ++i
If beginners don’t know much about Python language, and they happen to have c++, java With the language background, it is easy to confuse ++i and i+=1
Let’s look at a small example first:
i=0 mylist=[1,2,3,4,5,6] while i <len(mylist): print(mylist[i]) ++i
This code will take it for granted that there is no problem. It is a loop output, i keeps +1, which is quite right. In fact, it is not, this code will always output 1, Infinite loop . Because the Python interpreter will operate ++i as +(+i)
. + represents a positive sign, and the same is true for --i.
print(+1) >>>1 print(++1) >>>1 print(+++1) >>>1
Now I understand that although ++i is legal in Python syntax, it is not an operation of incrementing as we understand it.
2. Clearly distinguish the usage of == and is
When judging whether strings are equal, beginners especially will confuse is and ==, resulting in such a result The program behaves differently under different circumstances:
For example, let’s look at a simple example:
a='Hi' b='Hi' print(a is b) >>>True print(a==b) >>>True #看起来is和==好像是一样的
We Look at the second example:
str1='Wo shi yi ge chi huo' str2='Wo shi yi ge chi huo' print(str1 is str2) >>>False#is的结果是False print(str1==str2) >>>True #==的结果为True,看二者不一样了吧
The third example
str3='string' str4=''.join(['s','t','r','i','n','g']) print(str3) >>>string print(str3 is str4) >>>False #is的结果是False print(str3==str4) >>>True #==的结果为True,看二者不一样了吧
This is where it is easy to confuse beginners. It feels very strange. Why are sometimes the output of is and == are the same, and sometimes they are different. Let’s find out:
We use the built-in id()
This function is used to return the memory address of the object. If you check it, it will be clear.
is is the identifier of the object. Use To compare whether the memory spaces of two objects are the same and whether they use the same space address, and == is to compare the contents of two objects Equal.
3. When connecting strings, especially large-scale strings, it is best to use join rather than +
string processing. At times, the most commonly used one is connection. Strings in Python are a little different from other languages. They are immutable objects and cannot be changed once created. This feature will directly affect the efficiency of string connection in Python.
Use + to connect strings:
str1,str2,str3='test','string','connection' print(str1+str2+str3) >>>test string connection
Use join to connect strings
str1,str2,str3='test ','string ','connection' print(''.join([str1,str2,str3])) >>>test string connectionBut if you are connecting large-scale strings, for example, when you want to connect about 100,000 strings, the join method will be much faster (even a hundred times different). For example The following 100,000 string connections.
long_str_list=['This is a long string' for n in range(1,100000)]The reason is because you want to connect strings: S1+S2+S3+....+SN, because the string is For an immutable object, a new memory must be applied for once it is executed. In this case, during the process of connecting N strings, N-1 intermediate results will be generated. Each time an intermediate result is generated, a memory must be applied for. This will be serious. Affects execution efficiency.But join is different. It applies for the total memory at one time, and then copies each element in the string to the memory, so join will be much faster.Therefore, for string connection,
especially for large string processing, it is best to use join
4. Do not write else blocks after for and while loops
Python provides a feature that many programming languages do not support, that is, you can write an else block directly after the statement block inside the loop. For example:for i in range(3): print('Loop %d'%i) else: print('Else block') >>>Loop 0 >>>Loop 1 >>>Loop 2 >>>Else block
immediately after the entire loop is executed. If so, why is it called else? Why not call it and? In the if/else statement, else means: if the previous if block is not executed, then the else block is executed.
. In fact, just the opposite - Using the break statement in the loop to jump out early will cause the program not to execute the else block , which is a bit confusing. For those who are not familiar with for/else, it will It is quite confusing for people who read the code. Summary
The above is the detailed content of Summary of several mistakes that Python novices often make. For more information, please follow other related articles on the PHP Chinese website!