What is a list comprehension?
A concise way to create lists.
squares = [x**2 for x in range(10)]
How do you handle exceptions in Python?
Use try, except, and optionally finally.
try:
result = 10 / 0
except ZeroDivisionError:
result = None
How do you use the set data type?
A collection of unique elements.
unique = set([1, 2, 2, 3])
How do you use the zip function?
Combine multiple iterables into tuples.
for a, b in zip([1, 2], [3, 4]):
print(a, b)
How do you use the global keyword?
Declare that a variable is global inside a function.
count = 0
def increment():
global count
count += 1
How do you use the sorted function with a custom key?
names = [“bob”, “alice”, “carol”]
sorted_names = sorted(names, key=lambda x: x.lower())
How do you use the filter function?
Filter items in an iterable based on a condition.
evens = list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4]))
How do you use the map function?
Apply a function to every item in an iterable.
result = list(map(str.upper, [“a”, “b”, “c”]))
How do you use the enumerate function?
Get both index and value when looping.
for i, value in enumerate([10, 20, 30]):
print(i, value)
How do you use the isinstance function?
if isinstance(x, dict):
…
How do you use the getattr function?
Safely get an attribute from an object, with a default.
email = getattr(user, “email”, None)
How do you handle missing data in pandas?
Use .dropna() to remove, or .fillna() to replace.
df = df.dropna()
df = df.fillna(0)
How do you rename columns in a pandas DataFrame?
df = df.rename(columns={“A”: “Alpha”, “B”: “Beta”})
How do you iterate over rows in a pandas DataFrame?
Use .iterrows() or .itertuples().
for idx, row in df.iterrows():
print(row[“A”])
How do you select rows in a pandas DataFrame based on a condition?
filtered = df[df[“A”] > 1]
How do you check if a key exists in a dictionary?
Use the in keyword.
if “name” in my_dict:
print(my_dict[“name”])