0% found this document useful (0 votes)
7 views4 pages

imp python

The document explains key concepts in Python, including objects, inheritance, file handling, and NumPy. It covers the importance of inheritance, types of inheritance, file modes, and structured arrays, as well as string manipulation methods and their applications. Additionally, it discusses lambda functions, list comprehensions, and the characteristics of lists, along with built-in functions for list operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

imp python

The document explains key concepts in Python, including objects, inheritance, file handling, and NumPy. It covers the importance of inheritance, types of inheritance, file modes, and structured arrays, as well as string manipulation methods and their applications. Additionally, it discusses lambda functions, list comprehensions, and the characteristics of lists, along with built-in functions for list operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. What is an object in Python? Explain with example.

An object in Python is an instance of a class. It is a real-world entity that contains data


(variables) and methods (functions).

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.

Types of Inheritance (any two)

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.

File Handling allows us to create, read, write, and delete files.


Four Attributes (Modes)
• 'r' → Read mode
• 'w' → Write mode
• 'a' → Append mode
• 'r+' → Read and Write mode

4. What are structured arrays in NumPy? Explain their significance in handling


complex datasets.
Structured arrays in NumPy allow you to store complex datasets where each element
can have multiple fields (like a table with columns).
Significance: They are useful to handle complex data (like records with multiple data
types).

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

Slow for large data Faster and efficient

Many built-in functions


No built-in functions for maths

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]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy