12 Python programs that keep showing up in analyst interviews.
First 8 are plain Python logic. Last 4 are pandas, the part that actually matters on the job.
Don't just read them. Type every line, run it, then explain it out loud like you're in the room.
Save this and code 2 a day.
Strings
Warm up. Everyone asks these.
- 01
Reverse a string
s = "analyst" print(s[::-1]) # tsylana - 02
Check a palindrome
def is_pal(s): s = s.lower() return s == s[::-1]
Lists
The logic round starts.
- 03
Top 3 most frequent words
from collections import Counter words = text.split() print(Counter(words).most_common(3)) - 04
Second largest number
nums = [4, 9, 2, 9, 7] print(sorted(set(nums))[-2]) # 7
Lists
Where freshers freeze.
- 05
Find duplicates in a list
seen, dups = set(), set() for x in nums: if x in seen: dups.add(x) seen.add(x) - 06
Missing number from 1 to n
n = len(nums) + 1 missing = n * (n + 1) // 2 - sum(nums)
Logic
Clean code wins here.
- 07
FizzBuzz, the classic filter
for i in range(1, 16): if i % 15 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i) - 08
Flatten a nested list
grid = [[1, 2], [3, 4], [5]] flat = [x for row in grid for x in row]
pandas
Now the analyst part.
- 09
Top 3 cities by revenue
(df.groupby("city")["rev"].sum() .nlargest(3)) - 10
Fill missing values with median
med = df["age"].median() df["age"] = df["age"].fillna(med)
pandas
The ones that get you hired.
- 11
Remove duplicate customers
df = df.drop_duplicates( subset=["email"], keep="first") - 12
Month-wise sales
df["m"] = df["date"].dt.to_period("M") df.groupby("m")["sales"].sum()
How to practise these
Don't
- Memorise the code
- Only read solutions
- Skip edge cases
Do
- Type every line
- Explain it aloud
- Test empty inputs