BCC302 – PYTHON PROGRAMMING (DETAILED SOLUTIONS)
[Link] Semester III – Question-wise detailed answers prepared in clear and exam-oriented
language.
SECTION A – Short Answer Questions
Q1(a) Python Variables: Variables in Python are names that store data values in memory. Python
does not require explicit type declaration; the type is decided automatically at runtime. Example:
x = 10 (integer)
y = 3.14 (float)
Q1(b) for loop vs while loop: A for loop is generally used when the number of iterations is known
in advance and commonly uses range(). A while loop is used when the loop should continue until a
condition becomes false. for loop is more compact, while while loop is condition-based.
Q1(c) String Slicing: String slicing is used to extract a part of a string using indexing. Syntax:
string[start:end]. Example: s = 'Python'; s[1:4] gives 'yth'.
Q1(d) Tuple: A tuple is an ordered and immutable collection of elements written using parentheses
(). Once created, elements cannot be modified. A list uses [] and is mutable.
Q1(e) Dictionary: A dictionary stores data in key–value pairs. Keys are unique. Example: d =
{'name':'Ram','age':20}. Manipulation: d['age']=21.
Q1(f) read() and readline(): read() reads the entire file content at once, while readline() reads only
one line at a time, useful for large files.
Q1(g) Encapsulation: Encapsulation is an OOP concept that binds data and methods together in a
class and restricts direct access using access specifiers like private variables.
SECTION B – Descriptive Questions
Q2(a) Operators and Data Types: Python supports arithmetic (+, -, *, /), relational (>, <, ==),
logical (and, or, not) operators. Common data types are int, float, string, list, tuple, and dictionary.
Program:
n=int(input())
if n%2==0: print('Even')
else: print('Odd')
Q2(b) Loop Control Statements: pass does nothing, continue skips current iteration, break exits
loop immediately. Prime numbers are found by checking divisibility using for loop.
Q2(c) String Operations: Strings support concatenation, slicing, and built-in functions like len(),
upper(), lower(). Vowels are counted by checking each character using loop.
Q2(d) List and Dictionary Functions: Lists support append(), insert(), pop(). Dictionaries support
keys(), values(), items(). Two dictionaries can be merged using update() or | operator.
Q2(e) File Writing: write() writes a single string to a file. writelines() writes multiple strings from a
list. Student records can be written line by line.
SECTION C – Long Answer Questions
Q3(a) Conditional Statements: Python uses if, elif, and else for decision making. Based on marks,
grades are assigned using conditions.
Q3(b) for Loop with Lists: for loop iterates over each element of list. Sum is calculated using an
accumulator variable.
Q4(a) Functions: Functions are reusable blocks of code defined using def keyword. Area of circle
is calculated using formula πr².
Q4(b) String Manipulation: Methods like replace(), split(), join() modify strings. replace() changes
characters or substrings.
Q5(a) Sets: Sets store unique elements. Operations include union, intersection, difference.
Difference returns elements present in first set only.
Q5(b) Tuple and List Slicing: Slicing extracts a subset of elements. A tuple can be reversed using
slicing syntax[::-1].
Q6(a) File Pointer Manipulation: seek() changes the current file position. Last n lines can be read
using readlines() and slicing.
Q6(b) Regular Expressions: re module is used for pattern matching. Digits in a string are found
using [Link]('[0-9]', string).
Q7(a) Inheritance and Polymorphism: Inheritance allows a class to acquire properties of another
class. Polymorphism allows same method name with different behavior.
Q7(b) Tkinter: Tkinter is Python’s GUI library. Widgets like Label and Button are used to create
windows and interfaces.