Lecture 4 Flashcards

1
Q

what does the sorted() function do?

A

The sorted() function returns a sorted list from the items in an iterable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is the syntax and parameters of the sorted() function?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is scope?

A

The region within which a variable is created and, therefore, available.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does the global keyword mean?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do you access a variable defined outside a function from within a function?

A

just name it. nothing else needed.

c=1

def fxn():
print(c)

output 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

can you modify a global variable from inside a function?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the nonlocal keyword?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

a variable inside a function is, by default, ______.

A

local

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When we define a variable outside of a function, it is _______ by default. You don’t have to use the _______ keyword.

A

global
Global

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what happens if you use the global keyword outside the functin?

A

nothing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is *args?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is *kwargs? (3)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is the nonlocal keyword?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how do you pass each element of a list through *args?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how do you pass the keys in a dictionary using args/kwargs?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how do you format a float up to a certain number of decimal spaces?

A

print(““.format(2.567))

17
Q
A