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
how to use re.sub to seperate distint words and phrases when translating text from English to Spanish or vis versa in python programming


Normalize a vector Let's imagine we want  to normalize a vector  of three  variables;  that  is to compute a new  vector  of three values,  such that its size  is 1. Here is o

Tuples  and strings Python has two different more list-like data  types  that are very important to understand.A tuple  is a structure that  is like a list, but  is not  mutab

#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

List mutation and shared structure Lists are mutable data  structures, which defines  that  we can actually modifies  the values  stored in their components. We do this by usi

Your task is to construct a translator from the subset of Python in Project 3 intocontinuation-passing style (CPS). Of course, the expected way of accomplishing this is to trans

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

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

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