Interview Q&A / Python
Python interview questions
Lists, dictionaries, generators, decorators and the Python questions asked in service-company and data roles.
Prefer rapid revision? Practise Python short interview answers →
- What is the difference between a list and a tuple in Python?
A list is mutable, so you can add, remove or change items after creating it. A tuple is immutable, so its contents are fixed once created. Tuples are slightly lighter and can be used as dictionary keys if all their items are hashable; lists cannot.
BeginnerIn-depth3 min read - Why is (5) not a tuple in Python, and how do you create a one-element tuple?
Parentheses alone only group an expression, so (5) is just the integer 5. The comma makes the tuple, so a one-element tuple is written (5,) or 5,. The empty tuple is ().
BeginnerQuick answer1 min read