Assignment – II
Subject: Python Programming
Subject code: BCC302
Faculty Name: Session: 2025-26
Section:
[Link] Question CO/BTL Competitive/Univ.
.
1 What is the difference between for and while (BCC.302, AKTU 2022
loop in Python? Illustrate with examples. K3)
II Write a Python program using for loop to iterate (BCC302, AKTU 2023
over a dictionary and display key-value pairs. K3)
III What is the purpose of else with loops in (BCC302, AKTU 2022
Python? Write a program to demonstrate else K3)
with for loop.
IV Write a Python program using while loop to (BCC302, AKTU 2021
print numbers from 1 to 5, but terminate the loop K2)
when the number reaches 3.
V Consider the following python program def (BCC302, GATE 2024
func(A, n, m): s = A[0] for i in range(1, n-1): K4)
m = m * s + A[i] return m Let Z be an array of
10 elements with Z[i] = 2 for all i such that
0<=i<=9. The value returned by func(Z,10,2) is
____.
VI Consider the following Python code snippet. def (BCC302, GATE 2025
f(a,b): if (a==0): return b if (a%2==1): return K3)
2*f((a-1)/2,b) return b+f(a-1,b) print(f(15,10))
The value printed by the code snippet is
(Answer in integer)
VII You are writing a Python function for a logistics (BCC302, CodeChef
company to track pending shipments. Each K1)
shipment is represented as a dictionary with id,
weight, and status.
Write a function that:
-Returns a list of IDs for shipments where the
status is "pending" and weight is greater than
100 kg.
-Skips any shipment with status "cancelled"
using continue.
Sample input-
shipments = [
{"id": "S001", "weight": 150, "status":
"pending"},
{"id": "S002", "weight": 80, "status":
"completed"},
{"id": "S003", "weight": 110, "status":
"cancelled"},
{"id": "S004", "weight": 200, "status":
"pending"},
]
VIII You are monitoring a system log that tracks (BCC302, CodeChef
machine activity. The log is a list of strings K3)
representing machine [Link] a Python
script using a while loop that:
-Detects if any machine appears twice in a row
(indicating a potential error).
-Prints "Error: <machine_name> repeated" if
found.
-If no such error is found, print "No repeated
machines".
-Use else with the loop for the no-error message.
Sample input-
log = ["M1", "M2", "M2", "M3"]