-Memo # 1 for Python beginners to read "Detailed explanation of Python grammar" -Memo # 2 for Python beginners to read "Detailed Explanation of Python Grammar" -Memo # 3 for Python beginners to read "Detailed explanation of Python grammar" -Memo # 4 for Python beginners to read "Detailed explanation of Python grammar" -Note that a Python beginner read "Detailed explanation of Python grammar" # 5 -Note that Python beginners read "Detailed explanation of Python grammar" # 6
--Any object can be freely registered --Similar to a list, but cannot be updated (elements cannot be added / deleted after creation) --Elements are references to other objects -Of course, since it is a sequence object, it is possible to extract elements by subscripting or slicing.
Tuple
#The basic writing method is the same as the list.
t = (1, 2, 3)
t = ()
t = (1, 'Two', [3, 4])
#Tuple literal()Is optional (but required for empty tuples)
>>> t = (1, 2, 3) #this is
>>> t
(1, 2, 3)
>>> t = None
>>> t
>>> t = 1, 2, 3 #Can be written like this
>>> t
(1, 2, 3)
>>> t = 1, #Tuple consisting of one integer value
>>> t
(1,)
Swapping two variable values using tuples
#In C and Java, variables for temporary storage are required, but in Python it can be done like this
>>> a = 1
>>> b = 2
>>> b, a = a, b # b, a = (a, b)
>>> a
2
>>> b
1
Tuple method
#Get the number of elements equal to the argument
>>> t = (1, 2, 1)
>>> t.count(1)
2
#Finds an element with a value equal to the argument and returns the index of the first matching element
>>> A = (3, 4, 5, 3, 4, 5)
>>> A.index(4)
1
--Non-updatable sequences (ie tuples) have the advantage that they can be used as dictionary keys or set elements. --The elements of the list are independent data (for example, "list of user names" and "list of sales amount"). It is common to create it as a fixed list of data. (If it is an RDB, is it an image to put a column?) --Tuples are used when the tuple as a whole has meaning as data (elements alone do not make sense). (If it is an RDB, is it an image to insert a line?) ――So, tuples are used more like structures than arrays. --Tuples are more memory efficient than lists because they are fixed-length sequences. Based on the above policy, it is not uncommon for the amount of data that you want to keep in tuples to be large due to its nature, so you can expect efficient resource use and reduction of garbage collection load by properly using tuples instead of lists. ..
Recommended Posts