Chapter 5 - Lists and dictionaries Flashcards

1
Q

a list method to add something at the end of something else

A

scores.append(score) - adds score to the end of scores

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

a list method to remove something based on value (unlike del. which deletes based on position)

A

scores.remove(score) - removes score

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

sorts elements in a list (usually in ascending order - smallest values first)

A

sort()

reverse=True will sort in reverse order

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

reverse()

A

reverses the order of a list

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

count(value)

A

returns the number of occurrences of value

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

returns the first position number where value occurs

A

index(value)

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

inserts value at position i

A

insert(value, i)

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

pop([i])

A

returns value at position i and removes the value from the list. Providing the position number i is optional. Without it the last element in the list is removed and returned.

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

removes the first occurrence of value from the list

A

remove(value)

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

Name 2-3 reasons to use tuples instead of lists

A
  1. Tuples are faster than lists
  2. Perfect for creating constants
  3. Sometimes they are required
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

nested sequences

A

sequences inside other sequences

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
>>>name, age = ("Momchil", 21)
>>>print(name)
Momchil
>>>print(age)
21
A

unpacking (assigning each element to its own variable in a single line of code)

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

What do variables do?

A

they are references to values, like a name is a reference to a person (they don’t contain the person)

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

What is shared referencing?

A

Several variables referring to the same value (a change in the value results in a change in what all of the variables reference)

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

What would the string language = “Python” do?

A

Store the string “Python” in the computer’s memory and then create the variable language, which will refer to that place in the memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
>>> mike = ["khakis", "dress shirt", "jacket"]
>>> honey = mike[:]
>>> honey[2] = "red sweater"
>>> print(honey)
['khakis', 'dress shirt', 'red sweater']
>>> print(mike)
['khakis', 'dress shirt', 'jacket']
A

honey is assigned a copy of mike. honey does not refer to the same list, instead it refers to a copy. So a change to honey has no effect on mike.

17
Q

How do dictionaries store information

A

in items that consists of a key and a value