What’s the difference between a string and an integer in Python?
Strings are sequences of characters, while integers are whole numbers. You can't do math with strings unless you convert them.
What does this print and why? `print("4" * 3)`
`444` — because it's string multiplication, not math.
Write code that prints all even numbers from 1 to 20 using a `for` loop.
Use a `for` loop: `for i in range(2, 21, 2): print(i)`
What’s the bug? `numbers = [1, 2, 3, 4] for i in range(len(numbers)): print(numbers[i+1])`
Index error — `i+1` goes out of bounds on the last loop.
Write a function that returns True if a number is prime.
Loop from 2 to sqrt(n) to check divisibility. Return False if divisible.
Name 3 data types in Python and describe how they're used.
Int (whole numbers), str (text), list (ordered group of items).
What’s the logic behind this condition? `if not (age >= 13 and age <= 19):`
It's checking if the age is NOT between 13 and 19 — aka not a teenager.
Ask the user for their name. If it starts with "A", print "Awesome!" Else, print "Nice to meet you."
name = input("What’s your name? ")
if name[0].upper() == 'A':
print("Awesome!")
else:
print("Nice to meet you.")
Predict the output: `x = 10 def change(x): x = x + 5 change(x) print(x)`
It prints 10 — the change to x inside the function doesn’t affect the original.
Create a function that returns the highest number in a list (don’t use `max()`).
Loop through the list, keep track of max value manually.
What does `len()` do, and when would you use it?
`len()` returns the number of items in a list or the number of characters in a string.
When would you use a `while` loop instead of a `for` loop?
`While` loops are better when you don't know how many times you'll loop. `For` loops are for fixed ranges.
Code a function that checks if a number is divisible by both 3 and 5.
Use modulo: `if num % 3 == 0 and num % 5 == 0:`
Why doesn’t this work right? `for letter in "banana": if letter == "a": count += 1 print(count)`
The `print(count)` is indented inside the loop — it should be outside to only print once.
Code a number guessing game (1–10 range, 3 tries max).
Use `random.randint()`, loop 3 times with input and `if` check.
What's a syntax error vs. a runtime error?
Syntax error = your code breaks the rules of the language. Runtime error = code runs but crashes while executing.
Explain what this function is doing: `def mystery(x): return x[::-1]`
It reverses a string using slicing. `[::-1]` flips the string.
Keep asking the user for numbers until they enter `-1`, then print the average (excluding `-1`).
Use a `while` loop, keep a counter and total, break at -1, then compute average.
Why might this loop crash? `i = 0 while i < 5: print(i)`
There's no `i += 1`, so it loops forever.
Create a password checker (minimum eight characters, must include both numbers and letters).
Use `input()`, check `len()`, use `any()` with `.isalpha()` and `.isdigit()`.
Explain the difference between a list and a dictionary.
Lists are ordered and use indexes. Dictionaries use key-value pairs and don't rely on position.
Why does this function sometimes give an error? `def divide(a, b): return a / b`
It gives an error if b = 0 — because you can't divide by zero.
Write a function that returns the number of vowels in a string.
Loop through string, count characters if they’re in 'aeiou'.
Debug this: `def square_all(nums): for n in nums: n = n ** 2 return nums`
You modified `n`, but didn’t update the list. Use indexing or list comprehension.
Turn a sentence into “leet speak” (replace letters with numbers like: a→4, e→3, i→1, o→0, etc.)
Use a dictionary to map each target letter to its leet version, then loop through the sentence and build a new string by replacing letters using the dictionary.
def to_leet(sentence):
leet_map = {'a': '4', 'e': '3', 'i': '1', 'o': '0'}
result = ''
for char in sentence:
result += leet_map.get(char.lower(), char)
return result