π Student Practice
Original questions from assignments. Try solving them yourself!
π The course source also has Practice Questions on Maths and Additional Python / Pandas Questions in notebook formβsame style, extra practice.
π Assignment 1: Data Types & Strings
3.14 and an int like 5. Use int() to convert the sum to integer..count('i') method on strings. Be careful - this is case sensitive!type(variable) to get the type, or isinstance(variable, type) to check against a specific type.string[-4:] gets the last 4 characters..rstrip() to remove trailing whitespace, or .strip() for both ends.string[::2] gets every second character starting from index 0..isdigit() method..lower() method on strings..count('e') method on the string..isalpha() method - returns True if all characters are alphabetic.π Assignment 2: Loops & Conditions
if num not in [5, 7] or if num != 5 and num != 7 inside your loop before adding to sum.if num % 3 != 0 to check if NOT divisible by 3, then append to list.for i in range(1, 11) - remember range end is exclusive!sum(range(1, 101)) or accumulate in a loop.list1 = [11,2,190,43,23,65,19]list2 = [12,11,121,190,43,23,76,190]
Medium
& operator: set(list1) & set(list2)[21,44,22,878,55,90,17,68,69,91]
Medium
num % 2 == 0 for even, else odd. Append to respective lists.[21,44,767,98,37]
Easy
total = 0, then total += num for each number in the list.if num > 0, elif num < 0, else for zero..split(), then find max(words, key=len) or loop to find longest.1Β² + 2Β² + 3Β² + .... Add i**2 to total, increment i while i <= 10.π¦ Assignment 3: Lists & Dictionaries
sorted(list) for ascending, sorted(list, reverse=True) for descending.data = [1,2,3,4,6,7,7,8,9,9]. Drop duplicate values from this list and print the remaining numbers.
Easy
list(set(data)). Use dict.fromkeys(data) to preserve order.[21,44,767,98,37,106,345,87]
Medium
sorted(list)[-2]list1 = [21,44,767,98,37]
Easy
max(list1) and min(list1) functions.{**dict1, **dict2} or dict1 | dict2 (Python 3.9+) or dict1.update(dict2){i: i**2 for i in range(1, 6)}car_models = ["Toyota Camry", "Honda Civic", "Ford Mustang", "Chevrolet Silverado", "Nissan Altima"]car_prices = [2500000, 2200000, 4000000, 3500000, 2300000]
Easy
dict(zip(car_models, car_prices)) to combine two lists into a dictionary.for item, price in dict.items(): to iterate, then if price > 500: to check.sum(dict.values())/len(dict). Create new dict with items above avg, or use dict comprehension.set1 & set2 or set1.intersection(set2) to find common elements.βοΈ Assignment 4: Functions
return a+b, a-b, a*b. Call with add, sub, mul = func(x, y)For example: Factorial of 5 = 5Γ4Γ3Γ2Γ1 = 120 Medium
return sorted(lst, reverse=True) inside your function.if char.lower() in 'aeiou', count matches.num % 2 == 0 to check even. Return "Even" or "Odd" string, or True/False.[12,2,33,11,45,8]
Medium
sum(n for n in lst if n % 2 == 0)return list(set(lst)) or list(dict.fromkeys(lst)) to preserve order.return text[::-1]def greet_user(name="Guest"):return n ** 3 or return n * n * nπ Assignment 5: OOP, NumPy & Pandas
Variables:
β’ pi_value = 3.14 (private variable)
β’ radius
Functions:
β’ area_calculator
β’ circumference_calculator
Task: Create an object of Class Circle and calculate its area and circumference.
__pi_value for private variable. Area = Ο Γ rΒ². Circumference = 2 Γ Ο Γ r. Use __init__ to set radius.np.random.randint(10, 101, size=50) and np.sum(array)i) Create a 3Γ2 (3 rows, 2 columns) matrix using numpy and transpose it.
ii) After transposing print the first row of matrix (index 0)
iii) Print second element in first row
iv) Flatten the matrix to 1D (1 Dimensional) array
v) Print maximum, minimum and average value of 1D array
np.array([[...],[...],[...]]). Transpose with .T. Flatten with .flatten(). Stats: np.max(), np.min(), np.mean()π₯ Download dataset: Housing.csv β save in the same folder as your notebook.
Hardi) Calculate average and median price per square foot area (assume area column's unit is sq ft)
ii) Calculate median price per bedroom.
iii) Calculate average price per sq ft for furnished, semi-furnished and unfurnished homes.
iv) Are homes with guestrooms AND main road location costlier than remaining houses? Analyze the data using pandas to come up with your answer.
v) Convert the values in air conditioning column to 0 and 1 using lambda function
vi) Create a new column with the name price_index. Assign it 1 if the price of house is higher than average price else assign it value 0
vii) Sort the data by area, bathrooms and bedrooms column in descending order
pd.read_csv('Housing.csv').
Price per sqft: df['price']/df['area'].
GroupBy for categories: df.groupby('furnishingstatus')['price'].mean().
Lambda: df['ac'].apply(lambda x: 1 if x == 'yes' else 0)
βοΈ Ready to try?
Open Python on your computer or use an online editor like Replit or Google Colab to practice!