Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / Variables and Data Types
interview

Q1. Scenario: 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. Scenario: 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 careful with operations.

Q3. Scenario: 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: for val in amounts: try: int(val) except ValueError: handle or skip. Also use str.isdigit() to check. Example:
clean = [int(x) for x in amounts if x.isdigit()]
This ensures numeric data only.

Q4. Scenario: 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. Scenario: 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''].
Update: prices[''banana''] = 60. Add: 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.