SlideShare a Scribd company logo
Function
In Python, a function is a group of related statements that performs a specific task.
Functions help break our program into smaller and modular chunks. As our program grows larger and
larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes the code reusable.
Creating Function/Function definition Syntax
def functionname(parameter):
function-suit
return <expression>
• def is keyword.
• The function block is started with colon(:)
• All same level block indentation remain at same indentation
• No. of parameter must be same in function definition and dunction calling
Function calling Syntax
Functionname(parameter)
#Program to add two numbers using function
def add(a,b): #Function definition
c=a+b
return c;
a=int(input("value of a="))
b=int(input("value of b="))
print("sum=",add(a,b)) #calling function add()
Output
value of a=23
value of b=56
sum= 79
Call by reference in Python
Mutable object parameters (arguments) in the Python language are passed by reference. It means if
you change what a parameter refers to within a function, the change also reflects back in the calling
function. For example −list are mutable because these are passing by reference.
def change_list(list1):
list1.append(20)
list1.append(30)
print("list inside function=",list1)
list1=[10,20,40,50]
change_list(list1);
print("list outside function",list1);
Output
list inside function= [10, 20, 40, 50, 20, 30]
list outside function [10, 20, 40, 50, 20, 30]
Call by value in python
In python immutable objects are passed by value. It means if you change what a parameter
refers to within a function, the change does not reflects back in the calling function. Example is string.
def change_string(str):
str=str+"Hello"
print("string inside function",str)
str1="Hi I am here"
change_string(str1)
print("string outside function",str1)
Output
string inside function Hi I am hereHello
string outside function Hi I am here
Function Arguments
You can call a function by using the following types of formal arguments −
1. Required arguments
Required arguments are the arguments passed to a function in correct positional order. Here, the
number of arguments in the function call should match exactly with the function definition.
Example
def simple_interest(p,t,r):
return(p*t*r)/100
p=float(input("Enter the principle amount"))
r=float(input("Enter the rate of interest"))
t=float(input("Enter the time in years"))
print("Simple interest=",simple_interest(p,r,t))
Output
Enter the principle amount10000
Enter the rate of interest5
Enter the time in years2
Simple interest= 1000.0
2. Keyword arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the
caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter is able to use the
keywords provided to match the values with parameters.
Example
def simple_interest(p,t,r):
return(p*t*r)/100
print("Simple interest=",simple_interest(t=10,r=10,p=1900))))
Output
Simple interest= 1900.0
3. Default arguments
A default argument is an argument that assumes a default value if a value is not provided in the function call
for that argument.
Example
def printme(name,age=22):
print("My name is",name,"and age is",age)
printme(name="john")
Output
My name is john and age is 22
4. Variable-length arguments
Sometimes we may not know the number of arguments to be passed in advance. An asterisk (*) is
placed before the variable name that holds the values of all non keyword variable arguments. This tuple
remains empty if no additional arguments are specified during the function call.
Example
def printme(*names):
print("type of passed argument is",type(names))
print("printing the passed arguments...")
for name in names:
print(name)
printme("mohan","ram","shyam","sita")
Output
mohan
ram
shyam
sita
Scope of Variables
All variables in a program may not be accessible at all locations in that program. This depends on where you
have declared a variable.
The scope of a variable determines the portion of the program where you can access a particular identifier.
There are two basic scopes of variables in Python −
•Global variables
•Local variables
Global vs. Local variables
• Variables that are defined inside a function body have a local scope, and those defined outside have a global
scope.
• This means that local variables can be accessed only inside the function in which they are declared, whereas
global variables can be accessed throughout the program body by all functions. When you call a function, the
variables declared inside it are brought into scope.
Example of local and global variable
total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them.
total = arg1 + arg2; # Here total is local variable.
print (("Inside the function local total :"), total)
return total;
# Now you can call sum function
sum( 10, 20 );
print(("Outside the function global total : "), total)
Output
Inside the function local total : 30
Outside the function global total : 0
Functions-”First class citizens”
First Class objects are those objects, which can be handled uniformly. First Class objects can
be stored as Data Structures, as some parameters of some other functions, as control structures
etc. A programming language is said to support first-class functions if it treats functions as first-
class objects.
properties of First Class Functions
• It is an instance of an Object type
• Functions can be stored as variable
• Pass First Class Function as argument of some other functions
• Return Functions from other function
• Store Functions in lists, sets or some other data structures.
Map()
It returns a list of result after applying the given function to each item of a given iterable list or tuple etc.
Example
def cube(x):
return x*x*x
list1=[1,2,3,4,5]
result=map(cube,list1)
print(list(result))
Output
[1, 8, 27, 64, 125]
Filter
It is used for element in sequence to be true of false.

More Related Content

Similar to functioninpython-1.pptx

Function in c
Function in cFunction in c
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam
 
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
 
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
SangeetaBorde3
 
Functions
Functions Functions
Functions
Dr.Subha Krishna
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
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
TeshaleSiyum
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
nikshaikh786
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Functions
FunctionsFunctions
Functions
zeeshan841
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
bhuvanalakshmik2
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 

Similar to functioninpython-1.pptx (20)

Function in c
Function in cFunction in c
Function in c
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
 
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
 
Functions
Functions Functions
Functions
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
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
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Functions
FunctionsFunctions
Functions
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 

Recently uploaded

Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 

Recently uploaded (20)

Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 

functioninpython-1.pptx

  • 1. Function In Python, a function is a group of related statements that performs a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes the code reusable. Creating Function/Function definition Syntax def functionname(parameter): function-suit return <expression> • def is keyword. • The function block is started with colon(:) • All same level block indentation remain at same indentation • No. of parameter must be same in function definition and dunction calling Function calling Syntax Functionname(parameter)
  • 2. #Program to add two numbers using function def add(a,b): #Function definition c=a+b return c; a=int(input("value of a=")) b=int(input("value of b=")) print("sum=",add(a,b)) #calling function add() Output value of a=23 value of b=56 sum= 79
  • 3. Call by reference in Python Mutable object parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example −list are mutable because these are passing by reference. def change_list(list1): list1.append(20) list1.append(30) print("list inside function=",list1) list1=[10,20,40,50] change_list(list1); print("list outside function",list1); Output list inside function= [10, 20, 40, 50, 20, 30] list outside function [10, 20, 40, 50, 20, 30]
  • 4. Call by value in python In python immutable objects are passed by value. It means if you change what a parameter refers to within a function, the change does not reflects back in the calling function. Example is string. def change_string(str): str=str+"Hello" print("string inside function",str) str1="Hi I am here" change_string(str1) print("string outside function",str1) Output string inside function Hi I am hereHello string outside function Hi I am here
  • 5. Function Arguments You can call a function by using the following types of formal arguments − 1. Required arguments Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition. Example def simple_interest(p,t,r): return(p*t*r)/100 p=float(input("Enter the principle amount")) r=float(input("Enter the rate of interest")) t=float(input("Enter the time in years")) print("Simple interest=",simple_interest(p,r,t)) Output Enter the principle amount10000 Enter the rate of interest5 Enter the time in years2 Simple interest= 1000.0
  • 6. 2. Keyword arguments Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name. This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters. Example def simple_interest(p,t,r): return(p*t*r)/100 print("Simple interest=",simple_interest(t=10,r=10,p=1900)))) Output Simple interest= 1900.0
  • 7. 3. Default arguments A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. Example def printme(name,age=22): print("My name is",name,"and age is",age) printme(name="john") Output My name is john and age is 22
  • 8. 4. Variable-length arguments Sometimes we may not know the number of arguments to be passed in advance. An asterisk (*) is placed before the variable name that holds the values of all non keyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call. Example def printme(*names): print("type of passed argument is",type(names)) print("printing the passed arguments...") for name in names: print(name) printme("mohan","ram","shyam","sita") Output mohan ram shyam sita
  • 9. Scope of Variables All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable. The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python − •Global variables •Local variables Global vs. Local variables • Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. • This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope.
  • 10. Example of local and global variable total = 0; # This is global variable. # Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them. total = arg1 + arg2; # Here total is local variable. print (("Inside the function local total :"), total) return total; # Now you can call sum function sum( 10, 20 ); print(("Outside the function global total : "), total) Output Inside the function local total : 30 Outside the function global total : 0
  • 11. Functions-”First class citizens” First Class objects are those objects, which can be handled uniformly. First Class objects can be stored as Data Structures, as some parameters of some other functions, as control structures etc. A programming language is said to support first-class functions if it treats functions as first- class objects. properties of First Class Functions • It is an instance of an Object type • Functions can be stored as variable • Pass First Class Function as argument of some other functions • Return Functions from other function • Store Functions in lists, sets or some other data structures.
  • 12. Map() It returns a list of result after applying the given function to each item of a given iterable list or tuple etc. Example def cube(x): return x*x*x list1=[1,2,3,4,5] result=map(cube,list1) print(list(result)) Output [1, 8, 27, 64, 125]
  • 13. Filter It is used for element in sequence to be true of false.