How to realize multiple allocation of variables by python
This article will explain in detail how to achieve multiple allocation of variables in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Multiple allocation of variables
Python allows us to assign values to multiple variables in a row. You can separate variables with commas. Front-line multitasking has many advantages. It can be used to assign multiple values to multiple variables or to assign multiple values to a single variable name. Let's make a problem statement, where we must assign values 50 and 60 to variables an and b. The general code is as follows.
A = 50 b = 60 print (a) print (type (a)) print (type (b))
Output quantity
50 60
Condition I-value equals variable
When a variable is equal to multiple assignments, each value is stored in all variables.
Print (type (a)) print (type (b))
Output quantity
50 60
The two programs give the same result. This is the benefit of using a row value allocation.
Conditional II- value is greater than variable
Let's try to increase the number of values in the previous program. You can assign multiple values to a single variable. When assigning multiple values to a variable, we must use an asterisk before the variable name.
A, * b = 50,60,70 print (a) print (b) print (type (a)) print (type (b))
Output quantity
50 [60, 70]
The first value is assigned to the first variable. The second variable will collect the value from the given value. This creates a list type object.
Conditional III- multivariable one value
We can assign a value to multiple variables. Each variable will be separated by an equal sign.
A = b = c = 50 print (a meme bjorc) print (type (a)) print (type (b)) print (type (c))
Output quantity
50 50 50 this article on "how to achieve multiple allocation of variables in python" is shared here. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.