1/25
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
can functions read global variables without any special declarations
yes, global variables can be refered to just by their name
global variables can be accessed anywhere
is this code allowed:
def my_func():
print(“in my_func()”, num1)
yes this is allowed
What about if you want to modify a global variable within a function
you have to declar it with the global keyword
this tells python that you want to modify the variable to be global and not create a new local scope of the variable
give an example of making a local variable a global variable
num1 = 10
def my_func():
num 1 = 50 (this is a local variable underneath my_func():
print(in my_func()”, num1) refers to the local variable
def myfunc2():
print(“in my_func2()”, num1) refers to the global variable
num1 = 10
def my_func
global num1 (refers to global num1)
num1 = 50 (resets num1 and sets it to 50)
This changes how you treat num1 and a local or global variable
why is it bad practice to use global variables in python?
much better to pass parameters and use return values
What are optional parameters
parameters that the programmer do not have to pass into thefunction but can if they want
Give an example of an optional parameter
What are the pass and none keywords
pass is a placeholder
#none is an instance of class Nonetype which is an object that indicates no value
Passing lists into functions
lists are a mutable data type and can be changed after they get created
when you pass a list into a function they are passed by referenced (references the original memory location)
so you dont need to return values out of the function
Give an example of passing a list into a function
def calc_sum(values)
total = 0
for element in values
total = total + element
return total
How do you access a dictionary via a key
dict[key]
needs to use closed brackets
how do you check if an item is in the dictionary by the key
if 123 in dict or
if 1234 not in dict
how do you get the number of items in a list
len(list)
not closed brackets
how do you add an item using the key
dict[list] = ‘Daniel’ where daniel is the term to be added
how do you delete items using a key
del students[123] where students is the dict and [123] is the key
how do you turn a dict into a list
list(dict.keys())
where it will only return the keys in your dict
how do you sort the dictionary by keys"?
by using the sorted(dict.keys())
how do you get all the keys in a dictionary
dict.keys()
how do you get al the items from a dict
dict.items()
how do you clear a dictionary
dict.clear()
what is the difference between a list and a dict?
a list contains a single element while a dictionary contains a key value pair
how do you access an element on a list if the list is my_list
my_list[element]
What do keys have to be?
immutable data type, unwise to use floats
python allows value sin a dictionary to be any type
(key, value)
how does dictionary overwriting work
the latest entry over writes all others because you can only have one unique key-value pair
how are dictionaries similar to lists
you can access items in a dictionary by its key not its index (in lists this is done via index)