Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Python variable and assignment

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the method of Python variable and assignment". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python is a unique language, which is very different from the C language. Many newcomers to Python show that they do not understand variables and assignments. Anyone who has studied C knows that when assigning a value to a variable, you need to specify the data type first and open up a memory area to store values, for example:

Int a = 1

An is a small area of memory space, like a small box in a large room. The assignment is to load the integer 1 into the box.

Now reassign the variable a

A = 2

The box is still the same box, which means that the memory address has not changed, but the value in this section of memory has changed to 2.

Let's take a look at:

Int b = a

When you assign the variable a to another variable b, it is equivalent to passing a copy of the value to the variable b, which is a new memory area.

In Python, the strict name of "variable" is "name", which can also be understood as a label, just like our name, a name is a label hung on a person's body.

> 10-aTraceback (most recent call last): File "", line 1, in NameError: name 'a'is not defined

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

In Python, assigning a value to a variable is equivalent to tagging an object, just like we give a person 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 portion of memory space to create an integer object 1, and then label that 1 with the name a.

And then execute

Axi2

Python then creates an integer object 2 in another area of memory, and then rips the label an off 1 and pastes it on 2. At this point, we can no longer get the value of 1 through a.

Now assign the name a to another name b

B = a

It is equivalent to putting a new label b on the 2 just now. Note that this is completely different from the C language. There is no need to allocate memory space when defining b, so we can access 2 through an or 2 through B. they all visit the same object, just as we have both nicknames and big names for our babies, but they are all called by the same person.

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

After understanding the variables and assignments in Python, let's look at the parameter passing of the function, as follows:

Def fun_a (a):... A = axi4... > > g = 0 > > fun_a (g) > g0

When the global variable g is passed to the function fun_a, the parameter an equivalent to the function is also affixed to 0 as a label, and then an is re-assigned (a=a+4), which is equivalent to tearing off the label a from 0 to 4, and g is still the label above 0.

Let's look at this function, which passes a list object.

Def fun_b (names):... Names [0] = ['XLY']. > n_list = ['a PUBG] > fun_b (n_list) > n_list [['XPY],' BFLY]

The same as the previous steps, names and n_list are both tags on ['axiomagnegy', except that the 0th element in the list has been reassigned, but names and n_list are still affixed to the list object. Although the value of n_list is updated, the object is still the same as the original object. It is as if Zhang San and Xiao Zhang are the same person. Now when you change clothes for Xiao Zhang, you are actually changing clothes for Zhang San. The person is the same person, but the things on his body have changed.

This is the end of the content of "what is the method of Python variable and assignment". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report