SlideShare a Scribd company logo
1 of 34
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

Similar to function.pptx

Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfDaddy84
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and ModulesOmid AmirGhiasvand
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfprasnt1
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptxAshwini Raut
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 

Similar to function.pptx (20)

Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
C function
C functionC function
C function
 
Functions2.pptx
Functions2.pptxFunctions2.pptx
Functions2.pptx
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and Modules
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 

Recently uploaded

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 

Recently uploaded (20)

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 

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