Python: basics
8 questions, about 3 min
Questions and answers
- What does `print(7 // 2)` print?
- 3 ✓
- 3.5
- 4
- 2
The `//` operator is floor division: the fractional part is dropped.
- Which literal creates a list?
- (1, 2, 3)
- {1, 2, 3}
- {"a": 1}
- [1, 2, 3] ✓
Square brackets make a list. Parentheses make a tuple, curly braces make a set or a dict.
- What does `for i in range(3): print(i)` print?
- 1, 2, 3
- 0, 1, 2, 3
- 0, 1, 2 ✓
- 1, 2
`range(3)` starts at zero and does not include 3.
- Which keyword defines a function?
- function
- def ✓
- func
- fn
Python defines functions with `def`.
- What does `d["a"]` return if `d = {"a": 1}`?
- 1 ✓
- "a"
- KeyError
- None
Indexing a dict by key returns the value stored under it.
- What does `nums.append(4)` do to `nums = [1, 2, 3]`?
- Adds 4 to the start
- Replaces all items with 4
- Removes 4 from the list
- Adds 4 to the end: [1, 2, 3, 4] ✓
`append` changes the same list and puts the item at the end.
- What does `print("ab" * 3)` print?
- ab3
- ab ab ab
- ababab ✓
- An error
Multiplying a string by a number repeats it.
- What does `print(type(5 / 2))` print?
- <class 'int'>
- <class 'float'> ✓
- <class 'str'>
- <class 'double'>
Regular division `/` always gives a float, even when it divides evenly.