week 9 functions with lists Flashcards
(43 cards)
defining lists
can be hardcoded, list function, or from a file
mutable types in functions
list, dictionary, and set that are passed into function are also altered in the workspace
index method
allows you to search ordered object for certain value and returns the index
expense
the number of comparisons needed to find something (in reference to methods)
ordering/sorting data
increases efficiency because you know where stuff is and can select certain chunks to search for something
binary search
reducing search items by sorting data
scope
part of a program that variable/function exists
local variables
variables defined in functions, the scope is within the function
global variable
created outside function, scope is from defining statement until the end of the file
global statement
used to change a global value and create a global value inside a function (use keyword global, then state function name)
do modifications to mutable types within a function need a global statement?
no
function scope
from function definition until the end of the file
namespace
maps names to objects
locals()
prints all local variables
globals()
prints all global variables
3 nested scopes
built-in scope, local scope, global scope
built-in scope
contains built-in python names, like int, str, list, etc
scope resolution
process of searching for object name in available namespaces
order of scope resolution
local, global, built-in
pass-by-assignment
when the function is called, new local variables are created in the function’s local namespace, parameter names are binded to the passed arguments
immutable objects
modifications are limited to function
mutable object
modifications affect the full scope
how do you avoid changes to mutable objects in functions?
create a copy of the object
ex:
copy = item_list[:]
list
mutable container with sequential order, elements can be accessed through indexing