1. Explain Python Data Types with Examples.
Python supports various data types such as Numeric, String, List, Tuple, Set, Dictionary, and
Boolean. 1. **Numeric:** Includes int, float, and complex numbers. Example: x = 10 (int), y = 3.14
(float), z = 2 + 3j (complex) 2. **String:** Collection of characters enclosed in quotes. Example:
name = "Pranesh" print(name[0]) → P 3. **List:** Ordered and mutable collection. Example:
numbers = [1,2,3,4] 4. **Tuple:** Ordered and immutable collection. Example: data = (10,20,30) 5.
**Set:** Unordered and unique collection. Example: s = {1,2,3,3} → {1,2,3} 6. **Dictionary:** Stores
data as key-value pairs. Example: student = {'name':'Ravi','age':21} 7. **Boolean:** True or False
values. Example: a = True, b = False Python is dynamically typed, meaning you don’t need to
declare variable types explicitly.
2. Explain Iterative Statements (Loops) in Python.
Iterative statements are used to execute a block of code repeatedly. 1. **For Loop:** Used to iterate
through a sequence like list, string, or range. Example: for i in range(1,6): print(i) 2. **While Loop:**
Executes until the condition becomes false. Example: i = 1 while i <= 5: print(i) i += 1 3. **Nested
Loops:** Loop inside another loop. 4. **Loop Control Statements:** - break → exits the loop. -
continue → skips current iteration. - pass → does nothing. Loops reduce code repetition and
increase efficiency.
3. Explain Exception Handling in Python.
Exceptions are runtime errors that stop the program execution. Exception handling allows safe
program termination. **Syntax:** try: # code that may cause error except ExceptionType: # handle
error else: # executes if no exception finally: # executes always **Example:** try: a = int(input('Enter
number: ')) b = int(input('Enter number: ')) print(a/b) except ZeroDivisionError: print('Cannot divide
by zero') finally: print('End of program') Benefits: Prevents abnormal termination, improves program
reliability, and provides debugging information.
4. Explain Functions in Python (with Variable Arguments).
A function is a block of reusable code. **Syntax:** def function_name(parameters): statements
return value **Types of Arguments:** - Positional - Keyword - Default - Variable length (*args,
**kwargs) **Example:** def add(*nums): sum = 0 for n in nums: sum += n return sum
print(add(10,20,30)) Advantages: Code reuse, readability, modular programming, easy
maintenance.
5. Explain Lists and Tuples with Examples.
List: Mutable, allows modification. Tuple: Immutable, cannot be changed after creation. **List
Example:** fruits = ['apple','banana','cherry'] [Link]('mango') print(fruits) **Tuple Example:**
colors = ('red','blue','green') print(colors[1]) **Difference:** - List uses [], Tuple uses () - List mutable,
Tuple immutable - Lists slower, Tuples faster Used for storing multiple items in single variable.
6. Explain File Handling in Python with Example.
Python allows file operations like read, write, and append. **Steps:** 1. Open file using open() 2.
Perform operation 3. Close file using close() **Example:** file = open('[Link]','w') [Link]('Hello
Python') [Link]() file = open('[Link]','r') print([Link]()) [Link]() **Modes:** 'r' = read, 'w' =
write, 'a' = append, 'r+' = read/write File handling enables data storage permanently.
7. Explain Class and Object in Python (Employee Example).
Class: Blueprint for creating objects. Object: Instance of class. **Example:** class Employee: def
__init__(self, eid, name, salary): [Link] = eid [Link] = name [Link] = salary def
display(self): print('ID:', [Link], 'Name:', [Link], 'Salary:', [Link]) emp1 = Employee(101,
'Ravi', 50000) [Link]() **Features:** - Data encapsulation - Reusability - Data abstraction -
Real-world representation
8. Fraction Class Program in Python.
Program using class to perform addition and subtraction of fractions. class Fraction: def
__init__(self, num, den): [Link] = num [Link] = den def add(self, f2): n = [Link]*[Link] +
[Link]*[Link] d = [Link]*[Link] return Fraction(n,d) def display(self): print([Link],'/',[Link])
f1 = Fraction(1,2) f2 = Fraction(1,3) f3 = [Link](f2) [Link]() **Output:** 5 / 6
9. Write a Python Program for Matrix Addition.
Program to add two matrices of same dimension. X = [[1,2,3], [4,5,6], [7,8,9]] Y = [[9,8,7], [6,5,4],
[3,2,1]] result = [[0,0,0],[0,0,0],[0,0,0]] for i in range(len(X)): for j in range(len(X[0])): result[i][j] = X[i][j]
+ Y[i][j] for r in result: print(r) **Output:** [10,10,10] [10,10,10] [10,10,10]
10. Write a Python Program for Fibonacci Series using Function.
Program to print Fibonacci series. def fibonacci(n): a,b = 0,1 for i in range(n): print(a, end=' ') a,b =
b,a+b fibonacci(10) **Output:** 0 1 1 2 3 5 8 13 21 34 Explained: First two terms 0 and 1, each next
term is sum of previous two. Used in mathematical and algorithmic problems.