What is abstraction in programming, and how do functions support it?
Abstraction hides complex logic and lets you focus on key tasks. Functions support abstraction by packaging reusable code behind a name.
What is the difference between a function that prints a value and one that returns it?
Printing displays the result; returning gives the value back for reuse.
Explain in your own words what a recursive function does and when you might use one.
It calls itself to solve smaller versions of a problem, e.g., factorial.
How many elements (indices) are in this list? What is its last element?
myList = ['dog', 'cat', 'fish', 'lizard']
4 elements; last is 'lizard'
How is a tuple different from a list? What happens if you try to modify it?
Tuples are immutable. You get an error if you try to change them.
Count how many times the letter 'a' appears:
word = 'banana'
Use word.count('a') #result is 3
Here's a function header. Fill in the body so it prints 'Welcome, <name>!':
def welcome(name):
def welcome(name):
print(f"Welcome, {name}!")
Complete the function to return the average of two numbers:
def average(a, b):
def average(a, b):
return (a + b) / 2
What's the base case of this function?
def countdown(n):
if n == 0:
print('Blast off!')
else:
countdown(n-1)
The base case is when n == 0.
What will this code output?
myList = [10, 20, 30]
myList[1] = 50
print(myList)
[10, 50, 30]
Create a dictionary to store these student scores: Alice: 91, Bob: 87, Chris: 78
{'Alice': 91, 'Bob': 87, 'Chris': 78}
What does this return? Why?
msg = 'hello world'
print(msg.find('z'))
-1 because 'z' isn't in the string.
How does this function demonstrate code reuse?
def say_hello():
print('Hello!')
say_hello()
say_hello()
The same code runs twice with one definition, showing reuse.
Explain what happens here:
def square(x):
return x * x
def add_one(n):
return n + 1
result = add_one(square(3))
print(result)
square(3) returns 9;
add_one(9) returns 10;
result is 10.
Describe what's happening step-by-step:
def mystery(n):
if n == 1:
return 1
else:
return n + mystery(n-1)
print(mystery(4))
Adds 4 + 3 + 2 + 1 = 10
What will the slice return?
myList = [0, 1, 2, 3, 4, 5]
print(myList[2:5])
[2, 3, 4]
What does this return and why?
d = {'x': 1, 'y': 2}
print(list(d.items()))
[('x', 1), ('y', 2)] it shows key-value pairs as tuples.
What does this print?
msg = 'python'
print(msg[1:4])
'yth' #index 1 to before 4
This function is called inside another. What is the output?
def shout():
print('HEY!')
def greet():
print('Hello')
shout()
greet()
Hello
HEY!
Write a function called 'safe_divide' that returns the result of a / b. If b is 0, return 'undefined'.
def safe_divide(a,b):
if b == 0:
return 'undefined'
else:
return a / b
Why does this recursive function crash?
def broken(n):
return n + broken(n - 1)
No base case to stop recursion. It runs forever.
Update each value in this list so that it becomes double its original value:
nums = [1, 2, 3, 4]
Use a loop:
for i in range(len(nums)):
nums[i] *= 2
How can you avoid a KeyError when accessing a value in this dictionary?
my_dictionary = {'fruit': 'apple'}
print(my_dictionary['vegetable'])
Use
if "vegetable" in my_dictionary:
Why does this cause an error?
name = 'Sam'
name[0] = 'B'
Strings are immutable; you can't assign to a character.
Why does the following code give an error?
def greet():
message = 'Hi'
greet()
print(message)
'message' is local to greet() and doesn't exist outside it.
Why is this function not working as expected?
def calc_area(length, width):
area = length * width
result = calc_area(5, 2)
print(result)
It doesn't return anything, so result is None. Use 'return area'.
Complete this recursive factorial function:
def factorial(n):
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
What mistake might a student make here?
myList = []
for i in range(3):
myList.append(i)
print(myList)
No mistake here; it prints [0, 1, 2], but they might forget to initialize myList.
You have the following tuple:
t = (150, 250)
How could you convert this tuple into a dictionary?
d = dict([t])
How do you check if two strings are exactly equal in Python?
Use the == operator:
string1 == string2