What is the evaluation of the following line in Python ?
6 + 8 / 2
10.0, not 10.
What is the evaluation of the following line in Python ?
3 // 2 * 4
4, not 4.0. The operand // tells how many times a number in another. 2 enters 1 time in 3
What is the output of the following Python expression?
17 % 5
2
5 × 3 = 15
17 − 15 = 2
So the remainder is 2, and that’s what the % operator returns.
What is the result and type of the following Python expression?
12 / 3
Result: 4.0
Type: float
Explanation:
In Python, the / operator always performs true division, which means it always returns a floating-point number, even when the division is exact.
When should you use single quotes '...', double quotes "...", or triple quotes """...""" in Python ?
Use single '...' or double "..." quotes for normal one-line strings — choose whichever avoids needing escape characters.
Use triple quotes """...""" or '''...''' for multi-line strings or docstrings.
Example:
‘Hello’ — single-line string
“He said ‘Hi’” — avoids escaping
”"”This is a multi-line string””” — spans multiple lines or serves as a docstring
What does the :: operand do when used with a Python list?
The :: is used in list slicing to specify a step (stride). It allows you to:
Examples:
numbers = [0,1,2,3,4,5,6,7,8,9]
numbers[::2] # [0, 2, 4, 6, 8] → every 2nd elementnumbers[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] → reverses the list
:: is shorthand for [start:stop:step] with start and stop optional.
When should you use a tuple vs a list in Python?
List: use when you need a mutable sequence (can change elements).
**Tuple: **use when you need an immutable sequence (fixed, cannot change).
Also
List: usually for similar items (numbers, names, etc.)
Tuple: often for heterogeneous data (a record: name, age, id)
In Python, what does the second argument in dict.pop(key, 0) represent?
The value that should be returned if the key is not found in the dictionary.
What’s the difference between and and & in Python?
and → logical AND, short-circuits, safe for all truthy/falsy values.
& → bitwise AND, always evaluates both sides, may error with non-booleans.
x = 5; y = Nonex > 0 and y > 0 ✅ safe(x > 0) & (y > 0) ❌ TypeError
In Python, can you use AND instead of and?
No. Python is case-sensitive:
and → valid logical AND
AND → invalid, causes SyntaxError
What does True and False evaluate to in Python?
False — and returns True only if both operands are True.
What does True or False evaluate to in Python?
True — or returns True if at least one operand is True.
By default, what does for key in my_dict: iterate over in a Python dictionary?
The keys of the dictionary.
How do you iterate over only the values of a dictionary?
Use the .values() method:
for value in my_dict.values():
print(value)How do you iterate over both keys and values of a dictionary?
Use the .items() method:
for key, value in my_dict.items():
print(key, value)In Python, what does mode=”at” do when opening a file with open("example.txt", mode="at")?
a → append: adds content to the end of the file without deleting existing content.t → text mode: reads/writes strings (default mode).
If the file doesn’t exist, it is created automatically.
What is the purpose of the **mode** parameter in Python’s **open()** function?
Determines how the file is opened: reading, writing, appending, or binary/text.
Common options:
Can combine letters, e.g., 'at' = append in text mode.