Structured assignment, Python Programming

Structured assignment

Once  we have  tuples and lists, we may use  a nice trick  in assignment expression, based  on the packing and unpacking of tuples.

>>> a, b, c = 1, 2, 3

>>> a

1

>>> b

2

>>> c

3

Or, with lists,

 

>>> [a, b, c] = [1, 2, 3]

>>> a

1

>>> b

2

>>> c

3

When  you have  a list (or a tuple)  on the left-hand side of an assignment expression, you have  to have  a list of matching structure on the right-hand side.  Then  Python will "unpack" them  both,  and give  to the individual components of the structure on the left side.  You may get fancier with this function:

 

>>> thing = [8, 9, [1, 2], 'John', [33.3, 44.4]]

>>> [a, b, c, d, [e1, e2]] = thing

>>> c

[1, 2]

>>> e1

33.299999999999997

 

Posted Date: 8/11/2012 1:53:29 AM | Location : United States







Related Discussions:- Structured assignment, Assignment Help, Ask Question on Structured assignment, Get Answer, Expert's Help, Structured assignment Discussions

Write discussion on Structured assignment
Your posts are moderated
Related Questions
1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

#que Write a program that will take price data for stocks and print it graphically to the screen. Your program should begin by asking the user for the file name. It should then cre

Procedures as first-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable

Write a program that will take price data for stocks and print it graphically to the screen. Your program should begin by asking the user for the file name. It should then create a

Write a program that requests a password after the author/program information is displayed. Make the password "hello". The program should then ask the user for their name: if the n

Python  Style Software  engineering courses  often  provide very  rigid  guidelines on the style of programming, generally the appropriate value of indentation, or what  to us

In English, specify a representation of the board game in Python. The representation should capture the entire state of the game at one point in time. It may be helpful to first fi

I have python game project which is due by next Tuesday. Do you think that I can get it on time if I order today?

how to use re.sub to seperate distint words and phrases when translating text from English to Spanish or vis versa in python programming

Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of