Q1. You have a list of model accuracies [0.85, 0.92, 0.78, 0.95, 0.88]. Write a loop to find the highest accuracy and print it. Also, count how many models have accuracy above 0.90.
max_acc = max(accuracies)
count = sum(1 for acc in accuracies if acc > 0.90)
# Using loop:
highest = accuracies[0]
for acc in accuracies:
if acc > highest:
highest = accConditional statements (if) inside loop. This is basic iteration and comparison.Q2. A dataset has missing values marked as -999. Filter out these values and compute the mean of remaining numbers using a loop and conditional.
data = [10, -999, 20, 30, -999, 40]
filtered = [x for x in data if x != -999]
mean = sum(filtered) / len(filtered)
# Using loop:
total = 0
count = 0
for x in data:
if x != -999:
total += x
count += 1
mean = total / countThis simulates data cleaning.Q3. You need to classify a person''s BMI category: underweight (<18.5), normal (18.5-24.9), overweight (25-29.9), obese (>=30). Write a conditional statement for BMI=23.7.
if bmi < 18.5:
category = "Underweight"
elif bmi < 25:
category = "Normal"
elif bmi < 30:
category = "Overweight"
else:
category = "Obese"For bmi=23.7 → "Normal". Boolean conditions and elif chains are fundamental for rule-based classification.Q4. You are training a model. You have a list of epoch numbers [1,2,3,4,5] and corresponding loss values [0.5,0.4,0.35,0.33,0.32]. Write a loop to print each epoch and its loss. Stop the loop if loss drops below 0.34 using break.
for epoch, loss in zip(epochs, losses):
print(f"Epoch {epoch}: loss {loss}")
if loss < 0.34:
breakThis will print the first 4 epochs. break exits the loop prematurely. This is used in early stopping during training.Q5. Generate a list of even numbers from 0 to 20 using a for loop with range and a conditional. Then create the same list using list comprehension.
# Using loop
evens = []
for i in range(21):
if i % 2 == 0:
evens.append(i)
# Using list comprehension
evens = [i for i in range(21) if i % 2 == 0]Both produce [0,2,4,6,8,10,12,14,16,18,20]. Comprehension is more Pythonic and efficient.