[go: up one dir, main page]

0% found this document useful (0 votes)
2 views5 pages

Python 3 4 PRGMS

The document provides a Python program to calculate the factorial of a number using a recursive function, illustrating the concept of recursion. It also explains inheritance and polymorphism in Object Oriented Programming, showcasing how a base class can be extended into derived classes and how polymorphic functions can operate on different object types. Examples include a base class 'Person' with derived classes 'Student' and 'Professor', demonstrating method overriding and polymorphic behavior.

Uploaded by

ajaykumarak0531
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)
2 views5 pages

Python 3 4 PRGMS

The document provides a Python program to calculate the factorial of a number using a recursive function, illustrating the concept of recursion. It also explains inheritance and polymorphism in Object Oriented Programming, showcasing how a base class can be extended into derived classes and how polymorphic functions can operate on different object types. Examples include a base class 'Person' with derived classes 'Student' and 'Professor', demonstrating method overriding and polymorphic behavior.

Uploaded by

ajaykumarak0531
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/ 5

*Write a program to find factorial of given number using Recursive function:

Factorial recursion is a method in which a function directly or indirectly calls


itself. In mathematics, Factorial means the product of all the
positive integers from 1 to that number. An exclamation mark is used after the
integer to show that it’s a factorial.
For example, factorial eight is 8! So, it means multiplication of all the integers
from 8 to 1 that equals 40320. Factorial of zero is 1. We can find a factorial of a
number using python. There are various ways of finding; The Factorial of a
number in python. Among which recursion is the most commonly used.

Program:

def recursive_factorial(n):
if n == 1:
return n
else:
return n*recursive_factorial(n-1) #function calling itself
#taking input from the user
number = int(input("User Input : "))
print("The factorial of", number, "is", recursive_factorial(number))
output:
User Input: 4
The factorial of 4 is 24
Write a program to implement inheritance and polymorphism
Inheritance
One of the major advantages of Object Oriented Programming is re-use.
Inheritance is one of the mechanisms to achieve the same. Inheritance allows
programmer to create a general or a base class first and then later extend it to
more specialized class. It allows programmer to write better code.
syntax to create a derived class is −
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Polymorphism (“MANY FORMS”)
Polymorphism is an important feature of class definition in Python that is
utilized when you have commonly named methods across classes or subclasses.
This permits functions to use entities of different types at different times. So, it
provides flexibility and loose coupling so that code can be extended and easily
maintained over time.
This allows functions to use objects of any of these polymorphic classes without
needing to be aware of distinctions across the classes.
PROGRAM:
# Base class
class Person:
def __init__(self, name):
self.name = name

def role(self):
pass
# Derived class 1
class Student(Person):
def role(self):
return f"{self.name} is a student studying."

# Derived class 2
class Professor(Person):
def role(self):
return f"{self.name} is a professor teaching."

# Function demonstrating polymorphism


def person_role(person):
print(person.role())

# Creating objects of derived classes


student = Student("Alice")
professor = Professor("Dr. Smith")

# Calling the polymorphic function


person_role(student)
person_role(professor)

In this example:
1. Person is the base class with a constructor (__init__) to initialize the
name of the person and an empty method role.
2. Student and Professor are derived classes that inherit from Person and
override the role method.
3. The person_role function demonstrates polymorphism by calling the role
method on different types of Person objects (Student and Professor).
This simple program shows how inheritance allows Student and Professor to
inherit properties from the base class Person, and how polymorphism enables
the person_role function to work with different person objects.

You might also like