Tuples that are often seen in error displays.
Know how to become a tuple to prevent it from becoming a tuple before you know it.
--One of the python types --Type: list, int, str, etc. ――A group of multiple elements arranged in order --Different from array (list) --Cannot sort --Elements cannot be added / deleted --Limited methods available
[Assign to variable separated by "," without parentheses](# 1-Assign to delimited variable without parentheses)
Enclose in [(). "," Is one or more](# 2-Encloses one or more)
[Use tuple method](# 3-Use tuple method)
[1 character (str)](# 1 character str)
[2 or more character string (str)](# 2 or more character string str)
[Number in array format](# Number in array format)
[Empty argument](# array format number)
[Conditions that do not become tuples](# 4 Conditions that do not become tuples)
[How to specify tuple elements](# 5 Tuple element specification)
[Conditions for errors in tuples](# 6 Conditions for errors in tuples)
[Methods that can be used with tuples](Methods that can be used with # 7 tuples)
[count method](# 1 count method)
[index method](# 2index method)
[Summary of how to use tuples](# 8 How to use tuples)
Numerical values, strings, and variables can be tuples.
How to make tuple (numerical value)
a = 1,2,3,4,5
type(a)
#Output: tuple
How to make tuple (character string)
b = 'AAA', 'BBB', 'CCC',
type(b)
#Output: tuple
How to make tuple (variable)
A = 'AAA'
B = 'BBB'
C = 'CCC'
d = A, B, C,
type(d)
#Output: tuple
Not tuple (int)
e = 1
type(e)
#Output: int
Not tuple (str)
f = "A"
type(f)
#Output: str
Even if it is a single character, it becomes a tuple if you add ",".
tuple(int,)
g = 1,
type(g)
#Output: tuple
tuple(str,)
h = "A",
type(h)
#Output: tuple
tuple (empty)
j = tuple()
type(j)
#Output: tuple
Numerical values, strings, and variables can be tuples.
How to make tuple (with numbers and parentheses)
a = (1,2,3,4,5)
type(a)
#Output: tuple
How to make tuple (with character string and parentheses)
b = ('AAA', 'BBB', 'CCC')
type(b)
#Output: tuple
How to make tuple (with variables and parentheses)
A = 'AAA'
B = 'BBB'
C = 'CCC'
d = (A, B, C)
type(d)
#Output: tuple
Does not become tuple (with int and parentheses)
e = (1)
type(e)
#Output: int
Does not become tuple (with str and parentheses)
f = ("A")
type(f)
#Output: str
Even if it is a single character, it becomes a tuple if you add ",".
tuple(int,・ With parentheses)
g = (1,)
type(g)
#Output: tuple
tuple(str,・ With parentheses)
h = ("A",)
type(h)
#Output: tuple
tuple method (1 character)
tuple("A")
#output:('A',)
tuple method (multiple strings)
tuple("ABCD")
#output:('A', 'B', 'C', 'D')
Separated character by character
tuple method (one array type number)
tuple([123])
#output:(123,)
tuple method (multiple numbers of array type)
i = tuple([1,2,3,456,789])
type(i)
#Output: tuple
tuple method (numerical value / error)
tuple(1)
#Output: "TypeError": 'int' object is not iterable」
tuple method (numerical value / error)
j = tuple(1,2,3,456,789)
type(j)
#Output: "TypeError": tuple expected at most 1 argument, got 5」
** ■ Case Study **
You can avoid unintentionally tuples by using []
to indicate that it is a list.
Does not become tuple (with int and parentheses)
e = (1)
type(e)
#Output: int
Does not become tuple (with str and parentheses)
f = ("A")
type(f)
#Output: str
Does not become tuple (with int and parentheses)
e = (1)
type(e)
#Output: int
Does not become tuple (with str and parentheses)
f = ("A")
type(f)
#Output: str
Not tuple ([ ]Multiple characters enclosed in)
k = ["AAA","BBB","CCC"]
type(k)
#Output: list
Not tuple ([ ]Numbers enclosed in 1)
m = [1]
type(m)
#Output: list
Not tuple ([ ]Numbers enclosed in (multiple)
n = [1,2,3,4,5,]
type(n)
#Output: list
tuple error
tuple(1)
#Output: TypeError: 'int' object is not iterable
Tuple element specification (numerical value)
a = (1, 2, 3, 4, 5)
a[1] #Output: "2"
type(a[1]) #Output: "int"
Each element is an int, not a tuple
Tuple element specification (character string)
b = ('AAA','BBB','CCC')
b[2] #output:"'CCC'」
type(b[2]) #Output: "str"
Each element is str, not tuple
tuple error (append)
a = (1, 2, 3, 4, 5)
a.append(6)
#Output: AttributeError: 'tuple' object has no attribute 'append'
You can use it with list
a = [1, 2, 3, 4, 5]
a.append(6)
a
#output:[1, 2, 3, 4, 5, 6]
Count how many elements specified by the argument are in the tuple.
A.count(x)
└ "A" tuple
└ "x" counting element
└ * Not the number of characters
count(Numerical value)
a = (111,222,333,111,444,123,555,111,222,111)
a.count(111)
#Output: 4
There are four "111" in a
count(String)
b = ("AAA","BBB","CCC","aaa","bbb","AAA",123, 456, "AAA")
b.count("AAA")
#Output: 3
There are 3 "AAA" in b
A.index(x)
└ "A" tuple
└ "x" counting element
└ Returns index number
index
a = (111,222,333,111,333)
a.index(333)
#Output: 2
"333" in a is the second (counted from 0. 1st number)
index (character string)
b = ("AAA","BBB","CCC","aaa",123, 456, "aaa")
b.index("aaa")
#Output: 3
"Aaaa" in a is the third (counting from 0. First number)
index (all output)
a = (111,222,333,111,333,111)
for i, aaa in enumerate(a):
if aaa==111:
print(i)
#Output: 0 3 5
▼ What is ʻenumerate (A)`?
--enumerate: A function that can retrieve the "element" in A and the "index number" of the element --A contains list and tuple --Use as a set with a for statement --Two new variables are needed --Variable to put "element" --Variable to put "index number"
▼ How to use enumerate
for a, b in enumerate(A):
└ "a": Store index number
└ "b": Store element
In the above example, the index number is output when the element is 111.
There are few scenes where you have to be a tuple, so basically you don't use it (should ...)
To avoid unexpected tuples, add []
to indicate that it is a list.
Recommended Posts