What is the syntax for a for loop in python?
for <variable> in <sequence>:
<code block>
What will this code print?
x = 5
y = 3
print(x + y)
print(x * 2)
8
10
What is the difference between a parameter and an argument?
Missing colon after the condition ":"
Write a function called double that takes one parameter (a number) and returns double that value.
def double(num):
return num * 2
Write the syntax for defining a function that takes two parameters and returns a value.
def function_name(parameter1, parameter2): <code>
return value
What will this code print?
count = 0
while count < 3:
print(count)
count += 1
print("Done")
0
1
2
Done
When should you use a while loop versus a for loop?
Write a function called is_even that takes an integer and returns True if the number is even, False if it's odd.
def is_even(num):
return num % 2 == 0
Explain the difference between local and global variables.
What will this code print?
def multiply(a, b):
result = a * b
return result answer = multiply(4, 5) print(answer)
print(multiply(3, 3))
20
9
Explain what "scope" means in programming. Why is it important?
What different types of scope
Scope refers to the region of a program where a variable can be accessed or modified.
The loop variable count is never updated, creating an infinite loop!
Write a program that asks the user for a number and prints all numbers from 1 to that number (inclusive) using a for loop.
user_num = int(input("Enter a number: "))
for i in range(1, user_num + 1):
print(i)
Write the complete syntax for a nested loop structure. Explain how the inner and outer loops interact and when you would use this pattern.
for outer_var in range(n):
for inner_var in range(m):
<code>
The inner loop completes ALL its iterations for EACH iteration of the outer loop.
What will this code print?
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
i=2, j=0
i=2, j=1
What is the difference between a void function and a value-returning function? Give an example of when you'd use each.
Answer: Void Function:
Value-Returning Function:
Find and fix ALL errors (syntax, logic, and potential runtime errors)
Write a program that asks the user for numbers until they enter -1 (sentinel value). Calculate and display the average of all numbers entered (excluding the -1).
total = 0
count = 0
num = int(input("Enter a number (-1 to stop): "))
while num != -1:
total += num
count += 1
num = int(input("Enter a number (-1 to stop): "))
if count > 0:
average = total / count
print(f"Average: {average}")
else:
print("No numbers entered")
Question: Explain the syntax and behavior of the range() function with all three possible arguments. Write examples showing range() with 1, 2, and 3 arguments. Then explain how you would use range() to generate: (a) numbers counting backwards, (b) only even numbers, (c) every third number.
range(start, stop, step)
for i in range(10, 0, -1):
print(i)
for i in range(0, 21, 2):
print(i)
for i in range(1, 21, 3):
print(i)
What will this code print?
total = 0
def calculate(x):
global total
total += x
return total * 2
result1 = calculate(5)
print(result1)
result2 = calculate(3)
print(result2)
print(total)
10
16
8
Explain the concept of "input validation" and write a complete example that validates user input for a number between 1 and 100 (inclusive).
Input Validation is the process of checking user input to ensure it meets certain criteria before using it in your program. This prevents errors and ensures data integrity.
Question: Write a menu-driven program that maintains a running total. The program should: