Multi-assignment of variables in Python

Article #012

Python is an excellent tool which provides great versatility and practicality in terms of its day-to-day usage. One of the great features which it provides is the multi-assignment of variables. As the name suggests, we can define multiple variables in a single line of code. Let's get a better understanding with the help of the below example.

a, b, c = 3, 10, ‘Hi’    
type(a) will yield output as int    
type(b) will yield output as int    
type(c) will yield output as str
print(a) will yield output as 3

This is a very basic example of a multi-assignment of variables in Python. However, the implementation can be far more wider than that discussed in the example. One of such case is coded below.

name, place, year_of_birth, weight = "Sid", "Uttarakhand", 1994, 70

# Input
print(f"""
My name is: {name}
I live at: {place}
I was born in: {year_of_birth}
My weight is: {weight} in Kgs
""")

# Output
My name is: Sid
I live at: Uttarakhand
I was born in: 1994
My weight is: 70 in Kgs

Hope the above example helps you to understand one of the basic concept of Python.