python 5m
python 5m
Python provides built-in functions for creating, writing, and reading files. Two types of files can
be handled in Python, normal text files, csv files and binary files.
File Reading Process
Open the File: Use the open() function to open a file. You need to specify the file name and the
mode
Read the File: Use methods like .read(), .readline(), or .readlines() to read the contents of the
file.
Close the File: Use the .close() method to close the file after you're done.
File Writing Process
Open the File: Use the open() function with the mode 'w' (write) or 'a' (append).
Write to the File: Use the .write() or .writelines() methods to write data to the file.
Close the File: Use the .close() method to close the file.
Here’s a complete example demonstrating both reading from and writing to a file:
file_name = 'example.txt'
3.Multilevel Inheritance :
In multilevel inheritance, features of the base class and the derived class are further inherited into the
new derived class.
4.Hierarchical Inheritance:
When more than one derived class are created from a single base this type of inheritance is called
hierarchical inheritance.
5. Hybrid Inheritance:
Inheritance consisting of multiple types of inheritance is called hybrid inheritance.
Ex-
s = "Hello, Python!"
s2 = s[0:5]
print(s2)
joining operations
The join() function in Python is used to merge items from a collection, such as a list or tuple, into a
single string.
It takes a separator and combines the elements of the collection with that separator in between.
For example, if you have a list of words ["Hello", "world", "Python"] and want to join them
with spaces, you would use join() like this:
The result would be "Hello world Python". You can choose any separator, such as a comma,
dash, or even no separator at all.
this makes it easy to create strings from collections without needing complex
loops or logic.
3. Explain traversing a list in python with suitable example.
Traversing a list in Python means accessing each element of the list sequentially, typically to
perform some operation on it.
Python provides several ways to traverse a list, including for loops, while loops, and list
comprehensions.
1. Using a for Loop
# Example
numbers = [10, 20, 30, 40, 50]
# Traverse using a for loop
for number in numbers:
print(number)
2. Using for Loop with Indexes
# Example
numbers = [10, 20, 30, 40, 50]
# Traverse using index
for i in range(len(numbers)):
print(f"Index {i}: {numbers[i]}")
3. Using enumerate()
# Example
numbers = [10, 20, 30, 40, 50]
# Traverse with enumerate
for index, value in enumerate(numbers):
print(f"Index {index}: {value}")
4. Using a while Loop
# ExampleS
numbers = [10, 20, 30, 40, 50]
# Traverse using while loop
i=0
while i < len(numbers):
print(numbers[i])
i += 1
4. Distinguish between single and multiple Inheritance with examples.
Single Inheritance
Single inheritance occurs when a class inherits from only one parent class.
This means that the child class can access the properties and methods of a single parent class.
Multiple Inheritance
Multiple inheritance occurs when a class inherits from more than one parent class.
This allows the child class to access attributes and methods from multiple parent classes.
Key Differences:
Aspect Single Inheritance Multiple Inheritance
2. Import Matplotlib
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot
plt.plot(x, y)
# Customization
plt.title("Simple Line Graph")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Display
plt.show()
7. Explain the key features of python.
Python is a high-level, interpreted programming language known for its simplicity and
readability. Created by Guido van Rossum and first released in 1991
For Example:
if 10 > 5:
print("This is true!")
print("I am tab indentation")
The first two print statements are indented by 4 spaces, so they belong to the if block.
The third print statement is not indented, so it is outside the if block.
9. Write a python program for filter () to filter only even numbers from a given list.
Here's a Python program using the filter() function to filter only even numbers from a given list:
# Function to check if a number is even
def is_even(num):
return num % 2 == 0
Example:
my_tuple = (10, 20, 30, 40, 50)
print(my_tuple[0]) # Access first element (10)
print(my_tuple[3]) # Access fourth element (40)
print(my_tuple[-1]) # Access last element (50)
2. Slicing
Slicing
s
is used to access a subset (a slice) of the tuple.
The slice is defined by a start index, an end index, and an optional step.
Syntax:
tuple[start:end:step]
Example:
2. Object:
An object is an instance of a class. It represents a specific example of the class with actual values
for the attributes defined in the class.
An object consists of:
State: It is represented by the attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an object
to other objects.
Identity: It gives a unique name to an object and enables one object to interact with other
objects.
Example:
# instantiating a class
class Dog:
attr1 = "mammal"
attr2 = "dog"
# A sample method
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)
Rodger = Dog()