In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to achieve Python assignment operation, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
3.2 interesting assignment operations
The assignment operation couldn't be simpler, and it has been used many times in the previous chapter. However, assignment operations in the Python language are much more interesting. For example, you can assign multiple values to multiple variables at the same time.
XQuery ypencil z = 1, 2, 3, 3 print (xpencil, y1, 3) 1 2 3
In the code block above, 1, 2, and 3 are assigned to the three variables x, y, and z, respectively, and the values of these three variables are output. Using this feature in the Python language, it is easy to exchange values between two variables.
XQuery y = 20pr 30xjr y = yPol xprint (xPerry y) 30 20
In the operation of assigning multiple values to multiple variables at the same time, the names and the number of variables on the left and right sides of the equal sign (=) must be equal, otherwise an exception will be thrown.
This feature of the Python language is called sequence unpacking (sequence unpacking), but it is supported by any iterable object. More information about iterative objects (lists, collections) will be described in later chapters.
The Python language also supports chained assignment (chained assignments) and incremental assignment (augmented assignments), which refers to assigning the same value to multiple variables successively.
X = y = 20
Incremental assignment is a simplified form of expression that increases or decreases (negative increments) the specified value of the variable itself. For example, if you use an incremental assignment expression, x + = 2, that is, the x to the right of the equal sign (=) is omitted and the plus sign (+) is placed to the left of the equal sign (=), for example. For example:
X = 5x + = 2print (x) 7
That is to say, the value of x is 5 minutes x + = 2 is equivalent to x = 5 times 2 is assigned to x, and the final output of the x variable is 7.
In fact, the previous binary operators all support incremental assignment, for example, x = x 20 can be written as x = 20, x = x% 3 can be written as x% = 3.
[example 3.2] this example demonstrates the use of sequence unpacking, chain assignment, and incremental assignment.
XRecience yPower2 # # uses sequence unpacking method to assign print (xrecedence z) x # # uses sequence unpacks to exchange the values of x and y print (xPowery) # xMagne yMague z = 1 # throws an exception # xMague y = 1Magne2 # throws an exception x = y = z = j = 20 # uses chain assignment to set x, y, z, jprint (x Y * = 2 # multiplicative incremental assignment y% = 3 # division negative incremental assignment z-= 1 # subtraction negative incremental assignment j + = 1 # addition incremental assignment print 12 32 120 20 2040 2 19 21 Thank you for reading this article carefully I hope the article "how to realize the assignment operation of Python" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.