Chapter 5 Flashcards

1
Q

List

A

-Sequence of data values (items or elements)
-Allows programmer to manipulate a sequence of data values of any types
-Examples: to-do list, shopping list, text document (list of lines)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Dictionary

A

-Organizes data values by association with other data values rather than by sequential position
-In Python, associates a set of keys with values
-example: the keys in Webster’s dictionary comprise the set of words while the associated data values are their definitions.
-other examples: phone books, address books, encyclopedias etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Each item in a list has a unique ___ that specifies its position (from____)

A

-index
-0 to length -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is a list mutable or immutable?

A

mutable
-elements can be inserted, removed, or replaced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A list maintains its ___ but its ___ (its length and contents) can change

A

-identity
-state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can a subscript operator be used in a list?

A

-To replace an element
ie. example[3] = 0
Here “0” replaces the element in position 3 of the list called example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you replace each number in a list with its square?

A

IN: numbers = [2, 3, 4, 5]
IN: for index in range(len(numbers)):
numbers[index] = numbers[index] **2
IN: numbers
OUT: [4, 9, 16, 25]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What method can be used to extract a list of the words in a sentence?

A

string method split
ie.
IN: sentence = “This example has five words.”
IN: words = sentence.split()
IN: words
OUT: [‘This’, ‘example’, ‘has’, ‘five’, ‘words.’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you convert a list of words to uppercase?

A

IN: for index in range(len(words)):
words[index] = words[index].upper()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

L.append(element)

A

adds element to the end of L.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

L.extend(aList)

A

Adds the elements of aList to the end of L.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

L.insert(index, element)

A

Inserts element at index if index is less than the length of L. Otherwise, inserts element at the end of L.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

L.pop()

A

Removes and returns the element at the end of L.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

L.pop(index)

A

Removes and returns the element at index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

in…

A

determines an element’s presence or absence, but does not return position of element (if found)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What method that is used with strings can’t be used to locate an element’s position in a list?

A

the find method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

If we can’t use the find method how can we search a list?

A

-use method index to locate an element’s position in a list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Since method index raises an exception when the target element is not found how can we guard against this?

A

-First, use the in operator to test for presence
-then the index method if this test returns True.
ie.
aList = [34, 45, 67]
target = 45
if target in aList:
print(aList.index(target))
else:
print(-1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

natural ordering

A

the placement of data items relative to each other by some internal criteria, such as numeric value or alphabetical value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

A list’s elements are always ordered by ____ but you can impose a ____ on them

A

-position
-natural ordering

21
Q

T or F: When the elements can by related by comparing them <, >, and == they cannot be sorted

A

F: When the elements can by related by comparing them <, >, and == they can be sorted

22
Q

Method sort

A

mutates a list by arranging its elements in ascending order
ie.
IN: l = [4, 2, 10, 8]
IN: l.sort()
IN: l
OUT: [2, 4, 8, 10]

23
Q

All functions and methods from previous chapters return a value that the caller can then use to complete its work. What kind of methods usually return no value of interest to caller?

A

mutator methods (e.g., insert, append, extend, pop, and sort)

24
Q

Since mutator methods usually return no value of interest to caller what special value does Python automatically return?

A

-None
ie:
IN: aList = aList.sort()
IN: print(aList)
OUT: None

25
Aliases
2 variables refer to the same list object ie: first = [10, 20, 30] second = first
26
How can you prevent aliasing?
Create a new object and copy contents of original ie: third = [] for element in first: third.append(element)
27
== operator
-use to see whether 2 variables refer to the exact same object or to different
28
Object Identity
when the == operator returns True if the variables are aliases for the same object
29
Structural Equivalence
Unfortunately, when the == operator returns True if the contents of 2 diff objects are the same
30
Tuple
-Resembles a list but is immutable -indicated by enclosing elements in () ie: fruits = ("apple", "banana")
31
Syntax of simple function definitions
-consists of a header and body -syntax: def (, ..., ):
32
parameter and arguments
-name used in function definition for an argument that is passed to the function when it is called -The number & position of arguments of a function call should match the # & positions of the parameters in the definition -some functions expect no arguments - defined with no parameters
33
Return statement
-place at each exit pint of a function when function should explicitly return a value -syntax: return
34
what happens if a function contains no return statement?
Python transfers control to the caller after the last statement in the function's body is executed -the special value None is automatically returned
35
Boolean functions
-Usually tests its argument for the presence or absence of some property -returns True if property is present; False otherwise ie: IN: odd(5) OUT: True
36
main function
-serves as entry point for a script -usually expects no arguments and returns no value -definition of main & other functions can appear in no particular order in the script as long as main is called at the end of the script -script can be run from IDLE, imported into the shell, or run from a terminal command prompt
37
Dictionaries
-Organizes info by association, not position -data structures organized by assoc are also called tables and association lists -in Python, a dictionary assoc a set of keys with data values
38
Dictionary Literals
-written as a sequence of key/value pairs separated by commas -enclosed in curly brackets ({ and}) -a colon (:) separates a key and value -ie: {‘Savannah’:‘476‐3321’, ‘Nathaniel’:‘351‐7743’} an empty dictionary: {}
39
dictionary pairs are sometimes called___
entries
40
T or F: Keys in a dictionary can be data of any mutable types
F: Keys in a dictionary can be data of any immutable types, including other data structures -they are normally strings and integers
42
What do you use to replace a value at an existing dictionary key?
[] ie: IN: info[“occupation”] = “manager” IN: info OUT: {‘name’:‘Sandy’, ‘occupation’: ‘manager’}
43
Dictionaries- how would you obtain a value associated with a key
-Use [] ie: IN: info["name"] OUT: 'Sandy' -if key is not present an error is raised
44
Dictionaries- if the existence of a key is uncertain, test for it using the method____ or _____
- has_key - get ie: if "job" in info: print(info.["job"])
45
To delete an entry from a dictionary, remove using the method___
- pop - it expects a key and optional default value as args ie: IN: print(info.pop(“job”, None)) OUT: None
46
using for loop with dictionary
- the loop's variable is bound to each key in an unspecified order - to print all of the keys and their values: for key in d: print(key, d[key])
47
What is dictionary method items?
- returns a list of tuples containing the keys & values for each entry - list(d.items()) - ie: IN: grades = {90:‘A’, 80:‘B’, 70:‘C’} IN: list(grades.items()) OUT: [(80, ‘B’), (90,‘A’), (70,‘C’)]
48
Entries are represented as ____ within the list
- tuples for (key, value) in grades.items(): print(key, value)
49
___ the list first then traverse it to print the entries of the dictionary in alphabetical order.
- sort theKeys = list(info.keys()) theKeys.sort() for key in theKeys: print(key, info[key])