** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
tuple_unpacking
num_tuple = (10, 20)
print(num_tuple)
x, y = num_tuple
print('x =', x, type(x))
print('y =', y, type(y))
result
(10, 20)
x = 10 <class 'int'>
y = 20 <class 'int'>
The elements of the tuple can be separated.
tuple
X, Y = (0, 100)
print('X = ', X, type(X))
print('Y = ', Y, type(Y))
min, max = 0, 100
print('min = ', min, type(min))
print('max = ', max, type(max))
result
X = 0 <class 'int'>
Y = 100 <class 'int'>
min = 0 <class 'int'>
max = 100 <class 'int'>
Tuples could omit ()
.
Writing in parallel like min
and max
means
Once 0, 100
is determined to be a tuple, it is unpacked, resulting in
It is assigned as an int type.
change_object
a, b = 1, 2
print(a, b)
a, b = b, a
print(a, b)
result
1 2
2 1
You can replace the object once assigned.
Recommended Posts