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
Q

Aliases

A

2 variables refer to the same list object
ie:
first = [10, 20, 30]
second = first

26
Q

How can you prevent aliasing?

A

Create a new object and copy contents of original
ie:
third = []
for element in first:
third.append(element)

27
Q

== operator

A

-use to see whether 2 variables refer to the exact same object or to different

28
Q

Object Identity

A

when the == operator returns True if the variables are aliases for the same object

29
Q

Structural Equivalence

A

Unfortunately, when the == operator returns True if the contents of 2 diff objects are the same

30
Q

Tuple

A

-Resembles a list but is immutable
-indicated by enclosing elements in ()
ie:
fruits = (“apple”, “banana”)

31
Q

Syntax of simple function definitions

A

-consists of a header and body
-syntax:
def <function>(<parameter-1>, ..., <parameter-n>):</parameter-n></parameter-1></function>

<body>
</body>

32
Q

parameter and arguments

A

-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
Q

Return statement

A

-place at each exit pint of a function when function should explicitly return a value
-syntax:
return <expression></expression>

34
Q

what happens if a function contains no return statement?

A

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
Q

Boolean functions

A

-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
Q

main function

A

-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
Q

Dictionaries

A

-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
Q

Dictionary Literals

A

-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
Q

dictionary pairs are sometimes called___

A

entries

40
Q

T or F: Keys in a dictionary can be data of any mutable types

A

F: Keys in a dictionary can be data of any immutable types, including other data
structures
-they are normally strings and integers

41
Q

How do you add a new key/value pair to a dictionary?

A

-use []
<a>[<a>] = <a>
-ie:
IN: info = {}
IN: info[“name”] = “Sandy”
IN: info[“occupation”] = “hacker”
IN: info
OUT: {‘name’:‘Sandy’, ‘occupation’:‘hacker’}</a></a></a>

42
Q

What do you use to replace a value at an existing dictionary key?

A

[]
ie:
IN: info[“occupation”] = “manager”
IN: info
OUT: {‘name’:‘Sandy’, ‘occupation’: ‘manager’}

43
Q

Dictionaries- how would you obtain a value associated with a key

A

-Use []
ie:
IN: info[“name”]
OUT: ‘Sandy’
-if key is not present an error is raised

44
Q

Dictionaries- if the existence of a key is uncertain, test for it using the method____ or _____

A
  • has_key
  • get
    ie:
    if “job” in info:
    print(info.[“job”])
45
Q

To delete an entry from a dictionary, remove using the method___

A
  • pop
  • it expects a key and optional default value as args
    ie:
    IN: print(info.pop(“job”, None))
    OUT: None
46
Q

using for loop with dictionary

A
  • 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
Q

What is dictionary method items?

A
  • 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
Q

Entries are represented as ____ within the list

A
  • tuples
    for (key, value) in grades.items():
    print(key, value)
49
Q

___ the list first then traverse it to print the entries of the dictionary in alphabetical order.

A
  • sort
    theKeys = list(info.keys())
    theKeys.sort()
    for key in theKeys:
    print(key, info[key])