week 9 functions with lists Flashcards

(43 cards)

1
Q

defining lists

A

can be hardcoded, list function, or from a file

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

mutable types in functions

A

list, dictionary, and set that are passed into function are also altered in the workspace

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

index method

A

allows you to search ordered object for certain value and returns the index

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

expense

A

the number of comparisons needed to find something (in reference to methods)

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

ordering/sorting data

A

increases efficiency because you know where stuff is and can select certain chunks to search for something

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

binary search

A

reducing search items by sorting data

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

scope

A

part of a program that variable/function exists

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

local variables

A

variables defined in functions, the scope is within the function

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

global variable

A

created outside function, scope is from defining statement until the end of the file

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

global statement

A

used to change a global value and create a global value inside a function (use keyword global, then state function name)

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

do modifications to mutable types within a function need a global statement?

A

no

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

function scope

A

from function definition until the end of the file

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

namespace

A

maps names to objects

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

locals()

A

prints all local variables

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

globals()

A

prints all global variables

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

3 nested scopes

A

built-in scope, local scope, global scope

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

built-in scope

A

contains built-in python names, like int, str, list, etc

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

scope resolution

A

process of searching for object name in available namespaces

19
Q

order of scope resolution

A

local, global, built-in

20
Q

pass-by-assignment

A

when the function is called, new local variables are created in the function’s local namespace, parameter names are binded to the passed arguments

21
Q

immutable objects

A

modifications are limited to function

22
Q

mutable object

A

modifications affect the full scope

23
Q

how do you avoid changes to mutable objects in functions?

A

create a copy of the object
ex:
copy = item_list[:]

24
Q

list

A

mutable container with sequential order, elements can be accessed through indexing

25
in-place modifications
elements can be added or removed at any time
26
list methods
operations to add or remove elements to a list, lists can also be sorted or reversed
27
insert method example
list.insert(1, 'string') --> adds new element, 'string', at index one
28
for loop
can iterate over each element of a list
29
index error
trying to access an index out of range
30
enumerate()
function that iterates over a list and provides an iteration counter
31
all(list)
true if there are no 0s in list
32
any(list)
true if any element of list is true
33
max(list)
returns max, same structure for min and sum
34
list nesting
list of lists
35
how do you index nested lists
my_list[0][0] --> returns 1st element of 1st list in my_list
36
multidimensional data structure
data structure that contains more layers of data
37
nested for loops
can be used to access elements of nested lists
38
slice notation
read multiple items from list and create new one with desired elements
39
stride
step size for indexing and accessing elements my_list[start:end:stride]
40
sorted()
sorts list and makes copy instead of modifying existing
41
list method key
argument that specifies a function to be applied to each element before comparing
42
how do you use sorted function with lowercase key?
sorted(names, key=str.lower)
43
how do you use sorted function with reverse key?
sorted(names, reverse=True)