- Get link
- X
- Other Apps
In Python, you can assign multiple values to multiple variables in a single line using the syntax:
```
var1, var2, var3 = value1, value2, value3
```
This is known as multiple assignment, and it is a convenient way to assign values to multiple variables at once.
The number of variables on the left-hand side of the equals sign must match the number of values on the right-hand side. For example:
```
x, y, z = 1, 2, 3
```
This assigns the value 1 to variable `x`, the value 2 to variable `y`, and the value 3 to variable `z`. You can also use variables on the right-hand side:
```
a = 1
b = 2
c = 3
x, y, z = a, b, c
```
This assigns the value of `a` to `x`, the value of `b` to `y`, and the value of `c` to `z`.
You can also use a list or tuple on the right-hand side of the equals sign to assign multiple values to multiple variables:
```
values = (1, 2, 3)
x, y, z = values
```
This assigns the first value in the tuple to `x`, the second value to `y`, and the third value to `z`.
Multiple assignment can be a powerful tool for simplifying your code and making it more concise. By assigning multiple values to multiple variables in a single line, you can write more efficient and readable code.
Comments
Post a Comment