Python Programming Dictionaries, Lecture notes of Computer Science

Introduction to Python Programming

Typology: Lecture notes

2020/2021

Uploaded on 02/08/2021

aliahmed.98
aliahmed.98 🇵🇰

5

(4)

9 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Programming
Chapter 9 - Dictionaries
Python for Everybody
www.py4e.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Python Programming Dictionaries and more Lecture notes Computer Science in PDF only on Docsity!

Python Programming

Chapter 9 - Dictionaries

Python for Everybody

www.py4e.com

What is a Collection?

  • A collection is nice because we can put more than one value in it and carry them all around in one convenient package
  • We have a bunch of values in a single “variable”
  • We do this by having more than one place “in” the variable
  • We have ways of finding the different places in the variable

A Story of Two Collections..

  • List
    • A linear collection of values that stay in order
  • Dictionary
    • A “bag” of values, each with its own label

Dictionaries

  • Dictionaries are Python’s most powerful data collection
  • Dictionaries allow us to do fast database-like operations in Python
  • Dictionaries have different names in different languages
    • Associative Arrays - Perl / PHP
    • Properties or Map or HashMap - Java
    • Property Bag - C# / .Net

Comparing Lists and Dictionaries Dictionaries are like lists except that they use keys instead of numbers to look up values

lst = list() lst.append( 21 ) lst.append( 183 ) print(lst) [21, 183] lst[ 0 ] = 23 print(lst) [23, 183] ddd = dict() ddd['age'] = 21 ddd['course'] = 182 print(ddd) {'course': 182 , 'age': 21 } ddd['age'] = 23 print(ddd) {'course': 182 , 'age': 23 }

lst = list() lst.append( 21 ) lst.append( 183 ) print(lst) [21, 183] lst[0] = 23 print(lst) [23, 183] ddd = dict() ddd['age'] = 21 ddd['course'] = 182 print(ddd) {'course': 182 , 'age': 21 } ddd['age'] = 23 print(ddd) {'course': 182 , 'age': 23 }

[0] 21

[1] 183

lst

Key Value

['course'] 182

['age'] 21

ddd

Key Value

List

Dictionary

Most Common Name?

Most Common Name?

csev

zhen

zhen

marquard

zhen

cwen

csev

marquard

zhen

marquard

csev

cwen

zhen

Many Counters with a Dictionary One common use of dictionaries is counting how often we “see” something Key Value

ccc = dict() ccc['csev'] = 1 ccc['cwen'] = 1 print(ccc) {'csev': 1 , 'cwen': 1 } ccc['cwen'] = ccc['cwen'] + 1 print(ccc) {'csev': 1 , 'cwen': 2 }

Dictionary Tracebacks

  • It is an error to reference a key which is not in the dictionary
  • We can use the in operator to see if a key is in the dictionary

    ccc = dict() print(ccc['csev']) Traceback (most recent call last): File "", line 1, in KeyError: 'csev' 'csev' in ccc False

The get Method for Dictionaries

The pattern of checking to see if a key is already in a dictionary and assuming a default value if the key is not there is so common that there is a method called get() that does this for us if name in counts: x = counts[name] else : x = 0 x = counts.get(name, 0 ) Default value if key does not exist (and no Traceback).

{'csev': 2, 'zqian': 1, 'cwen': 2}

Simplified Counting with get()

We can use get() and provide a default value of zero when the key is not yet in the dictionary - and then just add one counts = dict() names = ['csev', 'cwen', 'csev', 'zqian', 'cwen'] for name in names : counts[name] = counts.get(name, 0 ) + 1 print(counts) Default

{'csev': 2, 'zqian': 1, 'cwen': 2}

Counting Words in Text

Writing programs (or programming) is a very creative and rewarding activity. You can write programs for many reasons ranging from making your living to solving a difficult data analysis problem to having fun to helping someone else solve a problem. This book assumes that everyone needs to know how to program and that once you know how to program, you will figure out what you want to do with your newfound skills. We are surrounded in our daily lives with computers ranging from laptops to cell phones. We can think of these computers as our “personal assistants” who can take care of many things on our behalf. The hardware in our current-day computers is essentially built to continuously ask us the question, “What would you like me to do next?” Our computers are fast and have vast amounts of memory and could be very helpful to us if we only knew the language to speak to explain to the computer what we would like it to do next. If we knew this language we could tell the computer to do tasks on our behalf that were repetitive. Interestingly, the kinds of things computers can do best are often the kinds of things that we humans find boring and mind-numbing.