SlideShare a Scribd company logo
Prepared by,
Prof. S. S. Gawali
Computer Engineering
 Function is a reusable block of statements used to perform a specific task.
 Function is execute only when we called.
Computer Engineering PSP Prof. S. S. Gawali 2
 The idea of function 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.
Computer Engineering PSP Prof. S. S. Gawali 3
 There are two types of function in Python programming:
 Standard library functions - These are built-in functions in Python that are
available to use.
 Example: type(), input(), print(), int(), float()
 User-defined functions - We can create our own functions based on our
requirements.
4
Computer Engineering PSP Prof. S. S. Gawali
 In Python a function is defined using the def keyword:
 Syntax:
5
Computer Engineering PSP Prof. S. S. Gawali
 def - keyword used to declare a function
 function_name - any name given to the function
 parameters - are place holders that define the parameters that go into the function.
 return (optional) - returns value from a function
Computer Engineering PSP Prof. S. S. Gawali 6
 The terms parameter and argument can be used for the same thing: information that
are passed into a function.
 From a function's perspective:
 A parameter is the variable listed inside the parentheses in the function definition.
 An argument is the value that is sent to the function when it is called.
7
Computer Engineering PSP Prof. S. S. Gawali
def hello():
''' Function to print Hello.'‘’
print("Hello")
Computer Engineering PSP Prof. S. S. Gawali 8
 After creating a function we can call it by using the name of the function followed by
parenthesis containing parameters of that particular function.
Computer Engineering PSP Prof. S. S. Gawali 9
def hello():
''' Function to print hello.'‘’
print("Hello")
hello()
Output
Hello
Computer Engineering PSP Prof. S. S. Gawali 10
 1. Positional arguments.
 2. Keyword arguments.
 3. Default arguments.
 4. Variable length arguments.
Computer Engineering PSP Prof. S. S. Gawali 11
 A positional parameter/argument in Python is a parameter/ an argument whose position
matters in a function call.
 Syntax: call_func(arg1, arg2, arg3)
 Example: Define a function that shows info about a person given the name and age:
def p_info(name, age):
'''Person infrormation.'‘’
print(f"Hi, I am {name}. I am {age} years old.")
#Call this function with two positional arguments name and age.
p_info("Rina",31)
Output:
Hi, I am Rina. I am 31 years
old.
Computer Engineering PSP Prof. S. S. Gawali 12
 A keyword argument in Python means that a function argument has a name label.
 Syntax: call_func(arg_name=arg1)
 Example: Define a function that shows info about a person given the name and age:
 def p_info(name, age):
 '''Person infrormation.'''
 print(f"Hi, I am {name}. I am {age} years old.")
 p_info(name="Siya",age=40)
 p_info(age=31,name="Rina")
Output:
Hi, I am Siya. I am 40 years
old
Hi, I am Rina. I am 31 years
old
Computer Engineering PSP Prof. S. S. Gawali 13
 Default arguments in Python represent the function arguments that will be used if no
arguments are passed to the function call.
 The default arguments are represented as parameter_name = value in the function
definition.
 Syntax: call_func(par_name=arg1)
 Example: Define a function that shows info about a person given the name and age:
def p_info(name="Priya", age=20): # function creation with default parameter/ argument
'''Person infrormation.'''
print(f"Hi, I am {name}. I am {age} years old")
p_info(name="Siya",age=40)
p_info()
Output:
Hi, I am Siya. I am 40 years
old
Hi, I am Priya. I am 20 years
old
Computer Engineering PSP Prof. S. S. Gawali 14
 Return statement use to return values.
 Function can take input values as parameters and execute statements, and
returns output to caller with return statement.
 Two way to return value from function:
 Return a value
 Return multiple values
Computer Engineering PSP Prof. S. S. Gawali 15
 To let a function return a value, use the return statement:
 Example:
def sq(x):
return x * x
print(sq(3))
print(sq(5))
print(sq(9))
Output:
9
25
81
Computer Engineering PSP Prof. S. S. Gawali 16
 In python, a function can return any number of values.
 Example:
def sq_cu(x):
return x * x, x*x*x
print(sq_cu(10)
a,b=sq_cu(5)
print(a)
print(b)
Output:
(100,1000)
25
125
Computer Engineering PSP Prof. S. S. Gawali 17
 Variable: Variables are the containers for storing data values.
 Scope of Variable: The location where we can find a variable and also access it if
required is called the scope of a variable.
 Based on the scope, we can classify Python variables into three types:
 1. Local Variables
 2. Global Variables
Computer Engineering PSP Prof. S. S. Gawali 18
 The variables which are declared outside of function are called global variables.
 These variables can be accessed in all functions of that module.
 Example: Create global variable to Print Square and cube of number using function.
a=5 # a is global variable
def sq():
print(a*a)
def cu():
print(a*a*a)
sq()
cu()
Output
25
125
Computer Engineering PSP Prof. S. S. Gawali 19
 The variables which are declared inside a function are called local variables.
 Local variables are available only for the function in which we declared it. i.e from
outside of the
 function we cannot access.
 Example: Create a function to display the square of numbers using local variable.
def sq():
a=5 # a is local variable
print(a*a)
sq()
Output
25
Computer Engineering PSP Prof. S. S. Gawali 20
 Sometimes we can declare a function without any name, such types of nameless
functions are called anonymous functions or lambda functions.
 The main purpose of the anonymous function is just for instant use (i.e for one-time
usage)
Computer Engineering PSP Prof. S. S. Gawali 21
 We can define by using lambda keyword
 Syntax:
 lambda arg_list:expression
 Example
 lambda n:n*n # to calculate n square
Computer Engineering PSP Prof. S. S. Gawali 22
 A Python module is a file containing Python definitions and statements.
 A module can define functions, classes, and variables.
 A module can also include runnable code.
 Grouping related code into a module makes the code easier to understand and use.
 It also makes the code logically organized.
Computer Engineering PSP Prof. S. S. Gawali 23
 Built-in Modules.
 User-defined Modules.
Computer Engineering PSP Prof. S. S. Gawali 24
 One of the feature of Python is “rich standard library”.
 This rich standard library contains lots of built-in modules.
 Hence, it provides a lot of reusable code.
 To name a few, Python contains modules like “math”, “sys”, “datetime”, “random”.
Computer Engineering PSP Prof. S. S. Gawali 25
 User can create module as per requirement is called user defined module.
Computer Engineering PSP Prof. S. S. Gawali 26
 The import keyword to import both built-in and user-defined modules in Python.
 Different way to import module
 Syntax 1: import module_name
 Syntax 2: import module_name as rename_of_module
 Syntax 3: from module_name import method/constant-variable
 Syntax 4: from module_name import method1/constant-var1, method2/constant-
var2…..
 Syntax 5: from module_name import *
 Syntax6: from module_name import method/constant as rename_of_method/constant
Computer Engineering PSP Prof. S. S. Gawali 27
import module_name
 Example:
import math
a=5
print(math.sqrt(a))
print(math.factorial(a))
print(math.pi)
Output:
2.23606797749979
120
3.141592653589793
Computer Engineering PSP Prof. S. S. Gawali 28
import module_name as rename_of_module
 Example:
import math as m
a=int(input(“Enter no.”))
print(sqrt(a)) Output:
Enter no. 16
4.0
Computer Engineering PSP Prof. S. S. Gawali 29
from module_name import method/constant-variable
 Example:
from math import sqrt
a=int(input(“Enter no.”))
print(sqrt(a))
Output:
Enter no. 16
4.0
Computer Engineering PSP Prof. S. S. Gawali 30
from module_name import method1/constant-variable1, method2/constant-
variable2…..
 Example:
from math import sqrt, pi, pow
a=int(input(“Enter no.”))
print(sqrt(a))
print(pi)
print(pow(a,3))
Output:
Enter no.2
1.4142135623730951
3.141592653589793
8.0
Computer Engineering PSP Prof. S. S. Gawali 31
from module_name import *
 Example:
from math import *
a=int(input(“Enter no.”))
print(sqrt(a))
print(pi)
print(pow(a,3))
Output:
Enter no.3
1.7320508075688772
3.141592653589793
27.0
Computer Engineering PSP Prof. S. S. Gawali 32
from module_name import method/constant as rename_of_method/constant
 Example:
from math import factorial as f
a=int(input(“Enter no.”))
print(f(a)) Output:
Enter no.5
120
Computer Engineering PSP Prof. S. S. Gawali 33
Computer Engineering PSP Prof. S. S. Gawali 34

More Related Content

PPTX
ESIT135: Unit 3 Topic: functions in python
PPTX
Unit 2function in python.pptx
PPTX
Unit 2function in python.pptx
PPTX
Functions in Python Programming Language
PDF
Unit 3 (1)
PPTX
Functionincprogram
PDF
Functions_21_22.pdf
PDF
Functions_19_20.pdf
ESIT135: Unit 3 Topic: functions in python
Unit 2function in python.pptx
Unit 2function in python.pptx
Functions in Python Programming Language
Unit 3 (1)
Functionincprogram
Functions_21_22.pdf
Functions_19_20.pdf

Similar to function.pptx (20)

PDF
Functionscs12 ppt.pdf
PDF
Functions.pdf
PDF
Functions2.pdf
PPTX
Python Functions.pptx
PPTX
Python Functions.pptx
PPTX
C function
PPTX
cbse class 12 Python Functions2 for class 12 .pptx
PPTX
Function oneshot with python programming .pptx
PPTX
Functions2.pptx
PDF
Functions.pdf cbse board latest 2023-24 all covered
PPT
Basic information of function in cpu
PPTX
JNTUK python programming python unit 3.pptx
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PPTX
Lecture_5_-_Functions_in_C_Detailed.pptx
PDF
Python Programming - Functions and Modules
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PDF
Functions2.pdf
PPTX
functions.pptx
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
Functionscs12 ppt.pdf
Functions.pdf
Functions2.pdf
Python Functions.pptx
Python Functions.pptx
C function
cbse class 12 Python Functions2 for class 12 .pptx
Function oneshot with python programming .pptx
Functions2.pptx
Functions.pdf cbse board latest 2023-24 all covered
Basic information of function in cpu
JNTUK python programming python unit 3.pptx
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Lecture_5_-_Functions_in_C_Detailed.pptx
Python Programming - Functions and Modules
CHAPTER 01 FUNCTION in python class 12th.pptx
Functions2.pdf
functions.pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
Ad

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Well-logging-methods_new................
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
web development for engineering and engineering
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Artificial Intelligence
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Current and future trends in Computer Vision.pptx
PDF
PPT on Performance Review to get promotions
DOCX
573137875-Attendance-Management-System-original
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
OOP with Java - Java Introduction (Basics)
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Well-logging-methods_new................
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mechanical Engineering MATERIALS Selection
Fundamentals of safety and accident prevention -final (1).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Construction Project Organization Group 2.pptx
additive manufacturing of ss316l using mig welding
web development for engineering and engineering
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Foundation to blockchain - A guide to Blockchain Tech
Artificial Intelligence
R24 SURVEYING LAB MANUAL for civil enggi
Current and future trends in Computer Vision.pptx
PPT on Performance Review to get promotions
573137875-Attendance-Management-System-original
Ad

function.pptx

  • 1. Prepared by, Prof. S. S. Gawali Computer Engineering
  • 2.  Function is a reusable block of statements used to perform a specific task.  Function is execute only when we called. Computer Engineering PSP Prof. S. S. Gawali 2
  • 3.  The idea of function 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. Computer Engineering PSP Prof. S. S. Gawali 3
  • 4.  There are two types of function in Python programming:  Standard library functions - These are built-in functions in Python that are available to use.  Example: type(), input(), print(), int(), float()  User-defined functions - We can create our own functions based on our requirements. 4 Computer Engineering PSP Prof. S. S. Gawali
  • 5.  In Python a function is defined using the def keyword:  Syntax: 5 Computer Engineering PSP Prof. S. S. Gawali
  • 6.  def - keyword used to declare a function  function_name - any name given to the function  parameters - are place holders that define the parameters that go into the function.  return (optional) - returns value from a function Computer Engineering PSP Prof. S. S. Gawali 6
  • 7.  The terms parameter and argument can be used for the same thing: information that are passed into a function.  From a function's perspective:  A parameter is the variable listed inside the parentheses in the function definition.  An argument is the value that is sent to the function when it is called. 7 Computer Engineering PSP Prof. S. S. Gawali
  • 8. def hello(): ''' Function to print Hello.'‘’ print("Hello") Computer Engineering PSP Prof. S. S. Gawali 8
  • 9.  After creating a function we can call it by using the name of the function followed by parenthesis containing parameters of that particular function. Computer Engineering PSP Prof. S. S. Gawali 9
  • 10. def hello(): ''' Function to print hello.'‘’ print("Hello") hello() Output Hello Computer Engineering PSP Prof. S. S. Gawali 10
  • 11.  1. Positional arguments.  2. Keyword arguments.  3. Default arguments.  4. Variable length arguments. Computer Engineering PSP Prof. S. S. Gawali 11
  • 12.  A positional parameter/argument in Python is a parameter/ an argument whose position matters in a function call.  Syntax: call_func(arg1, arg2, arg3)  Example: Define a function that shows info about a person given the name and age: def p_info(name, age): '''Person infrormation.'‘’ print(f"Hi, I am {name}. I am {age} years old.") #Call this function with two positional arguments name and age. p_info("Rina",31) Output: Hi, I am Rina. I am 31 years old. Computer Engineering PSP Prof. S. S. Gawali 12
  • 13.  A keyword argument in Python means that a function argument has a name label.  Syntax: call_func(arg_name=arg1)  Example: Define a function that shows info about a person given the name and age:  def p_info(name, age):  '''Person infrormation.'''  print(f"Hi, I am {name}. I am {age} years old.")  p_info(name="Siya",age=40)  p_info(age=31,name="Rina") Output: Hi, I am Siya. I am 40 years old Hi, I am Rina. I am 31 years old Computer Engineering PSP Prof. S. S. Gawali 13
  • 14.  Default arguments in Python represent the function arguments that will be used if no arguments are passed to the function call.  The default arguments are represented as parameter_name = value in the function definition.  Syntax: call_func(par_name=arg1)  Example: Define a function that shows info about a person given the name and age: def p_info(name="Priya", age=20): # function creation with default parameter/ argument '''Person infrormation.''' print(f"Hi, I am {name}. I am {age} years old") p_info(name="Siya",age=40) p_info() Output: Hi, I am Siya. I am 40 years old Hi, I am Priya. I am 20 years old Computer Engineering PSP Prof. S. S. Gawali 14
  • 15.  Return statement use to return values.  Function can take input values as parameters and execute statements, and returns output to caller with return statement.  Two way to return value from function:  Return a value  Return multiple values Computer Engineering PSP Prof. S. S. Gawali 15
  • 16.  To let a function return a value, use the return statement:  Example: def sq(x): return x * x print(sq(3)) print(sq(5)) print(sq(9)) Output: 9 25 81 Computer Engineering PSP Prof. S. S. Gawali 16
  • 17.  In python, a function can return any number of values.  Example: def sq_cu(x): return x * x, x*x*x print(sq_cu(10) a,b=sq_cu(5) print(a) print(b) Output: (100,1000) 25 125 Computer Engineering PSP Prof. S. S. Gawali 17
  • 18.  Variable: Variables are the containers for storing data values.  Scope of Variable: The location where we can find a variable and also access it if required is called the scope of a variable.  Based on the scope, we can classify Python variables into three types:  1. Local Variables  2. Global Variables Computer Engineering PSP Prof. S. S. Gawali 18
  • 19.  The variables which are declared outside of function are called global variables.  These variables can be accessed in all functions of that module.  Example: Create global variable to Print Square and cube of number using function. a=5 # a is global variable def sq(): print(a*a) def cu(): print(a*a*a) sq() cu() Output 25 125 Computer Engineering PSP Prof. S. S. Gawali 19
  • 20.  The variables which are declared inside a function are called local variables.  Local variables are available only for the function in which we declared it. i.e from outside of the  function we cannot access.  Example: Create a function to display the square of numbers using local variable. def sq(): a=5 # a is local variable print(a*a) sq() Output 25 Computer Engineering PSP Prof. S. S. Gawali 20
  • 21.  Sometimes we can declare a function without any name, such types of nameless functions are called anonymous functions or lambda functions.  The main purpose of the anonymous function is just for instant use (i.e for one-time usage) Computer Engineering PSP Prof. S. S. Gawali 21
  • 22.  We can define by using lambda keyword  Syntax:  lambda arg_list:expression  Example  lambda n:n*n # to calculate n square Computer Engineering PSP Prof. S. S. Gawali 22
  • 23.  A Python module is a file containing Python definitions and statements.  A module can define functions, classes, and variables.  A module can also include runnable code.  Grouping related code into a module makes the code easier to understand and use.  It also makes the code logically organized. Computer Engineering PSP Prof. S. S. Gawali 23
  • 24.  Built-in Modules.  User-defined Modules. Computer Engineering PSP Prof. S. S. Gawali 24
  • 25.  One of the feature of Python is “rich standard library”.  This rich standard library contains lots of built-in modules.  Hence, it provides a lot of reusable code.  To name a few, Python contains modules like “math”, “sys”, “datetime”, “random”. Computer Engineering PSP Prof. S. S. Gawali 25
  • 26.  User can create module as per requirement is called user defined module. Computer Engineering PSP Prof. S. S. Gawali 26
  • 27.  The import keyword to import both built-in and user-defined modules in Python.  Different way to import module  Syntax 1: import module_name  Syntax 2: import module_name as rename_of_module  Syntax 3: from module_name import method/constant-variable  Syntax 4: from module_name import method1/constant-var1, method2/constant- var2…..  Syntax 5: from module_name import *  Syntax6: from module_name import method/constant as rename_of_method/constant Computer Engineering PSP Prof. S. S. Gawali 27
  • 28. import module_name  Example: import math a=5 print(math.sqrt(a)) print(math.factorial(a)) print(math.pi) Output: 2.23606797749979 120 3.141592653589793 Computer Engineering PSP Prof. S. S. Gawali 28
  • 29. import module_name as rename_of_module  Example: import math as m a=int(input(“Enter no.”)) print(sqrt(a)) Output: Enter no. 16 4.0 Computer Engineering PSP Prof. S. S. Gawali 29
  • 30. from module_name import method/constant-variable  Example: from math import sqrt a=int(input(“Enter no.”)) print(sqrt(a)) Output: Enter no. 16 4.0 Computer Engineering PSP Prof. S. S. Gawali 30
  • 31. from module_name import method1/constant-variable1, method2/constant- variable2…..  Example: from math import sqrt, pi, pow a=int(input(“Enter no.”)) print(sqrt(a)) print(pi) print(pow(a,3)) Output: Enter no.2 1.4142135623730951 3.141592653589793 8.0 Computer Engineering PSP Prof. S. S. Gawali 31
  • 32. from module_name import *  Example: from math import * a=int(input(“Enter no.”)) print(sqrt(a)) print(pi) print(pow(a,3)) Output: Enter no.3 1.7320508075688772 3.141592653589793 27.0 Computer Engineering PSP Prof. S. S. Gawali 32
  • 33. from module_name import method/constant as rename_of_method/constant  Example: from math import factorial as f a=int(input(“Enter no.”)) print(f(a)) Output: Enter no.5 120 Computer Engineering PSP Prof. S. S. Gawali 33
  • 34. Computer Engineering PSP Prof. S. S. Gawali 34