sweigart chapter 4 Flashcards

1
Q

list

A

a value that contains multiple values in an ordered sequence. a list begins with an opening square bracket and ends with a closing square bracket.

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

list value

A

refers to the list itself, not the values inside the list.

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

items

A

values inside the list. they are separated with commas (comma-delimited).

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

index

A

the integer inside the square brackets that follows the list, eg. spam[0].

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

slice

A

can get several values from a list, in the form of a new list. it is typed between square brackets like an index, but it has 2 integers separated by a colon [:]. the first integer is the index where the slice starts, the second integer is where it ends. it goes up to but does not include the value at the second index.

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

del statement

A

will delete values at an index in a list. all of the values in the list after the deleted value will be moved up one index. it can also be used to delete a variable, as id it were an “unassignment” statement.

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

in and not in operators

A

can be used to determine whether a value is or isn’t in a list. they are used in expressions and connect two values: a value to look for in a list and the list where it may be found. these expressions will evaluate to a Boolean value.

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

multiple assignment trick/tuple unpacking

A

a shortcut that lets you assign multiple variables with the values in a list in one line of code. the number of variables and the length of the list must be exactly equal.

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

enumerate () function

A

on each iteration of the loop, the function will return two values: the index of the item in the list and the item in the list itself. the function is useful if you need both the item and the item’s index in the loop’s block. it can be used instead of the range(len(someList)) technique with a for loop.

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

random.choice() function

A

will return a randomly selected item from the list.

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

random.shuffle() function

A

will reorder the items in a list. this function modifies the list in place, rather than returning a new list.

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

augment assignment operator

A

eg. +=, which can be used as a shortcut.

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

method

A

the same thing as a function, except it is “called on” a value. each data type has its own set of methods. eg. if a list value were stored in spam, you would call the index() list method on that list like so: spam.index(‘hello’).

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

append() method

A

adds an argument at the end of the list.

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

insert() method

A

an insert a value at any index in the list. the first argument to insert() is the index for the new value, and the second argument is the new value to be inserted.

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

remove() method

A

removes value from the list. if the value appears multiple times in the list, only the first instance of the value will be removed.

17
Q

index() method

A

used to find a value in a list. if the value exists in the list, the index of the value is returned. if not, Python produces an error.

18
Q

sort() method

A

used to sort the number values or strings in a list. values can also be sorted in reverse order by passing True for the reverse keyword argument.

19
Q

ASCIIbetical order

A

means that uppercase letters come before lowercase letters. this is used for sort().

20
Q

str.lower

A

passed for the key keyword argument in a sort() method call to sort values in regular alphabetical order.

21
Q

reverse() method

A

used to reverse values in a list.

22
Q

line continuation character ()

A

used to split up a single instruction across multiple lines. it says “This instruction continues on the next line”.

23
Q

string

A

can be considered as a “list” of single test characters.

24
Q

mutable data type

A

can have values added, removed, or changed, eg. a list value.

25
Q

immutable data type

A

cannot be changed, eg. a string.

26
Q

tuple data type

A

almost identical to the list data type, except (1) they are typed with parentheses instead of square brackets, and (2) they are immutable. if you only have one value in your tuple you can indicate this by placing a trailing comma (,) after the value inside the parentheses.

27
Q

variables

A

technically, variables store references to the computer memory locations where values are stored.

28
Q

id() function

A

used to obtain the unique identity of values. the numeric memory address where the string is stored is returned by the id() function. this address is based on which memory bytes happen to be free on your computer at the time, so it’ll be different each time you run this code.

29
Q

append() method

A

changes the existing list object and does not create a new list object. this is called “modifying the object in-place”.

30
Q

automatic garbage collector

A

Python has an automatic garbage collector that deletes any values not being referred to by an variables to free up memory.

31
Q

copy() function

A

copy.copy() can be used to make a duplicate copy of a mutable value like a list or dictionary, not just a copy of a reference. this way, variables will refer to separate lists.

32
Q

deepcopy() function

A

copy.deepcopy() is used instead of copy.copy() if the list you need to copy contains lists.