Thursday 29 June 2023

Python: unpack the tuple to multiple variables

Unpacking is a procedure to assign values of an iterable to multiple values.

 

Example

employee = (1, 'Krishna', 34, 'Bangalore')
id, name, age, city = employee

In the above example,

a.   Value 1 is assigned to the variable 'id'

b.   'Krishna' is assigned to the variable 'name'

c.    34 is assigned to the variable 'age'

d.   'Bangalore' is assigned to the variable 'city'

 

unpack_data.py

employee = (1, 'Krishna', 34, 'Bangalore')
id, name, age, city = employee

print('id : ', id)
print('name : ', name)
print('age : ', age)
print('city : ', city)

Output

id :  1
name :  Krishna
age :  34
city :  Bangalore





 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment