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

How to realize multiple allocation of variables by python

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

Share

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

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.

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