Q4. What are loops in Python? Explain Q5. Define a function in Python. Explain Q6.
in Q6. Explain string operations in Python
while loop and for loop with examples. types of functions with examples. with examples. Answer (Long):
Answer (Long): Answer (Long): Python strings are immutable
sequences of characters.
Loops are used to repeat a block of code A function is a reusable block of code
multiple times. that performs a pecific task. Basic String Operations:
1. while Loop: Types of Functions: Concatenation: "Hello" + " World"
Executes until a condition becomes 1. Built-in Functions: Repetition: "A"*3 → "AAA"
false. i = 1
Examples: len(), type(), range() Indexing: "Python"[0] → 'P'
while i <= 5:
2. User-defined Functions: Slicing: "Python"[1:4] → "yth"
print(i)
def greet(): Important Functions: len("Python") → 6
i += 1
print("Welcome to Python") upper(), lower(), replace(), split(), find()
2. for Loop: Iterates through a sequence
3. Functions with Parameters: Strings are widely used for input
(list, tuple, string).
processing, text handling, data cleaning,
def add(a, b): return a + b
for x in [10, 20, 30]: and messaging in Python applications.
4. Functions with Default Arguments:
print(x)
Q7. Differentiate between lists and
def employee(name, salary=30000):
Loop Control Statemnts: tuples. Explain operations on lists.
print(name, salary)
break – exits loop Answer (Long):
5. Lambda (Anonymous) Functions:
continue – skips current iteraton Feature List Tuple
square = lambda x : x*x
pass – placeholder statement Mutability Mutable Immutable
Loops help automate repetitive Q8. Explain dictionaries and their Syntax [] ()
operations such as reading data, operations with examples. Answer
(Long): Usage Dynamic data Fixed data
processing records, and running
algorithms. Speed Slower Faster
A dictionary stores data in key–value
pairs. List Operations: roll = [10, 20, 30]
Q9. Explain file handling in Python. How
are files opened, read, and written? student = {"name":"Rahul", "age":21} [Link](40)
Answer (Long): Python uses the open() Operations: Access: student["name"] [Link](1, 15)
function for file operations.
Add: student["grade"] = "A" [Link](20)
Opening a File: f = open("[Link]", "r")
Update: student["age"] = 22 [Link]()
Modes: "r" – read "w" – write
Delete: del student["age"] Lists are essential for data
"a" – append "b" – binary
Keys & Values: [Link](), manipulation, processing datasets, and
"r+" – read & write [Link]() implementing dynamic structures.
Reading a File: content = [Link]() Dictionaries are used in JSON data, APIs, Q8. Explain dictionaries and their
A collection of modules stored in a
and modeling real-world objects. operations with examples.
Writing to a File: f = open("[Link]", directory with _init_.py.
"w") [Link]("Hello Python") Answer (Long):
Importance:
Q11. Explain modules and packages in
Closing File: [Link]() Reusability of code
Python. Why are they important?
File handling helps process external A dictionary stores data in key–value
Module: A file containing Python code Clean program design
data, logs, and configuration files. pairs.
(functions, classes). import math
Large-scale application development
[Link](25)
Access to thousands of libraries
student = {"name":"Rahul", "age":21}
Package:
Python’s library ecosystem makes it
powerful for analytics and automation.
Operations:
Q1. What is Python? Explain its key Q2. Explain Python data types with Q3. Explain decision-making statements
features and applications in modern examples. How does Python handle in Python with [Link] (Long):
computing. Answer (Long): dynamic typing? Answer (Long):
Python offers three types of conditional
Python is a high-level, interpreted, Python supports several built-in data statements:
object-oriented programming language types:
developed by Guido van Rossum in 1. if Statement:
1. Numeric Types:
1991. It emphasizes readability and Executes a block when a condition is
simplicity, making it ideal for beginners int: whole numbers → x = 10 true.
and advanced users.
float: decimal numbers → pi = 3.14 age = 20
Key Features of Python:
complex: imaginary numbers → z = 5 + if age >= 18:
Easy to Learn & Readable: 2j
print("Eligible to vote")
Python’s syntax is simple and close to 2. Sequence Types:
English, reducing development time. 2. if–else Statement:
str: "Hello"
Interpreted Language: Provides two-way decision-making.
list: [1, 2, 3]
Code is executed line-by-line, helping num = 5
tuple: (4, 5, 6)
easier debugging.
if num % 2 == 0:
3. Mapping Type:
Object-Oriented:
print("Even")
dict: {"name": "Amit", "age": 22}
Supports OOP concepts like classes,
else:
inheritance, and polymorphism. 4. Set Types:
print("Odd")
Cross-Platform: set: {1, 2, 2, 3}
3. if–elif–else Ladder:
Works on Windows, Mac, Linux, frozenset: immutable set
Raspberry Pi, etc. Used for multiple conditions.
5. Boolean Type:
Extensive Libraries: marks = 75
bool: True/False
NumPy, Pandas, Matplotlib, TensorFlow, if marks >= 90:
Django, Flak. Dynamic Typing:
print("A Grade")
Open Source: Python does not require variable type
[Link]: elif marks >= 75:
Free to use and supported by a strong
x = 10 # x is int print("B Grade")
global community.
x = "Hello" # now x is string else: print("C Grade")
Applications of Python:
Data Science & Machine Learning Python automatically assigns the
datatype based on the value assigned.
Business Analytics This increases flexibility and reduces
programming complexity. Decision-making statements allow
Web Development (Django/Flask)
programs to execute logical choices
Automation & Scripting Q10. What is Exception Handling? Explain try–except–finally
based on inputwith
and examples.
conditions.
Artificial Intelligence Answer (Long): Exceptions are runtime errors that stop program execution. Python uses
try–except blocks to handle them. Example:
IoT & Robotics
try: x = 10 / 0 except ZeroDivisionError:
Finance & Trading Algorithms
print("Cannot divide by zero")
Python has become one of the most
widely used programming languages finally:
due to its power and simplicity.
print("Execution completed")
Components:
try: Code that may produce an error except: Handles the error
finally: Always executes Exception handling ensures smooth program execution and