Python Flashcards
Why do long integers take longer to process in Python than in most other languages?
Because they are stored internally as an array of digits.
What is the difference between double quoted strings and single quoted strings in python?
There is no difference.
Are escape characters valid in Python?
Yes.
How are invalid escape characters treated in Python?
They treated like regular text.
How can escape characters be turned off in Python?
By prefacing the string with an r (to specify a raw string).
Do multi line strings require triple single-quotes or triple double-quotes?
Either.
When does the interpreter automatically add new line characters to multi line strings?
Always, unless the line ends in a backslash.
Does prefacing a multi line string in python with ‘r’ prevent the interpreter from adding new line characters?
No.
Does python provide bounds checking for when indexing strings?
Yes.
What is the output of the following?
x = “carlisle”
print(x[2:3])
r
(since slices use inclusive/exclusive format)
What is the output of the following?
x = “car”
print(x[::-1])
rac
What is the output of the following?
x = “bugman”
print(x[2:44])
gman
(valid, despite apparent out of bounds)
What is the output of the following?
x = “bugman”
x[2] = “s”
print(x)
This issues an error, since strings are immutable in Python.
Is the following valid, where x is a list of length 4?
print(x[2:99])
Yes.
How are lists in Python implemented at the memory level?
As dynamically resized arrays of pointers.
Why aren’t lists in Python implemented as linked lists at the memory level?
Because operations on linked lists (especially indexing) is much too slow.
What is the asymptotic complexity of accessing / updating a list element by index?
O(1)
What is the asymptotic complexity of appending to a list?
O(1) if space is available, O(n) otherwise.
What is the asymptotic complexity of inserting into a list?
O(n)
What is the asymptotic complexity of building a list by inserting every element into the first position?
O(n^2)
How can you attempt to access a key’s value in a dictionary, without raising a KeyError exception should that key not exist?
By using dict.get(key), over dict[key].
What is the (amortized) cost of a lookup in a dictionary?
O(1), as there is a constant number of steps in executing the hash function.
How are tuples of 1 element created?
With a trailing comma.
What are the 2 primary differences between sequences and sets in python?
Unlike sequences, sets are unordered and do not allow duplicates.