#100DaysOfCode: Python Day 9

#100DaysOfCode: Python Day 9

Dictionaries and Nesting

ยท

2 min read

#100DaysOfCode Day 9

๐Ÿ’ธ I CREATED MY OWN SECRET AUCTION APP! ๐Ÿ’ธ
CLICK HERE and press "Run" to try it out.

Screenshot 2021-05-20 at 08.23.44.png

Screenshot 2021-05-20 at 08.24.26.png

Yesterday I learned how to use the dictionary datatype and nesting them in lists. Now I can work with some complex data in a structured manner. Creating the secret auction was so awesome because it was so simple!

Lessons

  1. Language switching is hard!
  2. You can use this site to visualise your codes execution step by step, using this site.

TO THE CODE!!!

dictionaries.py

# Dictionaries

#{Key:Value}

programming_dictionary = {
    "Bug": "An error in a program that prevents the program from running as expected.", 
    "Function": "A piece of code that you can easily call over and over again.",
    "Loop": "The action of doing something over and over again."
}

# Access it by the key, this can also be an int
print(programming_dictionary["Bug"])

# Adding Values
programming_dictionary[123]: "Test int key"
print(programming_dictionary)

# Creating an empty dictionary
empty_dictionary = {}

# Wipe an existing dictionary
programming_dictionary = {}
print(programming_dictionary)

# Edit Values
programming_dictionary[123] = "EDITED: Test int key"
print(programming_dictionary)

#Looping through a dictionary
for item in programming_dictionary:
    print(item)
# OUTPUT:
# Bug
# Function
# Loop
# 123

# Use this to get the key and value:
for key in programming_dictionary:
    print(key)
    print(programming_dictionary[key])

nesting.py

# Nesting

# Dictionary
capitals = {
    "France": "Paris",
    "Germany": "Berlin"
}

# Nesting a List in a dictionary
travel_log = {
    "France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12},
    "Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 6}
}

# Nesting a dictionary in a list
travel_log = [
    {
        "country": "France", 
        "cities_visited": ["Paris", "Lille", "Dijon"], 
        "total_visits": 12
    },
    {
        "country": "Germany", 
        "cities_visited": ["Berlin", "Hamburg", "Stuttgart"], 
        "total_visits": 6
    }
]

The daily grind (working and studying) is tough, but I keep my goals in mind and push forward!