Q&A, Part II

CSCI 1360: Foundations for Informatics and Analytics

Reverse the keys and values in a dictionary (make the keys the values, and the values the keys).


In [ ]:

I define a child class. Instead of wholesale overriding parent behavior, however, I merely want to augment it. How can I do this without re-implementing code in the parent?


In [ ]:

How does Python resolve multiple inheritance collisions? (i.e. methods with the same name in multiple parent classes)

Name the two types of variable scope in Python. Name the three [four] specific environments which determine these scopes.


In [ ]:

What does the following code print:


In [ ]:
class Vehicle():
    def __init__(self, name):
        self.name = name
        print("I am a {}.".format(self.name))
        
    def drive(self, mileage):
        print("I drove {} miles.".format(mileage))

class Car(Vehicle):
    def drive(self, mileage, passengers):
        print("I have {} passengers.".format(passengers))
        super(Car, self).drive(mileage)
    
class BMW(Car):
    def __init__(self, model, year):
        self.model = model
        self.year = year
        super(BMW, self).__init__(model)

In [ ]:
bmw = BMW("Roadster", 2016)
bmw.drive(10, 3)

Write a single loop that simultaneously prints three lists which may or may not be the same length.


In [ ]:

Flip the type of loop you used in the previous question (if you used a for loop, use a while loop here to answer the same thing, and vice versa).


In [ ]:

What is "unpacking"? Give an example.


In [ ]:

How can you test if a specific value x is in a list? How can you test if a specific value x is NOT in a list?


In [ ]:

Explain the resulting data structure from the code below. Why would it be useful?


In [ ]:
import numpy as np

list1 = np.random.random(10).tolist()
list2 = np.random.randint(100, 10).tolist()

data_structure = enumerate(zip(list1, list2))  # What is this?

Draw and describe a list-of-lists that each contain lists (a 3D "cube").


In [ ]:

What happens in the following code?


In [ ]:
x = 2

def f(x):
    y = x ** 2
    x = x ** 2
    return y

y = f(x)
x = f(y)

# What is x?
# What is y?

What happens in the following code?


In [ ]:
def g(x, y):
    x[0] *= x[1]
    z = y.copy()
    z[0] *= x[1]
    return z

x = [1, 2]
y = [3, 4]
x = g(y, x)

# What are x and y?