Summary of several mistakes easily made by beginners of Python

Source: Internet
Author: User
Tags finally block
There are some small pitfalls in the python language, which are especially easy to confuse and make mistakes. if beginners do not pay attention to them, it is easy to get in. below I will give you an in-depth analysis of these pitfalls, I hope it will be helpful for beginners. if you need it, you can refer to learning. let's take a look at it. There are some small pitfalls in the python language, which are especially easy to confuse and make mistakes. if beginners do not pay attention to them, it is easy to get in. below I will give you an in-depth analysis of these pitfalls, I hope it will be helpful for beginners. if you need it, you can refer to learning. let's take a look at it.

Preface

This article mainly summarizes several mistakes that beginners are easy to make when learning Python. a total of four mistakes are easy to make. let's take a look at the details below.

I. I + = 1 is not equal to ++ I

If you are not familiar with the Python language, you can easily mix ++ I and I ++ = 1.

Let's take a look at a small example:


I = 0 mylist = [1, 2, 3, 4, 5, 6] while I
 
  

This code will take it for granted that there is no problem. it is a loop output, and I is always + 1, which is quite correct. actually not. this code will always output 1, an endless loop. because the Python interpreter performs the ++ I operation+(+i) . + Indicates a positive number, which is similar to -- I.


print(+1)>>>1print(++1)>>>1print(+++1)>>>1

Now we understand that ++ I is legal in Python, but it is not an auto-increment operation we understand.

II. usage of clear = and is

When determining whether a string is equal, beginners will especially confuse is and =. The result is that the program performs differently under different circumstances:

For example, let's look at a simple example:


A = 'hi' B = 'hi' print (a is B) >>> Trueprint (a = B) >>>> True # it looks like is and = are the same

Let's look at the second example:


Str1 = 'Wo shi yi ge chi huo 'str2 = 'Wo shi yi ge chi huo 'print (str1 is str2) >>> false # the result of is Falseprint (str1 = str2) >>> True #== the result is True. The two are different.

Example 3


Str3 = 'string' str4 = ''. join (['S', 'T', 'R', 'I', 'n', 'G']) print (str3) >>> stringprint (str3 is str4) >>> False # the result of is Falseprint (str3 = str4) >>>> True #= = the result is True. check whether the two are different.

This is the place where beginners are easily confused. it is strange that sometimes the is and = output are the same, and sometimes they are different. let's take a look:

We use the built-inid()This function is used to return the memory address of the object. it will be clear after checking.

str1,str2,str3='test','string','connection'print(str1+str2+str3)>>>test string connection

Join strings


str1,str2,str3='test ','string ','connection'print(''.join([str1,str2,str3]))>>>test string connection

However, if a large string is connected, for example, to connect a string of about 0.1 million, the join method will be much more efficient (or even a hundred times different). For example, the following 0.1 million string connections.


long_str_list=['This is a long string' for n in range(1,100000)]

The reason is that to connect to the string: S1 + S2 + S3 + .... + SN, because the string is an immutable object, to execute a request to apply for a new memory, so that in the process of N string connection, will generate an intermediate result of the N-1, memory is applied every time an intermediate result is generated, which seriously affects execution efficiency.

The join operation is different. it applies for the total memory at a time and copies each element in the string to the memory. Therefore, the join operation is much faster.

Therefore, it is best to use join for string connection, especially for handling large strings.

4. do not write else blocks after the for and while loops

Python provides a function not supported by many programming languages, that is, you can directly write else blocks after the statement blocks in 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
  • This else block runs immediately after the entire loop is executed. In this case, why is it called else? Why is it not "and? In the if/else statement, else indicates that if the previous if block is not executed, the else block is executed.

  • The same applies to try/try T/else. the else of this structure means that if the previous try block does not fail, the else block will be executed.

  • Try/finally is also very intuitive. here finally means that after the previous try block is executed, the finally block is always executed anyway.

The problem is that programmers who are new to Python may understand the else block in the for/else structure as follows: if the loop is not fully executed, execute the else block.

In fact, the opposite is true-if you use the break statement in the loop to jump out in advance, the program will not execute the else block, which will be a bit difficult. for those who are not familiar with for/else, it may be confusing to read the code.

Summary

The above is a summary of several mistakes that are easy to make by new Python beginners. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.