0% found this document useful (0 votes)
4 views

Python Ppt

FUCTION PPT

Uploaded by

debupanda2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Ppt

FUCTION PPT

Uploaded by

debupanda2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PRESENTED BY- GUIDED BY -

 DEBASISH PANDA  MR:-TRILOCHAN SAHOO


REG NO:(240720100091)
 SUPRAKASH DHAL
REG NO:(240720100089)
 WHAT IS FUNCTION IN PYTHON
 WHAT IS METHOD IN PYTHON
 FUNCTION VS METHOD
 POSITIONAL-ONLY PARAMETERS
 DOCSTRING IN PYTHON FUNCTION
PYTHON FUNCTIONS
Python Functions is a block of statements that return the specific task. The idea is to
put some commonly or repeatedly done tasks together and make a function so that
instead of writing the same code again and again for different inputs, we can do the
function calls to reuse code contained in it over and over again.

USING FUNCTIONS IN PYTHON OFFERS SEVERAL BENEFITS, INCLUDING:-

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.

2.IMPROVED CODE READABILITY:-

 Functions help to organize code into smaller, logical sections,


making it easier to read and understand.
3. SIMPLIFIED DEBUGGING:-

 Debugging is easier because you can test individual functions


separately.

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:-

 Changes in the function code automatically affect all its


usages, ensuring consistency.

 Easier to update or modify the code.


PYTHON FUNCTION DECLARATION:-

THE SYNTAX TO DECLARE A FUNCTION IS:

Syntax of Python Function Declaration


CREATING A FUNCTION IN PYTHON:-

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:-

• # A simple Python function


• def fun():
• print("Welcome to GFG")
CALLING A FUNCTION IN 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:-

• # A simple Python function


• def fun():
• print("Welcome to GFG")
• fun()
Python methods are functions that are associated with an object or a class in
Python programming. They are used to perform specific tasks or operations on
the data stored within objects or classes.

•Types of methods in Python


• Instance Methods
• Class Methods
• Static Methods
TYPES OF METHODS IN PYTHON
In Python, there are three types of methods: instance, class, and static.
1.Instance methods are associated with an instance of a class and can modify the data stored
within the instance.
2.Class methods are associated with the class rather than an instance and can access and
modify class-level data.
3.Static methods are similar to functions outside of the class and cannot access any instance
or class data.

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

• def __init__(self, first, last, salary):


• self.first = first
• self.last = last
• self.salary = salary

• @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

• #Create an instance of the Math


class and call the "add_numbers"
method, passing in two arguments OUTPUT:-
5
• print(Math.add_numbers(2, 3))
FUNCTION VS METHOD
POSITIONAL-ONLY PARAMETER IN PYTHON:-

 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:-

• def function(a, b, /, c, d, *, e, f):


• #Function Body
• pass
PYTHON DOCSTRINGS:-

 When it comes to writing clean, well-documented code, Python developers have a


secret weapon at their disposal – docstrings. Docstrings, short for documentation
strings, are vital in conveying the purpose and functionality of Python functions,
modules, and classes.

WHAT ARE THE DOCSTRINGS IN PYTHON?

 Python documentation strings (or docstrings) provide a convenient way of associating


documentation with Python modules, functions, classes, and methods. It’s specified in
source code that is used, like a comment, to document a specific segment of code.
Unlike conventional source code comments, the docstring should describe what the
function does, not how
 Declaring Docstrings: The docstrings are declared using ”’triple single quotes”’ or “”” triple
double quotes “”” just below the class, method, or function declaration. All functions
should have a docstring.
 Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the
object or using the help function. The below examples demonstrate how to declare and
access a docstring.

You might also like