CSC 1010 Lecture 8 1
CSC 1010 Programming for All
Lecture 8 Dictionaries
What do we know so far?
- Class – lecture, lab, Rephactor, Quick Checks, R&R, easter eggs
- Solve problems, computers useful, user vs. programmer
- Sequence of instructions, algorithm is step‐by‐step
- Python is 3rd most popular language, core principles, always more than one way
- Syntax, runtime, & logic errors, testing & debugging, hardware vs. software
- Control flow – step‐by‐step, function call, conditional, loop
- IDLE shell, editor, install Python, Hello World
- Intrepreter, compiler, Python Standard Library
- Variables, assignment, numeric expr., precedence
- Print function, Strings, concatenation, indexes, in, *
- Interactive programs, if, if‐else, if‐elif‐else, int, float
- Boolean expressions: ==, !‐, <, <=, >, >=, not, and, or
- Input function, comparing strings, programming conventions
- Variable & function names lowercase, CONSTANTS, indent
- while, for, range, augmented assignments, palindromes
- Turtle Graphics, forward, left, right, pensize, pencolor, dot, circle
- goto, penup, pendown, fillcolor, begin_fill, end_fill, speed
- Calling & defining functions, import, parameters vs. arguments, return
- Positional args, default args, variable args, keyword args, local variables
- String methods, replace, method vs. function, built‐in & external functions
- Using loops and functions to create graphics, random numbers, design process
- Lists: indexing, iterating, concatenating, containment, repetition
- Membership: in & not in, Identity: is & is not, type checking
- List algorithms, list methods, slicing strings and lists, ex: finding a minimum
2
Dictionaries
A Python dictionary is an unordered, changeable, and indexed collection of keys and corresponding values.
3
The keys and values can be of any type. Often, each key are a single string or integer. The types of each value are more varied, such as strings, integers, and lists of multiple values.
Uses of Dictionaries
- Map a U.S. state name to its capital city.
- Map a membership number to the corresponding member.
- Map a sports team to the name of the city in which that
team is based.
- Map a word to its frequency count (how often that word is
found in some text).
- Map a zip code to the corresponding city name (or other
location description).
- Map a software version number to the year it was released.
- Map an English word to the corresponding French word.
- Map a manager to the list of employees that work for that
manager.
- Map a course id to the list of students taking that course.
4
Creating Dictionaries
You define a dictionary in Python with curly braces ({}):
5
capi t al s = { }
The dict function also works with named arguments:
{ ' Cal i f or ni a' : ' Sacr am ent o' , ' New Yor k' : ' Al bany' , ' M ar yl and' : ' Annapol i s' , ' Del awar e' : ' Dover ' } I know 4 st at e capi t al s!
Initializing a dictionary with keys and values works like this:
capi t al s = { ' M ar yl and' : ' Annapol i s' , ' New Yor k' : ' Al bany' , ' Cal i f or ni a' : ' Sacr am ent o' , ' Del awar e' : ' Dover ' } pr i nt ( capi t al s) pr i nt ( ' I know' , l en( capi t al s) , ' st at e capi t al s! ' )
Looking Up Values
The values in a dictionary can be accessed individually using a key inside the index operator.
6
cap = capi t al s[ ' New Yor k' ] pr i nt ( ' The capi t al of New Yor k i s' , cap) The capi t al of New Yor k i s Al bany
If the value is not found, a KeyError is reported. The safer way is using get, which returns a None value if the key is missing.
pr i nt ( " Del awar e' s capi t al i s" , capi t al s. get ( ' Del awar e' ) )
Del awar e' s capi t al i s Dover