Study from Python Hour6: Frequently used data types: tuple type, set type, dictionary type

Study from Python Hour6: Frequently used data types: tuple type, set type, dictionary type

Learning materials

Past posts

environment

Tuple type

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
>>> 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

How to handle tuple types / functions

Unpack assignment

>>> (name, height, weight) = ('taro', 180, 80)
>>> print(name)					#Data is easier to handle
taro
>>> print(weight)
80
>>>

enumerate () function

>>> 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')]

list1 = ['a', 'b', 'c']
for (index, item) in enumerate(list1):
	print('{}:{}'.format(index, item))
0:a
1:b
2:c

Set type

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'}
>>>

Dictionary type

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'
>>>

This summary

For Quotations / Lightning Talk

Recommended Posts

Study from Python Hour6: Frequently used data types: tuple type, set type, dictionary type
Study from Python Hour4: Object-oriented ②
Study from Python Hour3: Functions
Study from Python Hour4: Object-oriented ①
Study from Python Hour2: Control statements
Study from Python Hour7: How to use classes
Study from Python Reading and writing Hour9 files
Basic operation list of Python3 list, tuple, dictionary, set
Receive dictionary data from a Python program in AppleScript
Study from the beginning of Python Hour1: Hello World
Convenient argument passing technique in Python (tuple, dictionary type)
Study from the beginning of Python Hour8: Using packages
[Python] Use JSON format data as a dictionary type object
[Python] Frequently used library code
Python data type summary memo
Python frequently used code snippets
Image data type conversion [Python]
python> tuple> data, address = s.recvfrom (10000)
New features in Python 3.9 (1)-Union operators can be used in dictionary types