If the object is an iterative (any sequence), it can be decomposed, including tuples, lists, strings, files, iterators, and generators, which can be decomposed into separate variables by a simple assignment operation.
The only requirement: The total number of variables matches the sequence, otherwise an error will occur;
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5, 20:32:19) [MSC v.1500 32bit (Intel)] on Win32type"Copyright","credits" or "license ()" forMore information.>>> p=[4,5]>>>p[4, 5]>>> x,y=P>>>x4>>>y5>>> data=['Lucy', 50, 12.1, (2016,07,31)]>>>data['Lucy', 50, 12.1, (2016, 7, 31)]>>> name,shares,price,date=Data>>>name'Lucy'>>>shares50>>> Price12.1>>>Date (2016, 7, 31)>>> Name,shares,price, (year,mon,day) =Data>>>name'Lucy'>>>shares50>>> Price12.1>>> Year2016>>>Mon7>>> Day31>>> s='Hello'>>>s'Hello'>>> a,b,c,d,e=s>>>C'L'>>>D'L'>>>e'o'>>> data=['Lucy', 50, 12.1, (2016,07,31)]>>> _,shares,price,_=data#when you want to discard certain values, you can choose a variable name that is not used as the name of the value to discard, such as "_" here .>>>shares50>>> Price12.1>>>
Note: Be sure to discard the value of the selected variable name that has not been used anywhere else.
"Python Cookbook" "Data Structure and algorithm" 1 breaks a sequence into separate variables