Week2Session5 - Lists and Dictionaries2
Week2Session5 - Lists and Dictionaries2
Introduction to Programming
Grace Tan
[email protected]
1-1
Outline
1. Lists
4. Dictionaries
1-2
Lists
A list is a sequence of data values called items or elements.
Some real-world examples of lists:
◦ A shopping list for the grocery store
◦ A to-do list
◦ A roster for an athletic team
◦ A guest list for a wedding
◦ A recipe, which is a list of instructions
◦ A text document, which is a list of lines
◦ The words in a dictionary
◦ The names in a phone book
Each item in a list has a unique index that specifies its
position.
Index of the first item is 0
Index of the last item is the length of the list minus 1
1-3
List Literals and Basic Operators
A list is written as a sequence of data values separated by
commas.
1-4
List Literals and Basic Operators
Build lists of integers using the range and list functions
1-5
List Literals and Basic Operators
Concatenation (+) and equality (==)
1-6
List Literals and Basic Operators
1-7
Replacing an Element in a List
subscript operator is used to replace an element at a given
position
1-8
Replacing an Element in a List
1-9
Replacing an Element in a List
Example 2:
replaces the first three elements of a list with new ones
1-
10
Inserting and Removing Elements
Example 3:
1-11
Inserting and Removing Elements
Example 4:
append and extend
adds the new element
to the end of the list
1-
12
Searching and Sorting
Search a list use the in operator to test for presence and then
the index method if this test returns true
1-
13
Aliasing and Side Effects
Do you notice any thing interesting/strange thing?
1-
14
Aliasing and Side Effects
prevent aliasing: create a
new object and copy the
contents of the original to
it.
1-
15
Tuples
A tuple is a type of sequence that resembles a list, except that,
unlike a list, a tuple is immutable. Enclose tuple elements in
parentheses instead of square brackets
7
7
3
[5, 3]
False
[5, 3, 7, 2, 10, 5]
(5, 3, 7)
1-
17
Outline
1. Lists
4. Dictionaries
1-18
Simple Function Definitions
A function consists of a header and a body.
1-
19
Simple Function Definitions
Example 5:
average value in a list of numbers.
1-
20
Simple Function Definitions
Example 6: Boolean function
1-
21
Defining a main Function
A special function named main that serves as the entry point
1-
Introduction 22
Function Definition Practice
Class Practice 2:
Define a function named even. This function expects a number
as an argument and returns True if the number is divisible by 2,
or it returns False otherwise
Sample Solution:
1-
23
Outline
1. Lists
4. Dictionaries
1-24
Case Study: Generating Sentences
Request: Write a program that generates sentences.
Analysis: simple grammar rules for our example
1-
25
Case Study: Generating Sentences
Design:
Function for Sentence: Noun phrase and Verb phrase are
concatenated
1-
27
Case Study: Generating Sentences
main function drives the program with a count-controlled loop
1-
28
Case Study: Generating Sentences
Implementation
1-
29
Outline
1. Lists
4. Dictionaries
1-30
Dictionaries
A dictionary organizes information by association,
its data structure is called tables or association lists
1-
31
Adding Keys and Replacing Values
creates an empty dictionary and adds two new entries
1-
32
Accessing Values
subscript to obtain the value associated with a key.
tuple of variables can then access the key and value of each entry
in this list within a for loop
1-
35
Dictionary Operations
1-
36
Dictionary Practice
Class Practice 3:
35
Nothing (return None with print function)
2
dict_keys(['b', 'a'])
dict_values([20, 35])
20
{'a': 35}
1-
37
References
This lecture was developed based on Chapter 5 in:
1-
Introduction 38