Python 103

Dictionaries, Sets, Nesting !
## A dictionary 
koray = {"name" : "koray tugay", "age" : 33}
print(koray["name"].title()) ## Koray Tugay
print(koray["age"]) ## 33
 
## You can add new key/value pairs
koray["gender"] = "male"
print(koray) ## {'name': 'koray tugay', 'age': 33, 'gender': 'male'}
 
for key in koray:
    print(key + " " + str(koray[key])) ## You need the str since 33 is an int!
 
print(koray.items()) ## dict_items([('name', 'koray tugay'), ('age', 33), ('gender', 'male')])
 
for key, value in koray.items():
    print(key, end = " ")
    print(value)
 
## name koray tugay
## age 33
## gender male
 
print(koray.keys()) ## dict_keys(['name', 'age', 'gender'])
print(koray.values()) ## dict_values(['koray tugay', 33, 'male'])
 
## Another example, demonstrates set..
foo = {
    "adam" : 1,
    "bjorn" : 2,
    "cindy" : 1,
    "dave": 2
}
 
print(foo) ## {'adam': 1, 'bjorn': 2, 'cindy': 1, 'dave': 2}
print(set(foo.values())) ## {1, 2}
 
def build_alien(name, age, color):
    alien = {
        "name" : name,
        "age" : age,
        "color" : color
    }
    return alien    
 
## Nesting Examples
alien_0 = build_alien("koray", 33, "brown")
alien_1 = build_alien("deniz", 1, "white")
 
alien_2 = {
    "name" : "toprak",
    "age" : 1,
    "color" : "brownish white"
}
 
all_aliens = [alien_0, alien_1, alien_2]
 
for alien in all_aliens:
    print(alien)
 
## {'name': 'koray', 'age': 33, 'color': 'brown'}
## {'name': 'deniz', 'age': 1, 'color': 'white'}
## {'name': 'toprak', 'age': 1, 'color': 'brownish white'}