Dictionaries

myDictionary = {}

Use this to create own dictionary then add sections such as

myDictionary = {

"class": "LOCOROBO11",

"topic": "Robots",

"year": 2020

}


HOW TO PRINT

  1. To loop and print the keys

  2. To loop and print the values

  3. To loop and print the values using the values() command

  4. To loop and print the key and value pairs using the items() command

# 1

for i in myDictionary:

print(i)


class

topic

year

# 2

for i in myDictionary:

print(myDictionary[i])



LOCOROBO11

Robots

2018

# 3

for i in myDictionary.values():

print(i)



LOCOROBO11

Robots

2018


# 4

for i, j in myDictionary.items():

print(i, j)



class LOCOROBO11

topic Robots

year 2018

KEY VALUE

myDictionary["term"] = "Fall"

print(myDictionary)


if "term" in myDictionary:

print("Yes, 'term' is one of the keys in myDictionary")

else:

print("'term' does not exist as a key")