Functions & Abstraction
Fruitful Functions
Recursion
Lists/Arrays
Tuples & Dictionaries
Strings
100

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.

100

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.

100

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.

100

How many elements (indices) are in this list? What is its last element?
myList = ['dog', 'cat', 'fish', 'lizard']

4 elements; last is  'lizard'

100

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.

100

Count how many times the letter 'a' appears:
word = 'banana'

Use word.count('a')  #result is 3

200

Here's a function header. Fill in the body so it prints 'Welcome, <name>!':

def welcome(name):

def welcome(name):
     print(f"Welcome,  {name}!")

200

Complete the function to return the average of two numbers:

def average(a, b):

def average(a, b):
     return (a + b) / 2

200

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.

200

What will this code output?
myList = [10, 20, 30]
myList[1] = 50
print(myList)

[10, 50, 30]

200

Create a dictionary  to store these student scores: Alice: 91, Bob: 87, Chris: 78

{'Alice': 91, 'Bob':  87, 'Chris': 78}

200

What does this return? Why?
msg = 'hello world'
print(msg.find('z'))

-1 because 'z'  isn't in the string.

300

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.

300

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.

300

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

300

What will the slice return?
myList = [0, 1, 2, 3, 4, 5]
print(myList[2:5])

[2, 3, 4]

300

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.

300

What does this print?
msg = 'python'
print(msg[1:4])

'yth' #index 1 to before 4

400

This function is called inside another. What is the output?

def shout():
    print('HEY!')

def greet():
    print('Hello')
    shout()

greet()

Hello
HEY!

400

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

400

Why does this recursive function crash?

def broken(n): 
     return n + broken(n - 1)

No base case to stop  recursion. It runs forever.

400

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

400

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: 

400

Why does this cause an error?
name = 'Sam'
name[0] = 'B'

Strings are immutable; you can't assign to a character.

500

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.

500

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'.

500

Complete this recursive factorial function:

def factorial(n):

def factorial(n):
     if n == 0: 
          return 1
     else: 
          return n * factorial(n - 1)

500

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.

500

You have the following tuple:
t = (150, 250)

How could you convert this tuple into a dictionary?

d = dict([t])

500

How do you check if  two strings are exactly equal in Python?

Use the == operator:  

string1 == string2