■ Python 6-Week Assignment Pack (CSE 20 + CSE
30)
■ Week 1 — Python Basics & Data Types
Topics: Variables, expressions, statements, basic I/O, data types
MCQs:
1. What will type(3.0) return?
a) int b) float c) double d) str
2. print("5" * 3) → ?
a) 15 b) 555 c) error d) None
3. Invalid variable name?
a) _count b) count1 c) 1count d) count_
Debugging:
num = input("Enter number: ")
square = num * num
print("Square is: " + square)
Practice:
1. Area of circle
2. Celsius to Fahrenheit
3. Print name and age
■ Week 2 — Control Structures
Topics: if-elif-else, while, for
MCQs:
1. Output of if/elif chain example?
a) A b) B c) A then B d) C
2. Loop for unknown iterations?
a) for b) while c) do-while d) None
Debugging:
count = 5
while count > 0:
print(count)
count -= 1
Practice:
1. FizzBuzz
2. Guessing game
■ Week 3 — Functions, Strings, Data Structures
MCQs:
1. Correct function definition?
a) def myfunc {} b) function myfunc(): c) def myfunc(): d) myfunc def():
2. s[::-1] for "hello"?
a) hello b) olleh c) error d) h
Debugging:
def add(a, b):
return a + b
print(add(2))
Practice:
1. Count vowels
2. Student topper dictionary
3. Remove duplicates
■ Week 4 — File I/O, Error Handling, Intro to OOP
MCQs:
1. Mode to overwrite file?
a) r b) w c) a d) rw
2. Purpose of __init__?
a) destroy b) init attrs c) copy obj d) none
Debugging:
file = open("data.txt", "r")
data = file.read()
print(data)
file.close
Practice:
1. BankAccount class
2. Division by zero handling
3. Count words in file
■ Week 5 — Advanced Python: OOP, Iterators, Generators, Recursion
MCQs:
1. Generator yield example output?
a) 1 2 b) 2 1 c) error d) None
2. Recursion truth?
a) call twice b) base case c) faster than loops d) no args
Debugging:
class Animal: ... class Dog(Animal): def speak(): ...
Practice:
1. Even number generator
2. Factorial recursion
3. Employee with private attrs
■ Week 6 — Algorithms, Functional Programming, Mini-Projects
MCQs:
1. Graph shortest path module?
a) graph b) networkx c) math d) numpy
2. reduce(lambda x,y: x+y, [1,2,3])?
a) 6 b) [6] c) (6,) d) error
Debugging:
from functools import map
nums = [1,2,3] squares = map(lambda x: x*x nums)
Practice:
1. BFS
2. Coin change DP
3. To-Do CLI app
■ Additional DSA Practice Questions
Strings:
Reverse string, palindrome check, char freq, longest non-repeat substring, anagram check
Arrays/Lists:
Max/min without built-in, rotate list, second largest, remove value, sum pairs
Bit Manipulation:
Count set bits, power of 2 check, swap without var, unique number, reverse bits
Sets:
Manual union/intersection, find duplicates, disjoint check, diff, symmetric diff
Dictionaries:
Word freq, invert dict, merge sum values, max value key, group by first letter
Tuples:
Sort by second elem, unpack, most common, tuple->dict, merge tuples
Recursion:
Factorial, Fibonacci, Tower of Hanoi, reverse string, prime check
Matrix:
Spiral print, transpose, multiply, sum, search in sorted matrix
Functions:
Varargs sum, Armstrong, min & max, flatten list, memoization
OOP:
Student class, multiple inheritance, + overload, instance counter, abstract Shape class
■ Additional DSA Practice Questions with Code & Explanations
1■■ Strings
1. Reverse a string without slicing:
def reverse_string(s): result = "" for ch in s: result = ch + result return result
print(reverse_string("hello")) Explanation: Prepending each character reverses the string.
2. Check palindrome:
def is_palindrome(s): s = s.replace(" ", "").lower() return s == s[::-1] Explanation: Remove spaces,
lowercase, compare to reversed.
2■■ Arrays / Lists
1. Find max without max():
def find_max(lst): m = lst[0] for num in lst: if num > m: m = num return m Explanation: Track the
largest element manually.
2. Rotate list right by k:
def rotate_list(lst, k): k %= len(lst) return lst[-k:] + lst[:-k] Explanation: Use slicing to split and
rearrange.
3■■ Bit Manipulation
1. Count set bits:
def count_bits(n): count = 0 while n: count += n & 1 n >>= 1 return count Explanation: Check last
bit with &1, shift right until 0.
4■■ Sets
1. Find duplicates:
def find_duplicates(lst): seen = set() dup = set() for x in lst: if x in seen: dup.add(x) else: seen.add(x)
return dup Explanation: Track seen elements, collect repeats.
5■■ Dictionaries
1. Word frequency:
def word_freq(text): freq = {} for word in text.split(): freq[word] = freq.get(word, 0) + 1 return freq
Explanation: Use dict with get() to count occurrences.
6■■ Tuples
1. Sort list of tuples by second element:
data = [(1, 3), (2, 1), (4, 2)] print(sorted(data, key=lambda x: x[1])) Explanation: key=lambda sorts
by second tuple element.
7■■ Recursion
1. Factorial:
def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) Explanation: Base case at 1, multiply
recursively.
8■■ Matrix
1. Transpose matrix:
def transpose(mat): return [[mat[j][i] for j in range(len(mat))] for i in range(len(mat[0]))] Explanation:
Swap rows and columns using list comprehension.
9■■ Functions
1. Variable arguments sum:
def sum_all(*args): return sum(args) Explanation: *args collects arguments into tuple, sum them.
■ OOP
1. Student class:
class Student: def __init__(self, name, age, marks): self.name = name self.age = age self.marks =
marks def grade(self): if self.marks >= 90: return "A" elif self.marks >= 75: return "B" return "C" s =
Student("John", 20, 88) print(s.grade()) Explanation: Simple class with method to calculate grade
based on marks.