imp python
imp python
What is the importance of inheritance in Python? Describe any two types of inheritance with
example.
Importance of Inheritance:
Inheritance allows one class (child) to reuse the properties and methods of another class
(parent). This reduces code duplication and increases code reusability.
1. Single Inheritance
2. Multiple Inheritance
3. What is File handling? Write four attributes of file handling. Explain with example to
open and write the file using file handling in python, also display the content of the file.
5. What is NumPy? Differentiate between List and NumPy? How do we create the
object of NumPy? Explain with example.
NumPy is a Python library used for numerical computing. It provides support for large
arrays, matrices, and many mathematical functions.
List NumPy Array
Can store different data types Stores elements of same data type
6. What is the purpose of using indexing and slicing in NumPy? Explain comparisons of
element in NumPy with example.
Indexing and Slicing:
Used to access or modify elements in an array.
Example (Indexing & Slicing)
Discuss the functionality of the break, continue, pass, and else statements when used
with loops. Provide detailed code examples.
• break: Exits the loop immediately.
python
Copy code
for i in range(5):
if i == 3:
break
print(i)
# Output: 0 1 2
• continue: Skips the current iteration and moves to the next.
python
Copy code
for i in range(5):
if i == 3:
continue
print(i)
# Output: 0 1 2 4
• pass: Does nothing; just a placeholder.
python
Copy code
for i in range(5):
if i == 3:
pass
print(i)
# Output: 0 1 2 3 4
• else with loops: Executes when the loop finishes normally (not interrupted by break).
python
Copy code
for i in range(3):
print(i)
else:
print("Loop finished")
# Output: 0 1 2 Loop finished
2. Explain the methods available for string manipulation in Python. How do these
methods contribute to text processing and data cleaning tasks?
Common string methods
• .lower() → Converts to lowercase
• .upper() → Converts to uppercase
• .strip() → Removes whitespace
• .replace(old, new) → Replaces substring
• .split(delimiter) → Splits string into list
• .join(list) → Joins list into string
• .find(substring) → Finds position
• .isdigit() → Checks if string is numeric
Example
python
Copy code
s = " Hello World! "
print(s.strip().lower()) # Output: hello world!
Contribution to text processing/data cleaning
• Removes unwanted spaces
• Converts text into a uniform format
• Replaces/cleans unwanted characters
• Splits text into words or tokens
3. Analyze the role of lambda functions and list comprehensions in Python. How do
they contribute to writing concise and efficient code? Provide code snippets.
• Lambda function → Small anonymous function (one-line)
python
Copy code
square = lambda x: x * x
print(square(5)) # Output: 25
• List Comprehension → Short way to create a list
python
Copy code
squares = [x * x for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
Contribution
• Makes code shorter and cleaner
• Reduces use of loops and traditional functions
4. Define a list and explain how it differs from other data structures. List any five built-
in functions used with lists in Python.
List → Ordered, mutable collection of items.
python
Copy code
my_list = [1, 2, 3, 4]
Differences
• Mutable → We can change items
• Ordered → Maintains the sequence
• Can store different types of data (int, str, float)
Five built-in functions
• len(list) → Length of list
• append(item) → Add item to end
• remove(item) → Remove item
• sort() → Sort list
• reverse() → Reverse list
Example
python
Copy code
my_list = [3, 1, 2]
my_list.append(4)
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4]