pYTHON FOR COMPUTING- LAB ACTIVITY
NAME:S.SAI KRISHNA REDDY
ROLL NO:24B81A67B0
BRANCH:CSD-B
1. Write a Python Program to
implement hybrid inheritance.
PROGRAM:
# Base class class
Person:
def display(self): print("I am
a person")
# First level derived class class
Student(Person):
def show(self):
print("I am a student")
# Another class (not related to Student but to be mixed)
class Artist:
def talent(self):
print("I can draw and paint")
# Hybrid class (inherits from both Student and Artist)
class TalentedStudent(Student, Artist): def
identity(self):
print("I am a talented student")
# Create object of TalentedStudent ts
= TalentedStudent()
# Access methods from all parent classes
ts.display() # from Person
ts.show() # from Student
ts.talent() # from Artist
ts.identity() # from TalentedStudent
OUTPUT:
I am a person
I am a student
I can draw and paint
I am a talented student
PROGRAM EXPLANATION:
1. Person class:
This is the base class.
It has a method display() that prints "I am a person".
2. Student class:
This class inherits from Person.
It has its own method show() that prints "I am a student".
3. Artist class:
This is another separate class.
It has a method talent() that prints "I can draw and paint".
4. TalentedStudent class:
This class inherits from both Student and Artist.
That means it can use methods from Person, Student, and Artist.
It also has its own method identity() that prints "I am a
talented student".
5. Creating an object:
We create an object of the TalentedStudent class.
6. Method calls:
ts.display() → calls the method from Person.
ts.show() → calls the method from Student.
ts.talent() → calls the method from Artist.
ts.identity() → calls its own method.
7. Hybrid Inheritance:
This program combines:
o Single Inheritance: Student → Person
o Multiple Inheritance: TalentedStudent → Student and Artist
2. Write a Python program to perform
some mathematical operations on a
numpy array.
PROGRAM:
import numpy as np
# Create a NumPy array
arr = np.array([10, 20, 30, 40, 50])
# Perform mathematical operations
add_result = arr + 5 # Add 5 to each
element
sub_result = arr - 5 # Subtract 5 from
each element
mul_result = arr * 2 # Multiply each
element by 2
div_result = arr / 2 # Divide each
element by 2
square_result = arr ** 2 # Square of each
element
# Display results print("Original
Array:", arr)
print("After Addition:", add_result) print("After
Subtraction:", sub_result) print("After
Multiplication:", mul_result) print("After Division:",
div_result) print("After Squaring:", square_result)
OUTPUT:
Original Array: [10 20 30 40 50]
After Addition: [15 25 35 45 55]
After Subtraction: [ 5 15 25 35 45]
After Multiplication: [ 20 40 60 80
100]
After Division: [ 5. 10. 15. 20. 25.]
After Squaring: [ 100 400 900 1600 2500]
PROGRAM EXPLANATION:
1. import numpy as np
o Imports the NumPy library and gives it a short name
np for easier use.
2. arr = np.array([10, 20, 30, 40, 50])
o Creates a NumPy array with five elements.
3. Mathematical operations:
o arr + 5 → Adds 5 to each element of the array.
o arr - 5 → Subtracts 5 from each element.
o arr * 2 → Multiplies each element by 2.
o arr / 2 → Divides each element by 2.
o arr ** 2 → Squares each element (raises to power 2).
4. Each result is stored in a separate variable:
o add_result, sub_result, mul_result, div_result,
square_result.
5. print() statements:
o Used to display the original array and the results of
each operation