>>> x,y = tuple1
>>> tuple1[0]
2
>>> x
2
>>> y
4
>>> type(tuple1)
<class 'tuple'>
>>> list1 =['apple',10.3,3,"High quality",["green","USA",3]]
>>> catelog,price,quantity,comment,info = list1
>>> catelog
'apple'
>>> price
10.3
>>> quantity
3
>>> comment
'High quality'
>>> info
['green', 'USA', 3]
>>> type(list1)
<class 'list'>
>>>
>>>
>>> title = "Hello"
>>> a,b,c,d,e = title
>>> a
'H'
>>> b
'e'
>>> c
'l'
>>> d
'l'
>>> e
'o'
>>> type(title)
<class 'str'>
>>>
>>>
>>> a,b = title
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
a,b = title
ValueError: too many values to unpack (expected 2)
>>> a,*b = title
>>> a
'H'
>>> b
['e', 'l', 'l', 'o']
>>>
>>> record =("Dave",'dave@example.com','9999999','8878877','man')
>>> name , email,*tel,sex = record
>>> name
'Dave'
>>> email
'dave@example.com'
>>> tel
['9999999', '8878877']
>>> sex
'man'
>>> records =[("foo",1,2),("bar","hello"),("foo",3,4)]
>>> for tag ,*contents in records:
print(tag,contents)
foo [1, 2]
bar ['hello']
foo [3, 4]
>>> for tag ,*contents in records:
if(tag=="foo"):
print(tag,contents[0],contents[1])
else :
print(tag,contents)
foo 1 2
bar ['hello']
foo 3 4
>>> def do_foo(x,y)
SyntaxError: invalid syntax
>>> def do_foo(x,y):
print('foo',x,y)
>>> def do_bar(s):
print("bar',s)
SyntaxError: EOL while scanning string literal
>>> def do_bar(s):
print("bar",s)
>>> for tag ,*contents in records:
if(tag=="foo"):
do_foo(*contents)
else :
do_bar(*contents)
foo 1 2
bar hello
foo 3 4
>>> for tag ,*contents in records:
if(tag=="foo"):
do_foo(contents)
else :
do_bar(contents)
Traceback (most recent call last):
File "<pyshell#67>", line 3, in <module>
do_foo(contents)
TypeError: do_foo() missing 1 required positional argument: 'y'
>>>