** Tuple ** is a type for managing multiple data together. In that sense, it's similar to a list.
Lists are commonly used to store "same type of data" in any number.
Tuples are used to combine different types of data.
For example, when you want to manage the combination of "name", "height", and "weight" of a student. When finding the average height in it. This is represented in a list as follows.
taro = ['taro', 170, 80]
jiro = ['jiro', 180, 85]
saburo = ['saburo', 172, 81]
class_list = [taro, jiro, saburo]
sum_height=0
for person in class_list:
sum_height += person[1]
print(sum_height/len(class_list))
174.0
taro = ('taro', 170, 80)
jiro =('jiro', 180, 85)
saburo = ('saburo', 172, 81)
class_list = [taro, jiro, saburo]
sum_height=0
for person in class_list:
sum_height += person[1]
print(sum_height/len(class_list))
174.0
The result is of course the same.
Tuples are similar to lists in terms of how to declare and refer to elements, but they are quite different in substance.
Lists can add, remove, and modify elements, but tuples are immutable objects that can only be referenced once created.
Let's see how to use tuples a little more
You can see that it can be treated like a list, but cannot be changed or deleted.
>>> taro = ('taro', 180, 80) # ()Declare a tuple type using.
>>> type(taro) #Confirm that it is a tuple type
<class 'tuple'>
>>> print(taro[1]) #You can refer to it like a list.
180
>>> taro[0] = 'jiro' #The value of tuple type cannot be changed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>
>>> del taro[1] #The tuple type cannot delete the value
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> (name, height, weight) = ('taro', 180, 80)
>>> print(name) #Data is easier to handle
taro
>>> print(weight)
80
>>>
>>> my_list = ['a', 'b', 'c', 'd']
>>> enum_obj = enumerate(my_list)
>>> print(enum_obj)
>>> print(list(enum_obj))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
The enumerate function makes it easy to loop the list with index numbers.
(I don't know what to use it for)
For example, how to use
list1 = ['a', 'b', 'c']
for (index, item) in enumerate(list1):
print('{}:{}'.format(index, item))
0:a
1:b
2:c
set | data |
---|---|
Fruit | Apple Banana Strawberry |
>>> my_set = set()
>>>
>>> my_set.add('apple') #Add apple
>>> print(my_set)
{'apple'}
>>>
>>> my_set.add('banana') #Added banana
>>> print(my_set)
{'banana', 'apple'}
>>>
>>> my_set.add('apple') #Add apple. Not added, no error
>>> print(my_set)
{'banana', 'apple'}
>>>
set | data |
---|---|
Fruit | Apple: Red Banana: Kiiro Strawberry: Aka |
>>> fruits_dict = dict()
>>> fruits_dict['apple'] = 'red' #Enter the key and value
>>>
>>> fruits_dict['banana'] = 'yellow'
>>> fruits_dict
{'apple': 'red', 'banana': 'yellow'}
>>> fruits_dict['apple'] #Extract value from key
'red'
>>>
Checked frequently used data types.
If you can handle dictionary type, it seems that you can import JSON format and spit it out.
Recommended Posts