week 9 functions with lists Flashcards

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
Q

in-place modifications

A

elements can be added or removed at any time

26
Q

list methods

A

operations to add or remove elements to a list, lists can also be sorted or reversed

27
Q

insert method example

A

list.insert(1, ‘string’) –> adds new element, ‘string’, at index one

28
Q

for loop

A

can iterate over each element of a list

29
Q

index error

A

trying to access an index out of range

30
Q

enumerate()

A

function that iterates over a list and provides an iteration counter

31
Q

all(list)

A

true if there are no 0s in list

32
Q

any(list)

A

true if any element of list is true

33
Q

max(list)

A

returns max, same structure for min and sum

34
Q

list nesting

A

list of lists

35
Q

how do you index nested lists

A

my_list[0][0] –> returns 1st element of 1st list in my_list

36
Q

multidimensional data structure

A

data structure that contains more layers of data

37
Q

nested for loops

A

can be used to access elements of nested lists

38
Q

slice notation

A

read multiple items from list and create new one with desired elements

39
Q

stride

A

step size for indexing and accessing elements
my_list[start:end:stride]

40
Q

sorted()

A

sorts list and makes copy instead of modifying existing

41
Q

list method key

A

argument that specifies a function to be applied to each element before comparing

42
Q

how do you use sorted function with lowercase key?

A

sorted(names, key=str.lower)

43
Q

how do you use sorted function with reverse key?

A

sorted(names, reverse=True)