
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Assign Value to Multiple 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'
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)
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))
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)
Assign Values to Several Variables Simultaneously
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.
When declaring variables in this way, focus to the order in which the names and values are assigned. For example, the first variable name on the left of the assignment operator gets assigned the first value on the right, and so on.
Example: Assigning Homogeneous 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 homogeneous. A single data type is shared by all the data items of a homogeneous set. For instance: Arrays
In this example, we will see how to assign a homogeneous 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)
This output will be displayed when the program runs -
Assigned value of a is 1 Assigned value of b is 2 Assigned value of c is 3
Example: 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 heterogeneous 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)
Following is the output of the above program -
Assigned value of a is 1 Assigned value of b is 6.55 Assigned value of c is Tutorialspoint
Example: 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 # Swap values using multiple assignment m, n = n, m # Print swapped values print(m) # Output: 10 print(n) # Output: 5
Following is the output of the above program -
10 5
Example: 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 like fruit1, fruit2, and fruit3. This shows how Python allows us to easily unpack values from lists into various variables.
# List of fruits fruits = ["apple", "banana", "cherry"] # Unpack the list into variables fruit1, fruit2, fruit3 = fruits # Print the assigned variables print(fruit1) print(fruit2) print(fruit3)
Following is the output of the above program -
apple banana cherry