a = [apple, banana, orange]
Each element can be retrieved by adding [] immediately after the variable name and substituting a numerical value.
a = ['Bulbasaur', 'Charmander', 'Squirtle']
print(a[0]) #Bulbasaur
print(a[1]) #Charmander
print(a[2]) #Squirtle
You can retrieve the list as it is by using:.
a = ['Bulbasaur', 'Charmander', 'Squirtle']
print(a[0:3]) # ['Bulbasaur', 'Charmander', 'Squirtle']
You can store the list in the list. At this time, what can be taken out with [] is Note that it is only the list in the list.
a = [['Bulbasaur', 'Charmander', 'Squirtle'], ['Chikorita', 'Cyndaquil', 'Totodile']]
print(a[0]) # ['Bulbasaur', 'Charmander', 'Squirtle']
print(a[1]) # ['Chikorita', 'Cyndaquil', 'Totodile']
b = {'Treecko', 'Torchic', 'Mudkip'}
Is the big difference between lists and tuples __mutable __? That's what it means.
You might think that it would be more convenient to be able to change it, Tuples are useful for data that you don't want to change.
For example, I don't want you to change __ semi-invariant data such as latitude and longitude of the map. Tuples are used in such cases.
c = {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water'}
print(c['Turtwig']) # 'Grass'
print(c) # {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water'}
Elements can be replaced by assigning to the key itself.
c['Turtwig'] = 'grass'
print(c['Turtwig']) # 'grass'
Similarly, if you set the key and value as a set, you can add elements.
c = {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water'}
c['Pikachu'] = 'Electric'
print(c) # {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water', 'Pikachu': 'Electric'}
Recommended Posts