Assigning multiple variables in one line in Python
A variable is a segment of memory with a unique name used to hold data that will later be processed. Although each programming language has a different mechanism for declaring variables, the name and the data that will be assigned to each variable are always the same. They are capable of storing values of data types.
The assignment operator(=) assigns the value provided to its right to the variable name given to its left. Given is the basic syntax of variable declaration:
Syntax: var_name = value
a = 4
Assign Values to Multiple Variables in One Line
Given above is the mechanism for assigning just variables in Python but it is possible to assign multiple variables at the same time. Python assigns values from right to left. When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.
While declaring variables in this fashion one must be careful with the order of the names and their corresponding value first variable name to the left of the assignment operator is assigned with the first value to its right and so on.
Variable assignment in a single line can also be done for different data types.
value assigned to a 4 value assigned to b 8
Not just simple variable assignment, assignment after performing some operation can also be done in the same way.
assigning values of different datatypes 4 geeks 3.14 True
Assigning different operation results to multiple variable.
Here, we are storing different characters in a different variables.
G eek s