week 8 intro to functions Flashcards
def keyword
defines the function, the statement has color at the end and indented function body
how can you customize a function?
add parameters so that numbers/vars can be passed as arguments in function call
3 steps of making a function
1) define (fun name, parameters, body)
2) call (pass arguments)
3) execute (use parameters to compute result)
find method
returns indices of first instance of a substring
string slicing
extracting certain characters (1st number inclusive, 2nd number exclusive)
split method
splits string into list based on specified delimeter
local variable
only exists in the function, the function is an ‘island’
global variable
exists and modified in main code
pass by value
parameters changed in function does not affect variables outside the function
return function
outputs value from function
copy paste error
forgetting to modify code after pasting into new environment
return error
accidentally returning the wrong variable or forgetting to add return statement
unpacking
operation that allows statement to perform multiple assignments at once (ex: assigning tuple/list to variable in one line)
index
integer matching specific position in ordered object (list, tuple, etc) starts at 0 and uses brackets to index
slice notation
allows selection of multiple indices per list (using colons to select all before, after, or in between integers) negative numbers means a step back from the end
slicing operation
if you assign a slice to a var and reassign the original string, the slice var will stay the same
stride
step size to increment index
my_str[0:10:2]
0 to 10 in steps of 2 (every other, starting at 0)
field width
defines minimum number of characters inserted in string, any extra space is padded with spaces to reach that minimum value