Python Ppt
Python Ppt
1.CODE REUSABILITY:-
Functions allow you to write code once and reuse it multiple times.
Reduces redundancy and effort in writing the same code repeatedly.
4.Modularity:-
Functions divide a program into smaller parts, making it modular and
manageable.
Promotes collaboration by enabling different people to work on
different functions.
5. BETTER MAINTENANCE:-
We can define a function in Python, using the def keyword. We can add
any type of functionalities and properties to it as we require. By the
following example, we can understand how to write a function in
Python. In this way we can create Python function definition by using
def keyword.
PYTHON:-
After creating a function in Python we can call it by using the name of the
functions Python followed by parenthesis containing parameters of that
particular function. Below is the example for calling def function Python.
PYTHON:-
INSTANCE METHODS
1. Instance methods are defined within the class definition and are called on an instance
of the class using the dot notation.
2. “Self” is often the first parameter of an instance method.
3. They can access and modify the attributes of the instance on which they are called.
4. They can also access and call other instance and class methods of the same class.
EXAMPLE :-
Example to find the area of a circle using instance
methods.
• class Circle:
• def __init__(self, radius):
• self.radius = radius
• def area(self):
• return 3.14 * self.radius ** 2
• circle1 = Circle(5)
• print(circle1.area())
OUTPUT:-
78.5
CLASS METHODS:-
Class methods operate on the class itself rather than on an instance. Class methods are
defined within the class definition and are called on the class rather than on an instance.
Examples of class methods:-
• class Employee:
• raise_amount = 1.05 # class variable
• @classmethod
• def set_raise_amount(cls, amount):
• cls.raise_amount = amount
• emp1 = Employee("John", "Doe", 50000) emp2 = Employee("Jane",
"Smith", 60000) Output:
1.05
• print(Employee.raise_amount) 1.10
• Employee.set_raise_amount(1.10)
• print(Employee.raise_amount)
STATIC METHODS:-
Static methods do not take the self or “cls” parameter and cannot access the instance, class
variables, or methods.
• class Math:
• # Define a static method named
"add_numbers" that takes two
parameters "x" and "y"
• @staticmethod
• def add_numbers(x, y):
• # Returns the sum of the two
numbers
• return x + y
Python introduces the new function syntax in Python3.8.2 Version, Where we can introduce
the / forward slash to compare the positional only parameter which comes before the / slash and
parameters that comes after * is keyword only arguments.
Rest of the arguments that are come between / and * can be either positional or keyword type of
argument.
That means we can combine positional arguments and regular arguments in such a way all the
non-positional argument comes after / slash.
SYNTAX:-