Lecture 4 Flashcards
what does the sorted() function do?
The sorted() function returns a sorted list from the items in an iterable.
what is the syntax and parameters of the sorted() function?
sorted(iterable, key, reverse)
Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted. Key(optional): A function that would serve as a key or a basis of sort comparison. Reverse(optional): If True, then the iterable would be sorted in reverse (descending) order, by default it is set as False.
what is scope?
The region within which a variable is created and, therefore, available.
what does the global keyword mean?
allows us to modify the variable outside of the current scope (within a function usually)
ex:
c=1
def fxn():
global c
c=c+1
print(c)
output 2
how do you access a variable defined outside a function from within a function?
just name it. nothing else needed.
c=1
def fxn():
print(c)
output 1
can you modify a global variable from inside a function?
no
what is the nonlocal keyword?
used to declare a variable in a nested function and indicate that it is not a local variable.
This means that the variable is defined in the enclosing function but is not in the global scope
a variable inside a function is, by default, ______.
local
When we define a variable outside of a function, it is _______ by default. You don’t have to use the _______ keyword.
global
Global
what happens if you use the global keyword outside the functin?
nothing
what is *args?
Special operator we pass through functions
any number of extra arguments can be tacked on to your current formal parameters
Gathers remaining arguments as a tuple
what is *kwargs? (3)
A special operator we can pass to functions
Gathers remaining keyword arguments as a dictionary (you provide a name to the variable as you pass it into the function)
One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out.
what is the nonlocal keyword?
allows us to access and modify variables in the immediate outer scope of a nested function.
particularly useful when we want to update a variable’s value within the nested function without affecting its value in the global scope or other nested functions.
how do you pass each element of a list through *args?
name_of_function(*name_of_list)
NOTE that even though it is in list format, *args will treat the formatted list as though it is a dictionary, with key-value pairs [though they are not labeled])
how do you pass the keys in a dictionary using args/kwargs?
display_names(names) -> this would give an error
def display_names(first, second):
print(first,second)
return f”{first} says hello to {second}”
names = {“first”: “Ram”, “second”: “Ankush”}
print(display_names(**names) #to pass the values) # ram says hello to ankush
print(display_names(*names) #to pass the keys)
output