Python Gotchas and Rules
The question sheet
Reveal any answer as you study-
When is an empty list default argument created?
- On import
- At definition
- Never
- Each call
Reveal answer
Answer: At definition
-
Calling add_item(1) then add_item(2) returns [1, 2] on the second call.
- True
- False
Reveal answer
Answer: True
-
To avoid the mutable default trap, use ____ as the default value.
- {}
- None
- []
- 0
Reveal answer
Answer: None
-
The mutable default problem applies to strings and tuples too.
- True
- False
Reveal answer
Answer: False
-
What does [10, 20, 30, 40][1:3] return?
- [20, 30]
- [30, 40]
- [20, 30, 40]
- [10, 20]
Reveal answer
Answer: [20, 30]
-
What does [10, 20, 30, 40][-1] give?
- 30
- 10
- -1
- 40
Reveal answer
Answer: 40
-
The slice ____ returns a list in reverse order.
- [::2]
- [::-1]
- [1:3]
- [:-1]
Reveal answer
Answer: [::-1]
-
Slicing modifies the original list in place.
- True
- False
Reveal answer
Answer: False
-
An out-of-range slice raises an error in Python.
- True
- False
Reveal answer
Answer: False
-
Which value is NOT falsy in Python?
- ""
- 0
- "hi"
- []
Reveal answer
Answer: "hi"
-
The operators and/or return one of their ____ rather than a strict boolean.
- operands
- errors
- indices
- types
Reveal answer
Answer: operands
-
What does the LEGB rule stand for?
- Local Extern Global Base
- List Enum Global Byte
- Level Enclosing Group Block
- Local Enclosing Global Built-in
Reveal answer
Answer: Local Enclosing Global Built-in
-
To assign to a global variable inside a function, use the ____ keyword.
- extern
- static
- global
- nonlocal
Reveal answer
Answer: global
-
Which keyword modifies an enclosing function's variable?
- local
- enclose
- global
- nonlocal
Reveal answer
Answer: nonlocal
-
Assigning inside a function without 'global' changes the outer variable.
- True
- False
Reveal answer
Answer: False
Make your own — free
Turn any notes into a game in under a minute. Free to start.