Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
BlogTech

Pythonlearn:resources-week09

By techsupport
October 7, 2016 2 Min Read
Comments Off on Pythonlearn:resources-week09

Lecture Notes

Examples below come from Chapter 9 Video

List – a linear collection of ordered values.

Dictionary – an assortment of unordered labeled values, similar to a database

Dictionaries

Different names for Dictionaries:

  • Associative Arrays – Perl/Php
  • Properties of Map or HashMap – Java
  • Property Bag – C#/.Net

An example of working with Dictionaries (9A):

purse = dict()  

purse['money'] = 12  
purse['candy'] = 3  
purse['tissues'] = 75  
print purse  

Output:
{‘money’:12,’tissues’:75,’candy’:3} The output shows both the key(‘money’) and the value (’12’). The value can be modified, like with lists.
For example (9B):

purse['candy'] = purse['candy'] + 2  

Will change the value of ‘candy’ to 5.

Dictionaries are like Lists except that they use keys(‘money’) instead of numbers to look up values. (Lists are ordered, Dictionaries are not and so rely on keys for lookup)

Dictionary literals (constants) use curly braces {} and have a list of key : value pairs. As usual for dictionaries, the list is unordered.

Using Dictionary to tally/count items

The “in/not in” operators can be used to see if a key exists in the dictionary – without resulting in a traceback error.

Example (9C):

counts = dict()  
names = ['csev','owen','csev','zqian','cwen']  
for name in names:  
  if name not in counts:  
    counts[name]= 1  
  else:  
    counts[name] = counts[name] + 1  
print counts  

The above operation is so common, that the method ‘get’ does it for us.

print counts.get(name,0)  

Performs the same operation as:

if name in counts:  
  print counts[name]  
else:  
  print 0  

So, example 9C can be simplified down to (9D):

counts = dict()  
names = ['csev','owen','csev','zqian','cwen']  
for name in names:  
  counts[name] = counts.get(name,0) + 1  
print counts  

Few more methods to iterate a dictionary using example 9C’s dictionary

Iterate over keys: counts.keys():

for name in counts.keys():
  print name

Iterate over the values in a dictionary: counts.values():

for occurrence in counts.values():
  print occurrence

Iterate over the elements ((key, value) pairs) in a dictionary : counts.items():

for name,occurrence in counts.items():
  print name, occurrence

Exercises previously posted here have been moved and can be accessed through the Chapter 9 Exercises link below.

Chapter 9 Exercises

The above page is intended as a place for students to work out solutions and answers to the exercises from the textbook. Please do not post answers to exercises that are actual graded assignments.

More Resource Topics

Add resources for this chapter to this page..

Python Dictionaries – https://www.jeffknupp.com/blog/2015/08/30/python-dictionaries/

Author

techsupport

Follow Me
Other Articles
Previous

Pythonlearn:resources-week08

Next

Pythonlearn:resources-week10

Categories

  • Android
  • Blog
  • Favorite Link
  • linux
  • Open Source
  • opencart
  • social
  • Tech
  • Uncategorized

Archives

  • January 2025
  • March 2024
  • August 2023
  • March 2023
  • February 2023
  • November 2021
  • August 2021
  • April 2021
  • March 2021
  • December 2020
  • October 2020
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • September 2019
  • August 2019
  • June 2019
  • October 2018
  • August 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
Copyright 2026 — Technical Personnal Blog. All rights reserved. Blogsy WordPress Theme