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
#Why can''t I cd my Desktop? A minute ago I would open up cmd and it starts in C:\Users\myname but now it''s starting in C:\WINDOWS\system> and I don''t where I am or how to get ou

Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl

We encourage you to adopt an interactive style of debugging and programming. Use the Python shell a lot. Write short pieces of code and check them.  It is much  simpler to test the

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

List Comprehensions   Python has a very nice built-in  facility for doing  many  iterative methods, known as list comprehensions. The basic template is       [

what are the issues assciated with top-down analysis

write a program that creates s dictionary containing course

Task This problem creates a list containing names. The list of names will be printed, sorted, printed again in the new sorted order, written to a new output file, and searched. Ca

Tower of Hanoi game that you can let a player to move discs between the towers using a mouse. Moreover, you are required to do the followings: •Graphically represent any state in t

Assume you have a truck which has to travel across a desert from the base camp at position 0 (left) to the target camp at position 4 (right). The intermediate positions 1,2, and 3