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
from urllib2 import urlopen # Open http://placekitten.com/ for reading on line 4! response = kittens.read() body = response[559:1000] # Add your ''print'' statement here!

Synthetic models One  goal  of various  people in a variety of sub-disciplines of  electrical  engineering and  computer science is automatic synthesis of machine from  formal

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?

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

Bank transfer What  if we  have  two  values,  representing bank  accounts, and  need  to transfer an  amount of money  amt between them?  Consider that a bank account is show

inputWithinRange (prompt, lowValue, highValue) This function will ask for a number using the prompt parameter. The function will only return a value that is within the inclusive ra

Write a program that asks the user to enter a number of seconds. The responses of the program will vary depending on the length of seconds: • If the number of seconds is under 60

Use the function to compute and print the sum of first n prime numbers.

Range Very frequently, we will want to iterate through a list of integer values, often as indices.  Python gives a useful function, range, which gives lists of integers. It ma

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