Detailed graphic explanation of Python variables and assignments

php中世界最好的语言
Release: 2018-04-09 09:54:18
Original
2462 people have browsed it

This time I will bring you a detailed graphic explanation of Python variables and assignments. What are the precautions when using Python variables and assignments? Here are practical cases, let’s take a look.

Python is a unique language, which is very different from the C language. Many newbies who are new to Python say they don’t understand variables and assignment. Anyone who has studied C knows that when assigning a value to a variable, you need to specify it first. Data type, at the same time, a memory area will be opened for storing values, for example:

int a = 1;
Copy after login

Detailed graphic explanation of Python variables and assignments

a is a small area in the memory space, It's like a small box in a large room. Assignment is to load the integer 1 into the box.

Now reassign the value of variable a

a = 2;
Copy after login

Detailed graphic explanation of Python variables and assignments2

The box is still the same box, which means that the memory address has not changed, but the value in the memory has changed. Yes, it became 2.

Let’s look at it again:

int b = a;
Copy after login

Detailed graphic explanation of Python variables and assignments3

When assigning variable a to another variable b, it is equivalent to copying the value and passing it to variable b. b is a newly opened memory area

In Python, the strict name of "variable" is "name (name)", which can also be understood as a label, just like our people's names, the name is A label attached to a person.

>>> 10-a
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined</module></stdin>
Copy after login

For example, the error reported above is that name 'a' is not defined, but is not called variable.

In Python, assigning a value to a variable is equivalent to labeling an object, just like we give names to people. The variable itself has no meaning. It has no type information, and the real information is in the object.

For example:

a = 1
Copy after login

Detailed graphic explanation of Python variables and assignments

Python will first allocate a memory space to create an integer object 1, and then label this 1 with the name a .

Then execute

a=2
Copy after login

Python will then create the integer object 2 in another memory area, and then tear off the label a from 1 and paste it on 2. At this time, we can no longer pass a to get the value 1.

Detailed graphic explanation of Python variables and assignments

Now assign the name a to another name b

b = a
Copy after login

Detailed graphic explanation of Python variables and assignments

It is equivalent to attaching it to the 2 just now A new label b. Note that this is completely different from the C language. There is no need to allocate memory space when defining b. In this way, we can access 2 through a or 2 through b. The accesses are the same. An object is just like when we name a baby with both a nickname and a big name, they actually name the same person.

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 In other languages, a variable is just a name.

After understanding variables and assignments in Python, let’s look at function parameterspassing, as shown below:

>>> def fun_a(a):
...   a = a+4
...
>>> g = 0
>>> fun_a(g)
>>> g
0
Copy after login

When the global variable g is passed to the function fun_a, it is quite The parameter a in the function will also be attached to 0 as a label, and then a will be reassigned (a=a 4), which is equivalent to tearing off the label a from 0 and attaching it to 4, and then g will still be the label on 0.

Detailed graphic explanation of Python variables and assignments

Let’s look at this function again. What is passed is 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']
Copy after login

与前面的步骤还是一样的,names 和 n_list 都是['a','b','c']上的一个标签,只是列表中的第0个元素被重新赋值了,但是names和 n_list 依然都贴在这个列表对象身上,虽然 n_list的值更新了,但对象依然是原来那个对象。就好比张三和小张都是同一个人,现在给小张换件衣服时,其实就是给张三换件衣服,人还是那个人,只是它身上的东西发生了变化。

Detailed graphic explanation of Python variables and assignments

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

python字符串如何转为二维数组

Event emitter的监听事件

The above is the detailed content of Detailed graphic explanation of Python variables and assignments. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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!