sweigart chapter 4 Flashcards
(32 cards)
list
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.
list value
refers to the list itself, not the values inside the list.
items
values inside the list. they are separated with commas (comma-delimited).
index
the integer inside the square brackets that follows the list, eg. spam[0].
slice
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.
del statement
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.
in and not in operators
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.
multiple assignment trick/tuple unpacking
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.
enumerate () function
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.
random.choice() function
will return a randomly selected item from the list.
random.shuffle() function
will reorder the items in a list. this function modifies the list in place, rather than returning a new list.
augment assignment operator
eg. +=, which can be used as a shortcut.
method
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’).
append() method
adds an argument at the end of the list.
insert() method
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.
remove() method
removes value from the list. if the value appears multiple times in the list, only the first instance of the value will be removed.
index() method
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.
sort() method
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.
ASCIIbetical order
means that uppercase letters come before lowercase letters. this is used for sort().
str.lower
passed for the key keyword argument in a sort() method call to sort values in regular alphabetical order.
reverse() method
used to reverse values in a list.
line continuation character ()
used to split up a single instruction across multiple lines. it says “This instruction continues on the next line”.
string
can be considered as a “list” of single test characters.
mutable data type
can have values added, removed, or changed, eg. a list value.