Methods and Functions Flashcards

python methods (44 cards)

1
Q

return a random integer between min and max inclusive

A

randint(min, max)

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

return a random integer between min and max-1 inclusive

A

randrange(min, max)

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

add a value to the end of a list

A

my_list.append(value)

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

remove the element at the index i from a list

A

my_list.pop(i)

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

remove the first element whose value matches

A

my_list.remove(‘abc’)

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

get the length of a list # of items in the list or in a set

A

len(list) len(set)

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

produce a new list by adding two lists together, order of definition is order in list

A

list1 + list2

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

find the element in the list with the smallest value

A

min(list)

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

find the element in the list with the largest value

A

max(list)

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

find the sum of all list elements (numbers only)

A

sum(list)

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

find the index of the first matching value in the list

A

list.index(value)

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

count the number of occurrences in a list

A

list.count(value)

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

add elements from one set to another set
add elements from one dictionary to another dictionary

A

set1.update(set2)
my_dict1.update(my_dict2)

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

add a value into the set

A

set.add(value)

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

remove value from a set. raises KeyError if not found

A

set.remove(value)

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

remove a random element from the set

A

set.pop()

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

clear all elements from a set

A

set.clear()

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

return a new set containing only the elements in common between set and provided sets

A

set.intersection(set_a, set_b, set_c)

19
Q

return a new set containing all of the unique elements in all sets

A

set.union(set_a, set_b, set_c)

20
Q

return a set containing only the elements of the set that are not found in any of the provided sets

A

set.difference(set_a, set_b, set_c)

21
Q

return a set containing only the elements that appear in exactly one of set_a or set_b

A

set_a.symmetric_difference(set_b)

22
Q

Adding new entries to a dictionary:

A

dict[k] = v: Adds the new key-value pair k-v, if dict[k] does not already exist.
Example: students[‘John’] = ‘A+’

23
Q

Modifying existing entries in a dictionary:

A

dict[k] = v: Updates the existing entry dict[k], if dict[k] already exists.
Example: students[‘Jessica’] = ‘A+’

24
Q

Removing entries from a dictionary:

A

del dict[k]: Deletes the entry dict[k].
Example: del students[‘Rachel’]

25
Add all items in [x] to list.
my_list = [5, 8] my_list.extend([4, 12]) [5, 8, 4, 12] Extends the list by appending all items from an iterable.
26
Insert x into list before position i.
my_list = [5, 8] my_list.insert(1, 1.7) [5, 1.7, 8]
27
Sort the items of list in-place.
my_list = [14, 5, 8] my_list.sort() [5, 8, 14]
28
Reverse the elements of list in-place.
my_list = [14, 5, 8] my_list.reverse() [8, 5, 14]
29
Returns a new sorted list from the items in the original list.
sorted(list)
30
Returns an iterator that accesses the elements of the list in reverse order.
reversed(list)
31
work on a copy of a list
my_list[:]
32
copy a dictionary
my_dict.copy()
33
empty all values from a list or dictionary
my_list.clear() my_dict.clear()
34
Returns a new dictionary with specified keys and a default value.
my_dict.fromkeys(keys, value)
35
Returns the value for the specified dictionary key, or None if the key does not exist.
my_dict.get(key, 'optional default if none exists')
36
Returns a view object of the dictionary’s key-value pairs.
my_dict.items() num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda, calories in num_calories.items(): print(f'{soda}: {calories}') Coke: 90 Coke_zero: 0 Pepsi: 94
37
Returns a view object of the dictionary's keys.
my_dict.keys() num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda in num_calories.keys(): print(soda) Coke Coke_zero Pepsi
38
remove the element associated with a specified key from a dictionary
my_dict.pop(key)
39
remove an abitrary key value pair from a dictionary
my_dict.popitem()
40
Return the value of the specified key if it exists, otherwise set it to a default value
my_dict.setdefault(key, value)
41
Returns a view object of the dictionary's values.
my_dict.values() num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for calories in num_calories.values(): print(calories) 90 0 94
42
iterate through a list using the list comprehension syntax
new_list = [expression for loop_variable_name in iterable] my_list = [10, 20, 30] list_plus_5 = [(i + 5) for i in my_list]
43
collect optional parameters into an arbitrary argument list for a function
*args def print_sandwich(bread, meat, *args): print(f'{meat} on {bread}', end=' ') if len(args) > 0: print('with', end=' ') for extra in args: print(extra, end=' ') print('') print_sandwich('sourdough', 'turkey', 'mayo') print_sandwich('wheat', 'ham', 'mustard', 'tomato', 'lettuce')
44
collect optional parameters associated with keywords into an argument list for a function
def print_sandwich(meat, bread, **kwargs): print(f'{meat} on {bread}') for category, extra in kwargs.items(): print(f' {category}: {extra}') print() print_sandwich('turkey', 'sourdough', sauce='mayo') print_sandwich('ham', 'wheat', sauce1='mustard', veggie1='tomato', veggie2='lettuce')