Define a variable called 's' that contains a new empty set
s = set()
Write a simple (but correct) if/elif/else
i = input()
if i == 'y':
print('yes')
elif i == 'n':
print('no')
else:
print('unknown')
False
In UML, a [_______] is used to specify whether a Derived Class extends from a Base class
What's an arrow?
The name of the code block that guarantees that some code will be executed even if there is some error being raised is [_______]
What is finally?
What's the double slash (//)?
for index, value in enumerate([3, 4, 5]):
print(f"{index}:{value}")
Names of the variables in local scope:
def count_dozens(num_apples):
dozens = num_apples // 12
remain = num_apples % 12
return dozens, remain
app = int(input())
d, r = count_dozens(app)
What's num_apples, dozens, remain?
Draw UML for any two classes to demonstrate what a 'has-a' relationship looks like
<drawing using a diamond arrow>
The output of this code:
def access(a_list, index, on_error):
try:
return a_list[index]
except:
return on_error
print(access([0], 5, "Ouch"))
What is ouch?
Given a list called abc, define a variable named xyz that contains a set of values present in abc, eliminating duplicates.
xyz = set(abc)
Use list comprehension to create a list of numbers 1 through 10 (inclusive) and store it in a new variable called nums.
nums = [i for i in range(1, 11)]
Code that prints the global scope of a program
print(globals())
The problem with this code:
class Alpha:
def sub(self):
print("Hello")
class Beta(Alpha):
def sub1(self):
print("Howdy")
print(Alpha().sub1())
last line: class Alpha can't access derived class Beta's sub1() method.
An example of code using a try/finally
def divide(a, b):
try:
return a / b
finally:
print("Done")
divide(5, 0)
Use the math module to compute the square root of a number provided by the user and print it.
import math
print(math.sqrt(float(input())))
The value of x is [____]:
x = False or not True or 4**2 >= 16 and "severe" > "serious"
True
The output of:
def abc(x=1, y=2):
print(y/x+2)
x=2
y=4
abc(y=x,x=y)
What's 2.5?
Define a class called Fork that derives from Utensil and contains two private attributes: size and color. The constructor for both classes (Fork and Utensil) has no parameters other than self
class Fork(Utensil):
def __init__(self):
Fork.__init__(self)
self.color = ""
self.size = 0
The problem with this code:
class Blank(Exception):
def __init__(self, msg):
Exception.__init__(self,msg)
The name of class Blank doesn't end with suffix 'Error' as promised between Python developers.