Python Exam Study Notes
Complete Guide: Chapter 1, 2 & 3
Python Basics | Data Structures | Functions & Modules
CHAPTER 1
Python Basics
Q1. Five Unique Features of Python for Modern Software Development
• Simple & Readable Syntax — English-like code, easy to learn and maintain
• Interpreted Language — Runs line by line, no compilation needed; faster debugging
• Dynamically Typed — No need to declare variable types; Python detects automatically
• Extensive Libraries — Built-in modules (math, os, random) + third-party (NumPy,
Pandas, TensorFlow)
• Platform Independent — Write once, run anywhere (Windows, Linux, Mac) without
modification
• Object-Oriented & Functional — Supports OOP (classes, inheritance) and functional
(lambda, map, filter)
Q2 & Q4. Python Data Types – Mutable vs Immutable
• Mutable: can be changed after creation → list, set, dict
• Immutable: cannot be changed after creation → int, float, str, tuple, bool
Data Type Mutable/Immutable Example
int Immutable x = 10
float Immutable x = 3.14
str Immutable x = "Hello"
bool Immutable x = True
list Mutable x = [1,2,3]
tuple Immutable x = (1,2,3)
set Mutable x = {1,2,3}
dict Mutable x = {"a":1}
lst = [1,2,3] # list (mutable)
lst[0] = 99 # OK
tpl = (1,2,3) # tuple (immutable)
# tpl[0] = 99 # ERROR
Q3. Types of Loops – for vs while
for loop — used when iterations are known:
for i in range(1, 6):
print(i) # 1 to 5
while loop — used when iterations are unknown:
i = 1
while i <= 5:
print(i)
i += 1
• for: cleaner for fixed iterations | while: better for input validation/unknown loops
Q5. Python Operators and Their Precedence
Type Operators Description
Arithmetic +, -, *, /, //, %, ** Add, Sub, Mul, Div, Floor, Mod, Power
Relational ==, !=, >, <, >=, <= Compare values, returns True/False
Logical and, or, not Combine boolean expressions
Assignment =, +=, -=, *= Assign values to variables
Bitwise &, |, ^, ~, <<, >> Operate on bits
Identity is, is not Check same object in memory
Membership in, not in Check if value exists in sequence
• Precedence (High → Low): ** > * / // % > + - > Relational > not > and > or
result = 2 + 3 * 4 # 14 (not 20)
result = (2 + 3) * 4 # 20
Q6. Comments and Docstrings in Python
Comments:
• Single-line: use # | Multi-line: use triple quotes ''' or """
• Ignored by interpreter — for human readers only
Docstrings:
• Document functions/classes — placed right after def/class
• Accessible via __doc__
# Single-line comment
def add(a, b):
"""Returns sum of two numbers."""
return a + b
print(add.__doc__)
Chapter 1 Programs
Prog 1. Check Positive, Negative or Zero
num = int(input('Enter a number: '))
if num > 0:
print('Positive')
elif num < 0:
print('Negative')
else:
print('Zero')
Prog 2. Sum, Average & Data Types
n = int(input('How many numbers? '))
total = 0
for i in range(n):
num = float(input('Enter number: '))
total += num
print('Type:', type(num))
print('Sum:', total, ' Average:', total/n)
Prog 3. Demonstrate Arithmetic, Relational & Logical Operators
a, b = 10, 3
print(a+b, a-b, a*b, a/b, a//b, a%b, a**b) # Arithmetic
print(a>b, a<b, a==b, a!=b) # Relational
print(a>5 and b<5, a>5 or b>5, not(a>5)) # Logical
Prog 4. Count Number of Digits Using a Loop
num = int(input('Enter a number: '))
count = 0
temp = abs(num)
while temp > 0:
temp //= 10
count += 1
print('Digits:', count)
Prog 5. Multiplication Table Using for Loop
num = int(input('Enter a number: '))
for i in range(1, 11):
print(num, 'x', i, '=', num * i)
CHAPTER 2
Data Structures – List, Tuple, Set, Dictionary
Q1. List and Tuple – Features & Operations
List:
• Ordered, mutable (changeable), allows duplicates
• Defined with [ ]
• Methods: append(), remove(), pop(), sort(), reverse(), len()
Tuple:
• Ordered, immutable (cannot change), allows duplicates
• Defined with ( )
• Methods: count(), index()
lst = [1, 2, 3, 4]
[Link](5) # Add
[Link](2) # Remove
print(lst)
tpl = (10, 20, 30)
print(tpl[0]) # Access
print(len(tpl)) # Length
Q2(i). Range Function in Python
• range(start, stop, step) — generates a sequence of numbers
• Default: start=0, step=1 — used in loops
for i in range(1, 6): # 1 to 5
print(i)
for i in range(0, 10, 2): # Even numbers: 0,2,4,6,8
print(i)
Q2(ii). List vs Dictionary Methods
• List methods: append(), insert(), remove(), pop(), sort(), reverse(), index(), count()
• Dictionary methods: keys(), values(), items(), get(), update(), pop(), clear(), copy()
d = {"name": "Ram", "age": 20}
print([Link]()) # names
print([Link]()) # values
[Link]({"city": "KTM"})
Q3. Count Elements in Tuple/List
tpl = (1, 2, 3, 2, 4, 2)
print('Length:', len(tpl))
print('Count of 2:', [Link](2))
Q4. Set – Operations & Adding List Elements to Tuple
Set features: Unordered, no duplicates, mutable — defined with { }
• Union: A | B Intersection: A & B Difference: A - B Symmetric: A ^ B
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B, A & B, A - B)
# Add list to tuple
lst = [4, 5, 6]
tpl = (1, 2, 3) + tuple(lst)
print(tpl)
Q5. Create 2 Dictionaries and Merge
d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}
merged = {**d1, **d2} # Method 1
print(merged)
[Link](d2) # Method 2
print(d1)
Q6. Set Operations – Union, Intersection, Difference
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print('Union:', A | B)
print('Intersection:', A & B)
print('Difference:', A - B)
[Link](7) # Insert element
Q7. Dictionary – Store Student Details
students = {}
n = int(input('How many students? '))
for i in range(n):
name = input('Name: ')
marks = int(input('Marks: '))
students[name] = marks
for k, v in [Link]():
print(k, '->', v)
Q8. All Python Data Structures
Structure Ordered Mutable Duplicates Syntax
List Yes Yes Yes []
Tuple Yes No Yes ()
Set No Yes No {}
Dictionary Yes Yes No (keys) {k:v}
lst = [1, 2, 3]
tpl = (1, 2, 3)
st = {1, 2, 3}
dct = {"a": 1, "b": 2}
print(lst, tpl, st, dct)
CHAPTER 3
Functions & Modules
Q1. User-Defined Functions, Local/Global Scope, Factorial
• Function: block of reusable code defined with def
• Local variable: declared inside function — accessible only there
• Global variable: declared outside — accessible everywhere — use global keyword to
modify inside function
count = 0 # global
def factorial(n):
global count
count += 1
result = 1
for i in range(1, n+1):
result *= i
return result
num = int(input('Enter number: '))
print('Factorial:', factorial(num))
print('Called:', count, 'times')
Q2. Built-in & Package Functions + math Module
• Built-in: print(), input(), len(), range(), type(), int(), str()
• Package functions: from math, os, random, etc.
import math
num = float(input('Enter number: '))
print('Square root:', [Link](num))
print('Power (2^3):', [Link](2, 3))
print('Pi:', [Link])
Q3. Anonymous / Lambda Function
• Lambda: single-line anonymous function
• Syntax: lambda arguments: expression
• def has a name and can be multi-line | lambda is nameless and one-liner
nums = [1, 2, 3, 4, 5]
# map – square all
squared = list(map(lambda x: x**2, nums))
print('Squared:', squared)
# filter – even numbers
evens = list(filter(lambda x: x % 2 == 0, nums))
print('Evens:', evens)
# reduce – sum
from functools import reduce
total = reduce(lambda x, y: x + y, nums)
print('Sum:', total)
Q4. Function Definition, Call + Prime Numbers
• Function definition: using def keyword
• Function call: using function name with arguments
def is_prime(n):
if n < 2: return False
for i in range(2, int(n**0.5)+1):
if n % i == 0: return False
return True
def display_primes(limit):
for i in range(2, limit+1):
if is_prime(i):
print(i, end=' ')
limit = int(input('Enter range: '))
display_primes(limit)
Q5. Module & Package in Python
• Module: a .py file containing functions/variables
• Package: a folder with multiple modules + __init__.py file
# [Link] (user-defined module)
def add(a, b):
return a + b
def subtract(a, b):
return a - b
# [Link] (import and use)
import mymath
print([Link](10, 5)) # 15
print([Link](10, 5)) # 5
Quick Revision Tips – All Chapters
Chapter 1:
• Python is interpreted, dynamically typed, and platform-independent
• Mutable = can change (list, dict, set) | Immutable = cannot change (int, str, tuple)
• for = known iterations | while = unknown/condition-based iterations
• Precedence: ** > * / % > + - > relational > not > and > or
• # for comments | """ """ for docstrings (accessible via __doc__)
Chapter 2:
• List = mutable, ordered | Tuple = immutable, ordered
• Set = no duplicates, unordered | Dictionary = key:value pairs
• Merge dicts: {**d1, **d2} or [Link](d2)
• Set ops: | union, & intersection, - difference, ^ symmetric difference
Chapter 3:
• Lambda = anonymous one-line function | def = named multi-line function
• Use global keyword to modify global variable inside a function
• import module_name to use a module | from module import func for specific function
• [Link](), [Link](), [Link] from math module
• map(), filter(), reduce() work with lambda for list processing