Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / Variables and Data Types
interview

Q1. You are analyzing customer age data from an e-commerce site. The ages are: 25, 30, 22, 45, 18. How would you store these ages in Python? What data type would you use for age and for a customer''s name?
Store ages in a list:
ages = [25, 30, 22, 45, 18]
Age is integer int, name is string str. Use type() to check. Variables: customer_name = "Alice". Python automatically infers types. You can also use tuple for immutable sequence, or NumPy array for numerical operations.

Q2. You are building a model to predict house prices. The features include number of bedrooms (integer), area (float), and city (string). How would you represent these different data types in Python?
Use appropriate types:
bedrooms = 3          # int
area = 1500.5          # float
city = "Bangalore"     # str
For a dataset, use a list of dictionaries or pandas DataFrame. Type conversion: str(area) converts to string, int(3.14) truncates. Dynamic typing allows mixing but be careful with operations.

Q3. In a data preprocessing step, you receive a list of transaction amounts: [''100'', ''200'', ''abc'', ''300'']. The third value is non-numeric. How would you safely convert to integers and handle the error?
Use a loop with try-except:
clean = [int(x) for x in amounts if x.isdigit()]
This uses str.isdigit() to filter only numeric strings. Alternatively use a loop with try: int(val) and catch ValueError.

Q4. You need to store a fixed set of operation codes that should not change: [101, 102, 103, 104]. Which data type would you choose to prevent accidental modification?
Use a tuple:
codes = (101, 102, 103, 104)
Tuples are immutable; trying to modify raises TypeError. Lists are mutable. For constants, tuple is appropriate. For set of unique codes, use set for fast lookup but mutable; frozenset for immutable set.

Q5. You have a dictionary of product prices: {''apple'': 120, ''banana'': 50, ''orange'': 80}. Write code to update banana price to 60 and add a new product ''grape'' with price 150. Then compute total cart value for items [''apple'', ''orange'', ''grape''].
prices['banana'] = 60
prices['grape'] = 150
total = sum(prices[item] for item in ['apple', 'orange', 'grape'])
# Result: 120 + 80 + 150 = 350
Dictionaries provide key-value mapping, essential for feature stores.