Lesson 03
Interview Tips
Give a Python interview answer in three beats: a short definition, one example, and the pitfall that usually follows that topic.
Give a Python interview answer in three beats: a short definition, one example, and the pitfall that usually follows that topic.
Example
def reply(topic):
answers = {
"mutable": "A list can change after it is created. A tuple cannot.",
"lock": "One thread finishes the shared section before the next thread enters.",
"query": "Pass values beside the SQL so a value cannot change the statement.",
}
return answers[topic]
for topic in ("mutable", "lock", "query"):
print(topic + ":", reply(topic))Each line is one definition you can say out loud. A list can change. A lock lets one thread through the shared section. Query values stay outside the SQL text. The samples below use the same three beats.
Sample answers
How do you tell a list from a tuple?
A list can change after it is created. A tuple cannot. badges.append("West") changes the list. A tuple has no append. The pitfall is using a list as a dict key. It is not hashable. A tuple of unchanging values can be a key.
When do you use == and when do you use is?
== asks whether the values match. is asks whether both names point at the same object. Two empty lists compare equal and are not the same object. The pitfall is using is to compare numbers or strings. Use is for None.
What happens if a default argument is a list?
The default list is created once, when the function is defined, and later calls share it. Use None as the default and create a new list inside the function. The pitfall is a function that remembers items from the previous call.
How do you wait for a child thread?
join() pauses the caller until that child finishes. is_alive() is true while it runs. The pitfall is thinking a missing join() deletes the child. A normal child still runs. join() only orders the later lines.
How do you keep two threads from mixing one print?
Put the shared prints inside with lock. The lock is acquired for the block and released when the block ends. The pitfall is acquiring the lock and never releasing it. The next thread then waits forever.
How do you send a value in a query?
Write ? in the SQL and pass the value in a tuple. cursor.execute(sql, (code,)) keeps the value out of the statement text. The pitfall is gluing the value into the SQL string. A surprising value can change the statement.
Key takeaways
-
Definition, then one example, then one pitfall
-
Stop after those three beats
-
isis for identity, includingNone -
A shared default list remembers earlier calls
-
Next: What Is NumPy — arrays for data science
-
Previous: Question Bank
Loading playground…
Check your understanding
Interview Tips Quiz
What is the first beat of an answer on this page?
1 / 10