How do we assign a value to several variables simultaneously in Python?


Python is not a "statically typed" programming language. We do not need to define variables or their types before utilizing them. Once we initially assign a value to a variable, it is said to be created. Each variable is assigned with a memory location.

The assignment operator (=) assigns the value provided to right to the variable name which is at its left.

Syntax

The syntax of the assignment operator is shown below.

var_name = value

Example

The following is the example which shows the usage of the assignment operator.

Name = ‘Tutorialspoint’

In Python, variable is really a label or identifier given to object stored in memory. Hence, same object can be identified by more than one variables.

>>> a=b=c=5
>>> a
5
>>> b
5
>>> c
5

a, b and c are three variables all referring to same object. This can be verified by id() function.

>>> id(a), id(b), id(c)
(1902228672, 1902228672, 1902228672)

Python also allows different values to be assigned to different variables in one statement. Values from a tuple object are unpacked to be assigned to multiple variables.

>>> a,b,c=(1,2,3)
>>> a
1
>>> b
2
>>> c
3

Assigning values to several variables simultaneously.

Python assigns values in a left to right manner. Different variable names are provided to the left of the assignment operator, separated by a comma, when assigning multiple variables in a single line. The same is true for their values, except that they should be placed to the right of the assignment operator.

When declaring variables in this way, it's important to pay attention to the sequence in which the names and values are assigned. For example, the first variable name to the left of the assignment operator is assigned with the first value to the right, and so on.

Example 1

Assigning homogenous data type at once

When all of the data elements in a structure are of the same data type, the structure is said to be homogenous. A single data type is shared by all the data items of a homogenous set. For instance: Arrays

In this example we will see how to assign a homogenous data type to variables in a single statement.

a,b,c = 1,2,3 print("Assigned value of a is") print(a) print("Assigned value of b is") print(b) print("Assigned value of c is") print(c)

Output

On executing the above code, the following output is obtained.

Assigned value of a is
1
Assigned value of b is
2
Assigned value of c is
3

Example 2

Assigning heterogeneous data types

Multiple types of data can be stored simultaneously in heterogeneous data structures.

In this example we will see how to assign a heterogenous data type to variables in a single statement.

a,b,c = 1, 6.55, "Tutorialspoint" print("Assigned value of a is") print(a) print("Assigned value of b is") print(b) print("Assigned value of c is") print(c)

Output

On executing the above code, the following output is obtained.

Assigned value of a is
1
Assigned value of b is
6.55
Assigned value of c is
Tutorialspoint

Updated on: 08-Nov-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements