Python key terms and interview refresher
Use this section as a quick revision page before an interview or when a term comes up at work. The aim is to understand the idea well enough to explain it in plain language before memorising syntax.
Mutable vs immutableMutable objects can change after creation, such as lists and dictionaries. Immutable objects, such as strings, integers and tuples, cannot be changed in place.
List vs tupleA list is mutable and is used when values may change. A tuple is immutable and is useful for fixed collections or records.
== vs is== compares values. is compares object identity, meaning whether two names refer to the same object.
Iterable vs iteratorAn iterable can produce values one at a time. An iterator is the object that keeps track of the current position during iteration.
GeneratorA generator produces values lazily, usually with yield, so large sequences do not have to be stored fully in memory.
Shallow vs deep copyA shallow copy copies the outer container but shares nested objects. A deep copy recursively copies nested objects too.
loc vs ilocpandas loc selects by labels; iloc selects by integer positions.
OverfittingA model overfits when it learns training data too closely and performs poorly on new data.
Common interview questions
What is the difference between a list and a tuple?
Lists are mutable; tuples are immutable. Use a list when the collection may change, and a tuple when the values should remain fixed.
What is the difference between == and is?
== checks whether values are equal. is checks whether two references point to the same object in memory.
What is a Python generator?
A generator is an iterator that yields values one at a time instead of creating the entire result in memory.
What is the difference between loc and iloc in pandas?
loc uses labels and can use Boolean conditions; iloc uses integer positions.
How do you reduce data leakage in machine learning?
Split data before fitting preprocessing steps, fit transformations only on training data, and use pipelines so the same transformations are applied consistently.
Practical interview tests
These short tasks test whether you can apply the tool, explain your reasoning and validate the result. In a live exercise, say your assumptions aloud and check the output rather than rushing straight to syntax.
Remove duplicates from a list while preserving the original order.What it tests: basic data structures and readable Python
Answer: Use a set to remember values already seen while building a new list in the original order.
Why: Turning the list directly into a set removes duplicates, but this pattern makes the order-preservation requirement explicit.
values = [3, 1, 3, 2, 1, 4]
seen = set()
unique_values = []
for value in values:
if value not in seen:
seen.add(value)
unique_values.append(value)
print(unique_values) # [3, 1, 2, 4]
Return the mean score and record count for each category in pandas.What it tests: grouping and aggregation
Answer: Group by category and use named aggregations.
Why: This produces clear output names and avoids manual loops.
summary = (
df.groupby("category")
.agg(
mean_score=("score", "mean"),
records=("score", "size")
)
.reset_index()
)
How would you identify rows that did not match another table after a join?What it tests: join validation and data quality
Answer: Use a left merge with indicator=True and inspect rows marked left_only.
Why: Interviewers often care as much about how you validate a join as how you write it.
checked = df_left.merge(
df_right,
on="customer_id",
how="left",
indicator=True
)
unmatched = checked[checked["_merge"] == "left_only"]
A numeric column contains blanks and text such as 'unknown'. How would you prepare it?What it tests: defensive data cleaning
Answer: Convert with pandas.to_numeric(errors='coerce'), inspect the resulting missing values, then choose a treatment that matches the business meaning.
Why: A strong answer does not automatically replace everything with zero because zero may be a genuine value.
df["age"] = pd.to_numeric(
df["age"],
errors="coerce"
)
print(df["age"].isna().sum())
A model performs very well on training data but poorly on test data. What is happening?What it tests: machine-learning judgement
Answer: The model is probably overfitting: it has learned training-specific patterns that do not generalise.
Why: Useful follow-ups are cross-validation, simpler models, regularisation, leakage checks and more representative training data.