Python Practice Questions for Data
Analysts
1. Python Intro & Data Types
Easy
1. Create variables to store a dataset's basic info: name (string), records_count (int),
accuracy (float), is_cleaned (bool). Print their types.
2. Given sales data: revenue = "125000", tax_rate = 0.18. Convert revenue to int,
calculate tax amount, and display the result with proper formatting.
3. Create a dictionary to store employee data: id, name, department, salary. Access and
modify the salary field.
Medium
4. You have mixed data types in a list: data = [100, "25.5", True, "30", 45.7].
Write code to separate numeric and non-numeric values into different lists.
5. Create a data validation function that checks if a value can be converted to float,
returning the converted value or None.
2. If-Else-Elif Statements
Easy
6. Write code to categorize students based on grades: A (90+), B (80-89), C (70-79), D
(60-69), F (<60).
7. Create a data quality checker that validates if a customer's age is reasonable (0-120)
and email contains "@".
Medium
8. Build a sales performance classifier: Excellent (>100k), Good (50k-100k), Average
(20k-50k), Poor (<20k). Handle missing/null values.
9. Create a discount calculator based on customer type (Premium: 20%, Regular: 10%,
New: 5%) and purchase amount (>500 gets extra 5%).
3. Loops (For, While, Break, Continue, Pass)
Easy
10. Calculate the total revenue from a list of daily sales: sales = [1200, 1500, 900,
2100, 1800].
11. Print all even numbers from a customer ID list: customer_ids = [101, 102, 103,
104, 105, 106, 107, 108].
12. Count how many products have prices above $50: prices = [25.99, 67.50,
12.00, 89.99, 45.00, 120.00].
Medium
13. Process a list of temperatures, skip invalid readings (None or negative), and calculate
the average of valid readings.
14. Find the first customer with age > 65 from a customer list. Stop searching once found
using break.
15. Create a data cleaning loop that removes empty strings and converts valid strings to
uppercase, skipping invalid entries.
Advanced
16. Implement a while loop that keeps asking for sales data input until user enters 'done',
then calculates total and average.
4. List Comprehension
Easy
17. Convert a list of temperatures from Celsius to Fahrenheit: celsius = [0, 20, 30,
40, 100].
18. Extract all product names that start with 'A' from: products = ['Apple',
'Banana', 'Avocado', 'Cherry', 'Apricot'] .
19. Create a list of squares for all even numbers from 1 to 20.
Medium
20. From employee data employees = [('John', 50000), ('Jane', 75000),
('Bob', 45000)], create a list of names for employees earning > 50000.
21. Clean a dataset by converting strings to lowercase and removing spaces: data = ['
SALES ', 'Marketing', ' IT', 'Finance '] .
22. From a list of dictionaries representing sales records, extract all amounts > 1000:
python
sales = [{'id': 1, 'amount': 1200}, {'id': 2, 'amount': 800}, {'id':
3, 'amount': 1500}]
Advanced
23. Create a nested list comprehension to flatten this sales data:
python
quarterly_sales = [[1200, 1300, 1100], [1400, 1600, 1500], [1800,
1900, 1700]]
24. Generate a list of tuples (product, category) where price > 100:
python
products = [{'name': 'Laptop', 'price': 1200, 'category':
'Electronics'},
{'name': 'Book', 'price': 25, 'category': 'Education'}]
5. Pattern Printing
Easy
25. Print a pyramid pattern with numbers representing data hierarchy levels (1 to 5).
26. Create a pattern showing sales performance stars: 1 star for <1000, 2 stars for <2000,
etc.
Medium
27. Print a diamond pattern using department codes (A, B, C, D, E).
28. Create a table-like pattern showing month names and their corresponding quarter
numbers.
6. Functions
Basic Functions
29. Write a function calculate_roi(investment, return_amount) that returns the
ROI percentage.
30. Create a function clean_phone_number(phone) that removes all non-digit
characters.
31. Build a function categorize_customer(age) that returns customer segments: Teen,
Adult, Senior.
Mathematical Functions
32. Addition Function: Create sum_quarterly_sales(q1, q2, q3, q4) that returns
total annual sales.
33. Fibonacci: Write a function that generates the first n numbers of Fibonacci sequence
(useful for growth modeling).
34. Armstrong Number: Create a function to check if a customer ID is an Armstrong
number (for data validation games).
35. Palindrome: Build a function to check if a product code is a palindrome (quality
control check).
Advanced Functions
36. Create a function that takes a list of sales data and returns statistics (mean, median,
max, min).
37. Build a recursive function to calculate compound interest over multiple years.
38. Write a function that validates and cleans a dataset by applying multiple
transformation rules.
7. Lambda Functions
Easy
38. Use lambda to sort employees by salary: employees = [('John', 50000),
('Jane', 75000), ('Bob', 45000)] .
39. Filter products with price > 100 using lambda: products = [('Laptop', 1200),
('Mouse', 25), ('Keyboard', 150)].
40. Convert a list of sales amounts to include tax (multiply by 1.18) using map and
lambda.
Medium
41. Sort a list of customer dictionaries by age using lambda:
python
customers = [{'name': 'John', 'age': 25}, {'name': 'Jane', 'age':
30}, {'name': 'Bob', 'age': 20}]
42. Use lambda with filter to find all months with sales > average from:
python
monthly_sales = [12000, 15000, 9000, 18000, 11000, 16000]
43. Create a lambda function that calculates the percentage change between two values,
then apply it to compare monthly revenues.
Advanced
44. Use lambda with reduce to find the total revenue from nested department data:
python
departments = [{'name': 'Sales', 'revenue': [10000, 12000, 11000]},
{'name': 'Marketing', 'revenue': [5000, 6000, 5500]}]