Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. The syntax of the assignment operator is shown below ?
var_name = value
Example of Assignment Operator
The following is an example that shows the usage of the assignment operator ?
Name = 'Tutorialspoint' print(Name)
Tutorialspoint
Multiple Assignment (Same Value)
In Python, a variable is really a label or identifier given to an object stored in memory. Hence, the same object can be identified by more than one variable.
a = b = c = 5 print(a) print(b) print(c)
5 5 5
a, b, and c are three variables all referring to the same object. This can be verified by the id() function ?
a = b = c = 5 print(id(a)) print(id(b)) print(id(c))
140712234567616 140712234567616 140712234567616
Simultaneous Assignment (Different Values)
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) print(a) print(b) print(c)
1 2 3
Python assigns values in a left-to-right order. When assigning many variables on a single line, different variable names appear to the left of the assignment operator, separated by a comma. The same is true for their values, with the exception that they should be set to the right of the assignment operator.
Assigning Homogeneous Data Types
When all of the data elements are of the same data type, you can assign them simultaneously. In this example, we will see how to assign integers to variables in a single statement ?
a, b, c = 1, 2, 3
print("Assigned value of a is:", a)
print("Assigned value of b is:", b)
print("Assigned value of c is:", c)
Assigned value of a is: 1 Assigned value of b is: 2 Assigned value of c is: 3
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 different data types to variables in a single statement ?
a, b, c = 1, 6.55, "Tutorialspoint"
print("Assigned value of a is:", a)
print("Assigned value of b is:", b)
print("Assigned value of c is:", c)
Assigned value of a is: 1 Assigned value of b is: 6.55 Assigned value of c is: Tutorialspoint
Swapping Values
Here we will first assign values to variables m and n. After that, we will swap their values with the usage of a single line of code ?
# Initially assign values to variables
m = 5
n = 10
print("Before swapping: m =", m, ", n =", n)
# Swap values using multiple assignment
m, n = n, m
# Print swapped values
print("After swapping: m =", m, ", n =", n)
Before swapping: m = 5 , n = 10 After swapping: m = 10 , n = 5
Unpacking a List
You can also assign values from a list to variables with the same number of elements. The example shows a list of fruits. We use unpacking to assign each fruit in the list to a separate variable ?
# List of fruits
fruits = ["apple", "banana", "cherry"]
# Unpack the list into variables
fruit1, fruit2, fruit3 = fruits
# Print the assigned variables
print("First fruit:", fruit1)
print("Second fruit:", fruit2)
print("Third fruit:", fruit3)
First fruit: apple Second fruit: banana Third fruit: cherry
Conclusion
Python allows simultaneous assignment using multiple methods: same value to multiple variables (a = b = c = 5), different values to different variables (a, b, c = 1, 2, 3), and unpacking from sequences. This feature makes Python code more concise and readable.
