Real Python Interview Questions American Express
Real Python Interview Questions American Express
Interview Questions
(0-3 Years)
15-17 LPA
Python Questions
1. What is the difference between a list, tuple, set, and dictionary
in Python?
List: Ordered, mutable, allows duplicates. Example: [1, 2, 3]
Tuple: Ordered, immutable, allows duplicates. Example: (1, 2, 3)
Set: Unordered, mutable, no duplicates. Example: {1, 2, 3}
Dictionary: Key-value pairs, unordered (ordered as of Python 3.7+), mutable. Example:
{'a': 1, 'b': 2}
try:
x = 10 / 0
except ZeroDivisionError as e:
print("Error:", e)
finally:
print("Cleanup done")
def decorator(func):
def wrapper():
print("Before function")
func()
print("After function")
return wrapper
@decorator
def greet():
add = lambda x, y: x + y
def count():
for i in range(5):
yield i
import copy
shallow = copy.copy(obj)
deep = copy.deepcopy(obj)
df.isnull().sum()
df.fillna(0)
df.dropna()
df.loc["row_label", "col_label"]
df.iloc[2, 3]
13. Group by region and get average sales
df.groupby('region')['sales'].mean()
Q1 = df['value'].quantile(0.25)
Q3 = df['value'].quantile(0.75)
IQR = Q3 - Q1
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr + 10
def is_palindrome(s):
return s == s[::-1]
20. 2nd largest without sort
def second_largest(nums):
return second
21. Word frequency count
list: O(n)
set: O(1) average case (hash-based)
import json
with open('data.json') as f:
data = json.load(f)
pd.read_csv('large.csv', chunksize=10000)
25. Flatten nested dict
items = {}
for k, v in d.items():
if isinstance(v, dict):
items.update(flatten(v, new_key, sep))
else:
items[new_key] = v
return items
class Demo:
@staticmethod
@classmethod
__repr__: unambiguous
28. Inheritance
class Animal:
def sound(self): print("Generic")
class Dog(Animal):
def sound(self): print("Bark")
def get_churned(transactions):