Keyword that starts a loop that runs while a condition is true.
What is a while loop?
The Python data structure that is ordered, mutable, and allows duplicates.
What is a list?
The keyword that declares or defines a function in Python.
What is def?
The expression that define a class named Dog.
What is:
class Dog:
pass
The output of print(2 + 3 * 4)?
What is 14?
The output of:
if 5 > 3: print("Yes")
else: print("No")
What is "Yes"?
The built-in data type that is like a list but immutable.
What is a tuple?
Write a function multiply that accepts 2 numerical arguments and returns the product of those arguments.
def multiply(num1, num2):
return num1 * num2
A use case for __init__.
What is a constructor, which initializes a new object?
There is an indentation error in the code below, what is it?
if True:
print("Yes")
The print line must be indented under the if
Behavior that happens if you put break inside a for loop.
What is exits the loop immediately?
The expressions that returns 4
d = {'a': [1, 4], 'nums': {'num': 34, 'digit': 4}
What is a parameter vs an argument? How are they are related?
A parameter is variable in the definition of a function and an argument is a value passed when the function is called. Functions access argument as parameter in the order that they are received.
Create an instance of an existing class Dog, which expects a breed and a name.
goodGirl= new Dog("chihuahua", "chichi")
What happens if you try to do my_list[5] but my_list only has 3 elements?
An IndexError is thrown by the compiler
Translate the following pseudocode into Python:
A short statement that prints "Even" if n is divisible by 2, otherwise prints "Odd"
if n % 2:
print("Even")
else:
Print("Odd")
An expression that checks if a list "L" includes an element "x".
What is: x in L ?
How can a function use the returned value of another function? Give an example.
The outer function can return the inner function which has been called/invoked.
def outerFunc():
return innerFunc() // assuming innerFunc is defined
What self refers to inside a class method? Show how it can be used in a method.
What is returned when this function is called:
def show_me_output() :
for i in range(2):
print(i)
else:
print("Done")
None. Because the function has no return statement so by default the value None is returned.
The number of time will this print:
for i in range(2):
for j in range(3):
print(i, j)
+100 if you include the output.
What is 6 times?
Output: (0,0), (0,1), (0,2), (1,0), (1,1), (1,2)
2 differences and 2 similarities between a set and a dictionary?
Similarities: both can be represented using brackets {}, collections, Neither allow duplicate elements (sets will remove duplications automatically), both are mutable
Differences: Set stores values only, but dictionaries store key/value pairs (property), empty sets can only be defined using the set() function, but objects can be defined as empty brackets or the dict() function, Sets are unordered but dictionaries are ordered.
Explain inheritance in Python and give a simple example. (Answer: class Child(Parent): … – child inherits methods/properties of parent)
class Parent:
def get_family_name(self):
return self.family_name
class Child(Parent):
first_child = new Child()
child_family_name = first_child.get_family_name()
What is the result of running this code:
A.
def foo(a, b=5, c):
return a + b
print(foo(3))
print(foo(3, 2))
B.
def foo(a, b, c):
return a + b
print(foo(3))
print(foo(3, 2))
A. SyntaxError: parameter without a default follows parameter with a default.
B. TypeError: foo() missing 2 required positional arguments: 'b' and 'c'