Python Lab Final Exam Complete
Preparation Notes
Prepared from the complete lab manual and teacher instructions.
Focus: Explanation Questions (25 Marks) + Output/Error Detection/Programs (25 Marks)
Variables & Data Types
Definition: A variable is a named memory location used to store data.
Data Types:
• int = 10
• float = 3.14
• str = "Ali"
• bool = True
Important:
input() always returns a string.
Use int(), float(), str(), bool() for type casting.
Common Errors:
1. ValueError: int("abc")
2. TypeError: "Age" + 20
Output Questions:
age=21
print(type(age))
Output: <class 'int'>
x=10
y=5
print(x>y)
Output: True
Tuples
Definition: Ordered and immutable collection.
Properties:
• Ordered
• Immutable
• Can store different data types
• Uses () brackets
Methods:
count()
index()
Important Outputs:
colors=("Red","Green","Blue")
print(colors[1])
Output: Green
data=(10,20,30,40)
print(data[::-1])
Output: (40,20,10,0?) -> Correct: (40,30,20,10)
Errors:
t=(1,2)
t[0]=5
TypeError
[Link](3)
AttributeError
Theory Question:
Why tuple is immutable?
Answer: To protect data from accidental modification.
Operators & Expressions
Arithmetic: + - * / // % **
Relational: > < >= <= == !=
Logical: and or not
Membership: in, not in
Identity: is, is not
Very Important Outputs:
15//4 = 3
15%4 = 3
2**3 = 8
Expression:
10 + 2 * 3 ** 2
3**2=9
2*9=18
10+18=28
Output: 28
Difference:
== compares values.
is compares memory locations.
Strings
Definition: Sequence of characters.
Indexing:
Python
012345
Examples:
s="Python"
s[0] => P
s[-1] => n
Slicing:
s[0:4] => Pyth
s[::-1] => nohtyP
Methods:
lower()
upper()
title()
strip()
replace()
split()
join()
find()
Output Questions:
'DaTa'.lower()
Output: data
email='user@[Link]'
print([Link]('@'))
Output: 4
Theory:
Difference between split() and join():
split() converts string to list.
join() converts list to string.
Lists
Definition: Mutable collection using [].
Methods:
append()
insert()
remove()
pop()
sort()
Output Questions:
a=[1,2,3]
[Link](4)
print(a)
Output: [1,2,3,4]
a=[1,2,3,4]
[Link]()
Output: [1,2,3]
Difference Between List and Tuple:
List -> Mutable []
Tuple -> Immutable ()
For Loop
Used when number of repetitions is known.
Syntax:
for i in range(1,6):
print(i)
Important:
break -> stop loop
continue -> skip iteration
enumerate() -> index + value
Output:
for i in range(5):
if i==3:
break
print(i)
Output:
0
1
2
While Loop
Used when repetitions are not known.
Syntax:
while condition:
statements
Output:
i=1
while i<=5:
print(i)
i+=1
Output:
12345
Important:
break
continue
pass
Conditional Statements
if
if-else
if-elif-else
nested if
Example:
num=0
if num>0:
print("Positive")
elif num<0:
print("Negative")
else:
print("Zero")
Output: Zero
Most Important Programs for Final Exam
1. Sum of List
nums=[1,2,3,4]
print(sum(nums))
2. Largest Number
print(max(nums))
3. Smallest Number
print(min(nums))
4. Factorial Using For Loop
n=5
fact=1
for i in range(1,n+1):
fact*=i
print(fact)
Output: 120
5. Prime Number Program
6. Multiplication Table
7. Vowel Counter
8. Sum of Digits
9. Reverse Number
10. Palindrome Check
11. Student Record Dictionary
12. Leap Year Program
13. Grade Calculator
14. Authentication System
15. Positive/Negative/Zero
Most Expected Explanation Questions
1. What is a variable?
2. Explain data types.
3. What is type casting?
4. Difference between list and tuple.
5. Explain indexing and slicing.
6. What is a loop?
7. Difference between for and while loop.
8. Explain break and continue.
9. What is a conditional statement?
10. Difference between == and is.
11. What is a string?
12. Explain append(), remove(), pop().
13. Why are tuples immutable?
14. Explain operator precedence.
15. Explain input() function.
Error Detection & Correction Practice
Q1:
print("Age="+20)
Error:
TypeError
Correction:
print("Age="+str(20))
Q2:
num=int("abc")
Error:
ValueError
Q3:
t=(1,2)
t[0]=5
Error:
TypeError
Q4:
t=(1,2)
[Link](3)
Error:
AttributeError
Q5:
if x=5:
Error:
SyntaxError
Correction:
if x==5: