** * 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. ** **
>>> t = (1, 2, 3)
>>> t
(1, 2, 3)
>>> type(t)
<class 'tuple'>
I used [] in the list type, but
Use () for tuple type.
Indexes, slices, .count ()
and .index ()
can also be used in tuple types.
>>> t = (1, 2, 3)
>>> t
(1, 2, 3)
>>> t[0] = 100
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
In the list type, you could edit the elements in the list later, Basically, you cannot edit elements in tuple type.
>>> x = (1, 2, 3)
>>> y = (4, 5, 6)
>>> z = x + y
>>> z
(1, 2, 3, 4, 5, 6)
()
>>> t = 1, 2, 3
>>> t
(1, 2, 3)
>>> type(t)
<class 'tuple'>
If ()
is omitted, it is judged as a tuple type.
>>> a = 1
>>> a
1
>>> type(a)
<class 'int'>
>>> b = 1,
>>> b
(1,)
>>> type(b)
<class 'tuple'>
When ,
is added, it is judged as a tuple type.
Note that it may be judged as a tuple type by the ,
that you accidentally attached, and it may cause a bug.
Recommended Posts