A detailed description of the Python variable and the assigned value

Source: Internet
Author: User
This time for you to bring the Python variable and the assignment of the text, the use of Python variables and assignment of attention to what, the following is the actual case, together to see.

Python is a unique language, and C language is very different, beginners python a lot of Meng new expression on variables and assignments do not understand, learned C know that when assigning a variable, you need to specify the data type, but also open up a memory area, for storing values, for example:

int a = 1;

A is a small area in the memory space, like a small box inside a large room, the assignment is to load the whole number 1 into the box.

The variable A is now re-assigned to a value

A = 2;

The box is still the box, that is, the memory address does not change, but the memory value of the segment has changed to become 2.

See again:

int b = A;

When a variable A is assigned to another variable B, it is equivalent to copying a copy of the value to the variable b,b is a newly opened memory area

In Python, the "variable" is strictly called "name", also can be understood as the label, just like our name, the name is a tag hanging on the person.

>>> 10-atraceback (most recent): File "<stdin>", line 1, in <module>nameerror:name ' a ' is Not defined

The above error, for example, says that name ' a ' is not defined, and is not variable.

In Python, assigning a variable is equivalent to labeling an object, as if we were giving a name, the variable itself is meaningless, it has no type information, and the real information is on the object.

For example:

A = 1

Python will first allocate a memory space to create an integer object 1, and then give this 1 a label named A.

And then execute


Python then creates an integer object 2 in another area of memory, and then rips the label a from 1 to the 2, where we can no longer get the 1 value by a.

Now assign the name A to another name, B.

b = A

The equivalent of a new label in the 2 just now labeled B, note that here and C language is completely different, the definition of B does not need to allocate memory space, so that we can access through a to 2 can also access through B to 2, access is the same object, as we give the baby name when there are both nicknames and name, In fact, the same people are called.

Although we usually use the term "variable" in Python (because this is a common term in programming languages), we need to understand that variables in Python are different from other languages, and variables are just names.

After understanding the variables and assignments in Python, take a look at the parameter passing of the function as follows:

>>> def fun_a (a): ...   A = a+4...>>> g = 0>>> fun_a (g) >>> G0

When the global variable G is passed to the function fun_a, the equivalent of the parameter A in the function is also attached as a label on 0, then a is re-assigned (A=A+4), which is equivalent to removing the label a from 0 to 4, and then G is still the label on the 0.

Let's look at this function, pass a list object

>>> def fun_b (names): ...   Names[0] = [' x ', ' y ']...>>> n_list = [' A ', ' B ', ' C ']>>> fun_b (n_list) >>>>>> n_list[[ ' x ', ' y '], ' B ', ' C ']

The same as the previous steps, names and n_list are [' A ', ' B ', ' C '] a label, but the list of the No. 0 element is re-assigned, but names and N_list are still posted on this list object, although the value of n_list updated, But the object is still the original object. Just like Zhang San and Xiao Zhang are the same person, now to small Zhang change clothes, in fact, is to give Zhang San change clothes, people or that person, but it has changed things.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

How Python strings are converted to two-dimensional arrays

Event Emitter Listener Events

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.