Paiza Python Primer 5: Basics of Dictionaries

Python3 is completely free on Paiza, so I summarized it.

Introduction to Paiza Python3

01: Learn what a dictionary is

lesson.py


Text data(Key)Handle with

list
for list 012

list[0]="monster"
list[1]="Dragon"
list[2]="Devil"

dictionary

dict Zako middle boss last boss

dict["Zako"]="monster"
dict["Medium boss"]="Dragon"
dict["Las boss"]="Devil"

Can be done like

Basic function of dictionary

enemies Zako middle boss last boss

Browse data with key
print(enemies["Zako"])

Find the key variable
level="Medium boss"
print(enemies[level])

The number of data
print(len(enemies))

Can be added, updated and deleted

Dictionary usage
1.Processing of data exchanged with DB
Score list data(Column)→ Process with list
National language score

2.Processing of data that interacts with the API
Student record(Row)→ Dictionary(dictionary)Processed by
Attendance number, national language, math, English

02 Make a dictionary

lesson.py


#List review
enemyArray=["Slime","monster","Dragon"]
print(enemyArray)
print(enemyArray[0])

#Specific example of dictionary
enemyDictionary= {"Zako":"Slime","Medium boss":"Dragon","Las boss":"Devil"}
print(enemyDictionary)
print(enemyDictionary["Zako"])
print(enemyDictionary["Medium boss"])

level= "Zako"
print(enemyDictionary[level])

level="Las boss"
print(enemyDictionary[level])

#Anything that does not exist will result in an error
#print(enemyDictionary["enemy"])

lesson.py


print(enemyArray)
['Slime', 'monster', 'Dragon']
print(enemyArray[0])
Slime
print(enemyDictionary)
{'Zako': 'Slime', 'Medium boss': 'Dragon', 'Las boss': 'Devil'}
print(enemyDictionary["Zako"])
Slime
print(enemyDictionary["Medium boss"])
Dragon
level= "Zako"
Slime
level="Las boss"
Devil

Exercise

  1. Let's make the specified character into a dictionary

lesson.py


skills = {"Profession":"Warrior", "Physical fitness":100, "Magical power":200,"gold":380}

print(skills)

  1. Let's check the contents of the hash

lesson.py



skills = {"Profession":"Warrior", "Physical fitness":100, "Magical power":200, "gold":380}
#Below this, let's output the dictionary
print(skills)


  1. Let's output a specific value in the dictionary

lesson.py



skills = {"Profession":"Warrior", "Physical fitness":100, "Magical power":200, "gold":380}
#Below this, let's output from the dictionary
print(skills["Profession"])

03: Basic dictionary operation

lesson.py



#Basic operation of dictionary
enemies = {"Zako":"Slime", "Medium boss":"Dragon", "Las boss":"Devil"}
print(enemies)
print(enemies["Zako"])
print(enemies["Medium boss"])

#You can get the number of dictionaries
print(len(enemies))

#Can be added to the dictionary
enemies["Zako 2"]="Metal monster"
print(enemies)
print(len(enemies))

#Change value
enemies["Medium boss"]="Red Dragon"
print(enemies)
print(len(enemies))

#Delete value
del enemies["Zako"]
print(enemies)
print(len(enemies))

Exercise

  1. Let's output the length of the dictionary

lesson.py


skills = {"Profession":"Warrior", "Physical fitness":100, "Magical power":200, "gold":380}
#Below this, let's output the length of the dictionary
print(len(skills))

  1. Let's add dictionary elements

lesson.py



skills = {"Profession":"Warrior", "Physical fitness":100, "Magical power":200, "gold":380}
#Below this, let's add data to the dictionary

skills["attribute"]="flame"
print(skills)

  1. Update the elements of the dictionary

lesson.py



skills = {"Profession":"Warrior", "Physical fitness":100, "Magical power":200, "gold":380}
print(skills)
#Below this, let's update the hash
skills["Profession"]="Wizard"
print(skills)

  1. Delete the elements of the dictionary

lesson.py



skills = {"Physical fitness" : 100,"Profession" : "Warrior",  "Magical power" : 200, "gold" : 380}
print(skills)
#Below this, let's delete the dictionary
del skills["Physical fitness"]
print(skills)

04: Loop the dictionary

lesson.py



#Review of the dictionary
enemies = {"Zako":"Slime", "Medium boss":"Dragon", "Las boss":"Devil"}
print(enemies)
print(enemies["Zako"])
print(enemies["Medium boss"])

#Loop processing
for rank in enemies:
    print(enemies[rank] + "However, it appeared!")
    
#Use the items method to retrieve dictionary keys and values in pairs
for (rank,enemy) in enemies.items():
    print(rank + "of" + enemy + "However, it appeared!")
    

Exercise

  1. Let's output the dictionary value in a loop

lesson.py


skills = {"Profession" : "Warrior", "Physical fitness" : 100, "Magical power" : 200, "gold" : 380}
#Below this, let's output the dictionary value in a loop
for i in skills:
    print(skills[i])
  1. Output dictionary keys and values in a loop

lesson.py



skills = {"Profession" : "Warrior", "Physical fitness" : 100, "Magical power" : 200, "gold" : 380}
#Below this, let's output the hash value in a loop
for(key,item) in skills.items():
    print(key + "Is" + str(item) + "is")
    
  1. Calculate the total in a loop

lesson.py



points = {"National language" : 70, "arithmetic" : 35, "English" : 52}
sum = 0
#Below this, let's calculate the sum of the dictionary values in a loop

for key in points:
    sum +=points[key]
print(sum)

05: List alignment

lesson.py



#Sorting the list

weapons=["Aegis Sword","Wind Spear","Earth breaker","Inazuma Hammer"]

print(weapons)

#Aiueo order
print(sorted(weapons))

#Opposite of Aiueo order
print(sorted(weapons,reverse=True))

wepons2=["4.Aegis Sword","1.Wind Spear","3.Earth breaker","2.Inazuma Hammer"]

#Numbers>In order of Aiueo
print(sorted(wepons2))


wepons3=["4.Aegis Sword","1 Wind Spear","1 Earth breaker","2.Inazuma Hammer"]

#If there are the same numbers, the order of Aiueo will be prioritized.
print(sorted(wepons3))



wepons4=["Burning sword","Fujin Spear","Earth breaker","Lightning hammer"]

#Be careful when kanji and katakana are mixed!Sometimes not sorted
print(sorted(wepons4))

lesson.py


['Aegis Sword', 'Wind Spear', 'Earth breaker', 'Inazuma Hammer']
['Earth breaker', 'Inazuma Hammer', 'Aegis Sword', 'Wind Spear']
['Wind Spear', 'Aegis Sword', 'Inazuma Hammer', 'Earth breaker']
['1.Wind Spear', '2.Inazuma Hammer', '3.Earth breaker', '4.Aegis Sword']
['1 Earth breaker', '1 Wind Spear', '2.Inazuma Hammer', '4.Aegis Sword']
['Burning sword', 'Earth breaker', 'Lightning hammer', 'Fujin Spear']

Exercise

  1. Sort the list in reverse order

lesson.py


apples = [310, 322, 292, 288, 300, 346]
#Here is the code to sort and display the elements
print(sorted(apples))

  1. Sort the list in reverse order

lesson.py


apples = [310, 322, 292, 288, 300, 346]
Here is the code to sort and display the elements
print(sorted(apples,reverse=True))
  1. Arrange the English word list in alphabetical order

lesson.py


words = ["pumpkin", "orange", "apple", "carrot", "onion"]
#Here is the code to sort and display the elements
print(sorted(words))

06: Alignment of dictionaries

lesson.py


weapons={"Aegis Sword":40,"Wind Spear":12,"Earth breaker":99}
#Display as a dictionary
print(sorted(weapons))
print(weapons)

#Sort dictionary by key(Alignment)To do
#Tuple: A type of data structure, contents cannot be updated
#A data structure that manages various values collectively like a list.
print(sorted((weapons).items()))

Exercise

  1. Let's sort the dictionary in ascending order by key

lesson.py



math = {"Good" : 12, "Pea" : 99, "Adachi" : 40}
#Below this, let's sort the dictionary by key and output it
#Aligning dictionaries

#Display as a dictionary
print(sorted(math))

  1. Sort the dictionary and output it in the dictionary

lesson.py



#Sort the dictionary and output it in the dictionary
math = {"Pea" : 99, "Adachi" : 40, "Good" : 12}

#Below this, let's sort the dictionary by key and output it as a dictionary
print(sorted((math).items()))

07 Reproduce the item list of RPG 1

lesson.py


Item List:Display multiple item images
Sword, vertical, healing medicine, healing medicine, crystal

1.Display item image and item name
2.I want to display the same item multiple times
3.I want to manage the order of items
4.Share one file of the same item image

Combine dictionaries and arrays
Sort order list item_oders

|Sort order|Item name|
| ---- | ---- |
|  0   |crystal|
|  1   |  "sword"  |
|  2   |  "Recovery agents"  |
|  3   |  "shield"  |


↓item_Use orders as a key to retrieve the image file of ↓

Image dictionary item.images
|Item name|Image file|
| ----       | ---- |
|sword|Image of sword|
|crystal|Crystal image|
|shield|Shield image|
|Recovery agents|Image of recovery drug|

If you code this,
item_orders[0]→itemimages[item_orders[0]]→ Crystal image file
item_orders[1]→itemimages[item_orders[1]]→ Sword
:
:
I want to make a program that repeats

08: Reproduce the RPG item list 2

lesspn.py


 
 #Image hash
item_images = {
    "sword":"http://paiza.jp/learning/images/sword.png ",
    "shield":"http://paiza.jp/learning/images/shield.png ",
    "Recovery agents":"http://paiza.jp/learning/images/potion.png ",
    "crystal":"http://paiza.jp/learning/images/crystal.png "
}

#Item order arrangement
items_order = ["crystal", "shield", "sword", "Recovery agents", "Recovery agents", "Recovery agents"]

#print(item_images)
#print(items_order)

lesson.py



for item_name in items_order:
    print(item_name)

output
crystal
shield
sword
Recovery agents
Recovery agents
Recovery agents


lesson.py



#Extract the item name
for item_name in items_order:
    print(item_images[item_name])
    print(item_name)
    
http://paiza.jp/learning/images/crystal.png
crystal
http://paiza.jp/learning/images/shield.png
shield
http://paiza.jp/learning/images/sword.png
sword
http://paiza.jp/learning/images/potion.png
Recovery agents
http://paiza.jp/learning/images/potion.png
Recovery agents
http://paiza.jp/learning/images/potion.png
Recovery agents

lesson.py



#Extract the item name
for item_name in items_order:
    print("<img src='"+item_images[item_name]+"'>")
    print(item_name+"<br>")
<img src='http://paiza.jp/learning/images/crystal.png'>
crystal
<img src='http://paiza.jp/learning/images/shield.png'>
shield
<img src='http://paiza.jp/learning/images/sword.png'>
sword
<img src='http://paiza.jp/learning/images/potion.png'>
Recovery agents
<img src='http://paiza.jp/learning/images/potion.png'>
Recovery agents
<img src='http://paiza.jp/learning/images/potion.png'>
Recovery agents

Exercise

  1. Output images in order

lesson.py



items_imges = {
    "sword" : "http://paiza.jp/learning/images/sword.png ",
    "shield" : "http://paiza.jp/learning/images/shield.png ",
    "Recovery agents" : "http://paiza.jp/learning/images/potion.png ",
    "crystal" : "http://paiza.jp/learning/images/crystal.png "
}

#Item order list
items_orders = ["sword", "shield", "Recovery agents", "crystal"]

#Let's write below from here
for item_name in items_orders:
    print("<img src='"+items_imges[item_name]+"'><br>")
  1. Let's make an item list

lesson.py


input
6
Recovery agents
shield
crystal
crystal
sword
sword

#Image dictionary
items_imges = {
    "sword" : "http://paiza.jp/learning/images/sword.png ",
    "shield" : "http://paiza.jp/learning/images/shield.png ",
    "Recovery agents" : "http://paiza.jp/learning/images/potion.png ",
    "crystal" : "http://paiza.jp/learning/images/crystal.png "
}

#Let's write below from here
#Assign the number of items to be output to a variable
item_cnt = int(input())

#Output items in standard input
while item_cnt > 0:
  item = input()
  print("<img src = '" + items_imges[item] + "'>")
  item_cnt = item_cnt - 1
  
output
<img src = 'http://paiza.jp/learning/images/potion.png'>
<img src = 'http://paiza.jp/learning/images/shield.png'>
<img src = 'http://paiza.jp/learning/images/crystal.png'>
<img src = 'http://paiza.jp/learning/images/crystal.png'>
<img src = 'http://paiza.jp/learning/images/sword.png'>
<img src = 'http://paiza.jp/learning/images/sword.png'>
  

Recommended Posts

Paiza Python Primer 5: Basics of Dictionaries
Paiza Python Primer 4: List Basics
Basics of Python ①
Basics of python ①
Basics of Python scraping basics
# 4 [python] Basics of functions
Basics of python: Output
Paiza Python Primer 1 Learn Programming
python: Basics of using scikit-learn ①
Paiza Python Primer 8: Understanding Classes
Paiza Python Primer 7: Understanding Functions
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Python basics ⑤
Python basics
Python basics ④
Paiza Python Primer 3: Learn Loop Processing
Getting Started with Python Basics of Python
Python basics ③
Python basics
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
About the basics list of Python basics
Learn the basics of Python ① Beginners
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
I didn't know the basics of Python
The basics of running NoxPlayer in Python
[Basics of python basics] Why do __name__ == "__main__"
[Python] Chapter 02-04 Basics of Python Program (About Comments)
[Python] Chapter 02-03 Basics of Python programs (input / output)
[Introduction to Data Scientists] Basics of Python ♬
[Python3] Understand the basics of file operations
Python basics: list
Introduction of Python
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
Copy of python
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Introduction of Python
[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)
[Python] Chapter 02-01 Basics of Python programs (operations and variables)