Quiz 2 Material Flashcards
creating a function
def name(parameters):
statements…
return something
evaluate
turn an expression into a value
argument
the value/expression we send to a function
parameter
name we use for the variables in the function (you see them in the header) that are assigned to the arguments
call
to use a function
function definition
the code of the function (starts with “def”)
return
exit the value we exited with
side effect
actions that a function does (beyond returning something) that affect the environment outside of the function
in a function, it is important to _____ a value instead of printing it
return
def print_a_number(num):
print num
f1 = print_a_number(7)
print f1
7
none
do values that only existed within a function still remain in the memory after the function call line was executed?
no, python removes the memory for the function and those values are gone
scope
- the area where the variable is recognized (or has meaning)
global variables
declared outside of a function
(have scope from where they are first created until the end of the program
local variables
declared inside of a function
(have scope from where they are first created until the end of the function definition, this includes parameters)
which variable will be used? -> if the variable name is on the left of an assignment operator, it doesn’t have a value yet and the global operator is not used
python treats it as a local variable
if the variable is assumed to have a value already:
- if a local variable by that name exists, the _____ variable will be used
- if no local variable by that name exists, the _____ variable will be used
- local
- global
you have to use the global keyword at the beginning of the function to ______ variables
modify
default parameters
def my_function(a, b=15, c=’dog’)
b and c
- gives us the option of providing an argument for a certain parameter
- it we don’t provide an argument for that parameter, the function uses the default argument instead
arguments: when you ___ a function
parameters: when you ___ a function
call
define
which one is the positional argument and which one is the named argument?
my_function(a=10)
my_function(10)
do they do the same thing?
first one: named argument
second one: positional argument
yes
libraries
groups of functions or pre-existing programs you can build from
what is the syntax for importing random
import random
what happens if we forgot to import and we use the a function within the library in our code?
there will be a name error that says that ‘—-‘ is not defined
what is the syntax for choosing a random integer from 1 to 10 after you have imported random?
random.randint(1, 10)