Python Classroom Series – 07/Sept/2020

Dictionary

  • Dictionary is collection of key value pairs
  • Dictionary is mutable
  • Samples Preview Preview
  • Iterating over dictionary
house_rooms = {'balcony': 3, 'lounge': 1, 'bedrooms': 4 }
for room in house_rooms:
    print(room)
  • Consider the following python file and execute it to understand different ways of iterating dictionary
movie_dict = { 'name': 'Bahubali', 'actors': ['Prabhas', 'anushka'],
     'director': 'SS Rajamouli'
}

print("using movie_dict in for loop")
for detail in movie_dict:
    print(detail)

print("##############################\n\n")



print("using movie_dict.values() in for loop")
for detail in movie_dict.values():
    print(detail)
print("##############################\n\n")

print("using movie_dict.items() in for loop")
for item in movie_dict.items():
    print(item)

print("##############################\n\n")

print("using d_name, d_value in movie_dict.items:")
for d_name, d_value in movie_dict.items():
    print(f"{d_name} = {d_value}")

Preview

Set

  • Set is collection of distinct objects Preview Preview
  • Iterating sets
electronics = set('Laptop', 'Mobile', 'Television', 'Refrigirator')
for item in electronics:
    print(item)


Preview Preview

Pythonic Terms

  • From now on to refer double underscore (like main, getitem) lets use the term dunder (dunder main, dunder getitem)
  • Generally dunder methods are referred as special methods (magic methods)

Python Data Model

  • Refer Here for official documentation of Python data model.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin