Python's expanded knowledge of + = and =

Source: Internet
Author: User

There are two points on the internet, and I want to say something.

1th

Online has always said num = num + num and num + = num is equivalent, here I want to say, these two only when NUM is a numeric value is equivalent, if not numeric,num = num + num is the value of NUM Plus num is assigned to NUM, which One is to create a new space for Num, and num+=num is the result of modifying Num's original value to num + num . Maybe you'll be a little stunned, okay, let's look at the example below

A = [100]def Zhouyu (num): num = num + num print (num) Zhouyu (a) print (a)

With the above understanding, we know that num = num + num is actually assigning the result of [100] plus [100] to NUM, and then Num will go back to that result and say that if the value of NUM is [100], then the value of NUM is now [100,100] , while A is still [100].

Here's another one.

A = [100]def Zhouyu (num): num + = num print (num) Zhouyu (a) print (a)

Because Num +=num actually modifies the value of NUM itself, it modifies the value of NUM and the value of a is changed.

2nd.

Compare the results of the following two paragraphs of code

A = 100def Zhouyu (num): num +=num print (num) Zhouyu (a) print (a)

Why is the result of execution

200

100

Because A is an integer type, except the list and the dictionary is basically immutable, Zhouyu (a) is passed a, so Num will point to a, because a=100 is an integer type, is immutable, so here will go to create a new a,a=100+100, so A is 200, Print out is 200, and the outside of a is that a so, it is still 100. If you don't understand it, see below.

A = [100]def Zhouyu (num): num +=num print (num) Zhouyu (a) print (a)

Why is the result of execution the following result?

[100,100]

[100,100]

Because Zhouyu (a) is passing in a, num points to a, where a is a list type and can be changed. So it will change the above, and the print will change. That is, when the parameter is passed to see if a is mutable, if it is mutable will change the value of the outside, if not mutable, it is a new creation of a, this time the value of a and function outside the value is not the same


This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1967627

Python's expanded knowledge of + = and =

Related Article

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.