Many Values to Multiple Variables
In Python, you can assign multiple values in a single line. Here are a few methods.
a, b, c = "code", "with", "ronny"
print(a) #code
print(b) #with
print(c) #ronny
One Value to Multiple Variables
In Python, you can assign one values to multiple variables in a single line. Here are a few methods.
a = b = c = "code"
print(a) #code
print(b) #code
print(c)#code
Unpacking
Assigning all the value into variables is called unpacking. There is alots of methods
Method 1: Unpacking Using Lists
# Using a list
values = [10, 20, 30]
a, b, c = values
print(a) #10
print(b) #20
print(c) #30
Method 2: Unpacking Using Tuples
# Using a tuple
values = (40, 50, 60)
a, b, c = values
print(a) #40
print(b) #50
print(c) #60
Method 3: Unpacking Using the Asterisk *
(Splat) Operator
# Using the splat operator to gather values into a list
values = [100, 110, 120]
x = [*values]
print(x) #[100, 110, 120]
# Using the splat operator to gather values into a list
values = [100, 110, 120, 130, 140, 150, 160, 170]
x, *y, z = values
print(x) #100
print(*y) #110 120 130 140 150 160
print(z) #170
# Using the splat operator to gather values into a list
values = [100, 110, 120, 130, 140, 150, 160, 170]
x, y, *z = values
print(x) #100
print(y) #110
print(z) #120 130 140 150 160 170