Python Programming - Semester Exam Important Q&A
Q1: Explain the various categories of operators in Python. Solve expressions for a=3, b=5,
c=10.
Operators in Python:
1. Arithmetic Operators (+, -, *, /, %, **, //)
2. Comparison Operators (==, !=, >, <, >=, <=)
3. Logical Operators (and, or, not)
4. Bitwise Operators (&, |, ^, ~, <<, >>)
5. Assignment Operators (=, +=, -=, *=, etc.)
6. Identity Operators (is, is not)
7. Membership Operators (in, not in)
i) a & b < 2/5**2 + c^b:
- Precedence: ** > / > + > < > &
- Solve: 5**2=25, 2/25=0.08, c^b=10^5=15, then 0.08+15=15.08, then b<15.08 is True (1), a & 1 => 3 & 1 = 1
ii) b >> a**2 >> b**2 ^ c**3:
- a**2=9, b**2=25, c**3=1000
- b >> 9 = 5 >> 9 = 0, 0 >> 25 = 0, 0 ^ 1000 = 1000
Q2: What is operator precedence and associativity?
Operator precedence defines the order of operations.
Associativity determines the order of evaluation of operators with the same precedence.
Example: 5 + 3 * 2 => 3*2=6, then 5+6=11.
Q3: Discuss different types of data types in Python.
1. Numeric (int, float, complex)
2. Sequence (str, list, tuple)
3. Set (set, frozenset)
4. Mapping (dict)
Python Programming - Semester Exam Important Q&A
5. Boolean (bool)
6. Binary (bytes, bytearray, memoryview)
7. NoneType (None)
Q4: How is memory managed in Python? Explain PEP8.
Memory is managed via reference counting and garbage collection. Python uses private heap space and
memory manager.
PEP8 is a style guide for Python code (indentation, naming, spacing). Helps maintain readable and
consistent code.
Q5: How is Python an interpreted language?
Python code is not compiled to machine-level code. It is executed line-by-line using the Python interpreter.
Q6: What type of language is Python?
Python is high-level, dynamically typed, interpreted, object-oriented, and general-purpose programming
language.
Q7: Define floor division. Benefits of Python?
Floor division (//) returns the largest integer less than or equal to division result.
Benefits: Easy syntax, large standard library, community support, platform-independent.
Q8: Explain conditional statements in Python with code.
if condition:
# code
elif another_condition:
# code
else:
# code
Python Programming - Semester Exam Important Q&A
Example:
x = 10
if x > 0:
print('Positive')
elif x == 0:
print('Zero')
else:
print('Negative')
Q9: Program to check leap year.
year = int(input('Enter year: '))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print('Leap year')
else:
print('Not a leap year')
Q10: Fibonacci series program.
n = int(input('How many terms? '))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
Q11: Pattern program (e.g., pyramid).
rows = 5
for i in range(1, rows+1):
print(' '*(rows-i) + '*'*(2*i-1))
Python Programming - Semester Exam Important Q&A
Q12: List data structure and methods.
List is ordered, mutable.
Methods: append(), insert(), remove(), pop(), sort(), reverse()
Example:
l = [1,2,3]
l.append(4)
l.sort()
Q13: List comprehension and tuple add.
List comprehension: [x for x in range(5)]
Tuple add:
t = (1,2,3)
t += (4,)
print(t)
Q14: Compare list and dictionary with examples.
List: Ordered, index-based. Dict: Unordered, key-based.
List methods: append(), pop()
Dict methods: get(), keys(), values(), update()
Q15: Argument passing & variable-length args.
Types: Positional, Keyword, Default, Variable-length (*args, **kwargs)
Example:
def add(*args):
return sum(args)
Q16: Function parts and scope - Calculator example.
def calc(a, b, op):
Python Programming - Semester Exam Important Q&A
if op == '+': return a+b
elif op == '-': return a-b
elif op == '*': return a*b
elif op == '/': return a/b
print(calc(4,2,'+'))
Q17: Function removekth(s, k).
def removekth(s, k):
return s[:k] + s[k+1:] if k < len(s) else s
Examples:
removekth('python',1) -> 'pthon'
Q18: File I/O and program to read line-by-line.
f = open('file.txt','r')
data = []
for line in f:
data.append(line)
f.close()
Q19: Difference between read(), readline(), readlines() and write(), writelines().
read(): entire content
readline(): single line
readlines(): list of lines
write(): write string
writelines(): write list of strings
Q20: File opening modes in Python.
r, w, a, r+, w+, a+ - modes for reading/writing/appending in text/binary modes.
Python Programming - Semester Exam Important Q&A
Q21: Python module and import methods.
Module: File with .py extension. Methods: import module, from module import, import as
Significance: Reusability, organization
Q22: Creating a module usable as library and script.
Use if __name__ == '__main__':
# script code
Otherwise functions will be used in import.
Q23: Python programming cycle.
Edit -> Save -> Run using Interpreter -> Debug -> Repeat. IDEs like IDLE or PyCharm are used.
Q24: Short notes: type conversion, precedence, Boolean expression.
Type conversion: int('5'), float('3.14')
Precedence: *, / > +, -
Boolean: True, False, with and/or/not
Q25: Define Pandas and built-in functions.
Pandas: Data manipulation library.
Functions: read_csv(), head(), tail(), describe(), drop(), fillna(), merge()