際際滷

際際滷Share a Scribd company logo
AXL IT Academy
Functions
In Python, a function can be defined as a named block of code. We use
functions mainly to avoid code repetition. We can define that action only once
using a function and call that function whenever required to do the same
activity.
The benefit of using a function is code reusability and modularity. Function
improves efficiency and reduces errors because of the reusability of a code.
Once we create a function, we can call it anywhere and anytime.
Creating a Function
In Python a function is defined using the def keyword. The def stands for
define. It is used for telling the interpreter that it is a user defined function. The
def is followed by function name, which is followed by parameters(Optional).
Define the function body with a block of code.
Syntax:
functionNameHere(parameters):
def
#your code here
AXL IT Academy
Note: There is no need to specify curly braces for the function body, in python.
Only indentation is needed to separate code blocks.
Calling a Function
To call a function, use the function name followed by parenthesis, which is
followed by arguments (if any).
:
Note function definitions cannot be empty, but if you for some reason have a
function definition with no content, put in the pass statement to avoid getting
an error.
Arguments
AXL IT Academy
Arguments are specified after the function name, inside the parentheses, for
passing information into function. You can add as many arguments as you
want, just separate them with a comma.
Parameters and Arguments
1. A parameter is the variable listed inside the parentheses in the function
definition.
2. An argument is the value that is sent to the function when it is called.
Recursion
Recursion means that a defined function calls itself. When written correctly
recursion can be a very useful and effective.
AXL IT Academy
Example:
#Recursion function which prints upto 99 without for/while
recur (k):
def
if(k <100):
(k)
print
k+=1
( k )
recur
#calling recur
recur (1)
Function Assignment
We can assign a function to a variable in the same way as we assign a value
to it. You can even use those variables to call the function as well.
def proc():
return "Welcome"
procAlias= proc
print(proc)
print(procAlias)
print(proc ())
AXL IT Academy
print(procAlias())
Return in Function
Return means the task is completed and now you need to return to where you
came, with the specified data. You can return any type of data, you can even
return a function. This should be the last statement in a function.
def proc():
return "Bon Voyage"
print(proc ())
Inner Function
A function which is defined inside another function is known as inner function or
nested function. Nested functions are able to access variables of the enclosing
scope.
Types of function arguments.
There are various ways to use arguments in a function. In Python, we have the
following 4 types of function arguments.
AXL IT Academy
1. Default argument
2. Keyword arguments (named arguments)
3. Positional arguments
4. Arbitrary arguments (variable-length arguments *args and **kwargs)
AXL IT Academy
Default Arguments
Default arguments are arguments those have default values. We assign
default values to the argument using the = (assignment) operator at the time
of function definition. You can define a function with any number of default
arguments.
The default value of an argument will be used inside a function if we do not
pass a value to that argument at the time of the function call. Due to this, the
default arguments become optional during the function call.
It overrides the default value if we provide a value to the default arguments
during function calls.
Keyword Arguments
AXL IT Academy
Usually, at the time of the function call, values get assigned to the arguments
according to their position. So we must pass values in the same sequence
defined in a function definition.
For example, when we call show('dass', 19), the value dass gets assigned to
the argument name, and similarly, 19 to age and so on as per the sequence.
We can alter this behavior using a keyword argument.
Keyword arguments are those arguments where values get assigned to the
arguments by their keyword (name) when the function is called. It is preceded
by the variable name and an (=) assignment operator. The Keyword Argument
is also called a named argument.
Change the sequence of keyword arguments
Also, you can change the sequence of keyword arguments by using their name
in function calls.
Python allows functions to be called using keyword arguments. But all the
keyword arguments should match the parameters in the function definition.
When we call functions in this way, the order (position) of the arguments can
be changed.
Positional Arguments
Positional arguments are arguments that can be called by their position in the
function definition
By default, Python functions are called using the positional arguments.
Variable-length arguments
AXL IT Academy
We use variable-length arguments if we dont know the number of arguments
needed for the function in advance.
Types of Arbitrary Arguments:
 arbitrary positional arguments (*args)
 arbitrary keyword arguments (**kwargs)
The *args and **kwargs allow you to pass multiple positional arguments or
keyword arguments to a function.
Arbitrary positional arguments (*args)
We can declare a variable-length argument with the * (asterisk) symbol. Place
an asterisk (*) before a parameter in the function definition to define an
arbitrary positional argument.
we can pass multiple arguments to the function. Internally all these values are
represented in the form of a tuple.
Ad

Recommended

Python functions
Python functions
Learnbay Datascience
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
Powerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
Functions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
Functions in python slide share
Functions in python slide share
Devashish Kumar
Python programming - Functions and list and tuples
Python programming - Functions and list and tuples
MalligaarjunanN
python slides introduction interrupt.ppt
python slides introduction interrupt.ppt
Vinod Deenathayalan
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
Osama Ghandour Geris
functions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
Lecture 08.pptx
Lecture 08.pptx
Mohammad Hassan
Python Function.pdf
Python Function.pdf
NehaSpillai1
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
Python Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Functions.pptx
Python Functions.pptx
AnuragBharti27
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
Functions in C++
Functions in C++
home
UNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
Python programming variables and comment
Python programming variables and comment
MalligaarjunanN
Functions in C
Functions in C
Kamal Acharya
function of C.pptx
function of C.pptx
shivas379526
use of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
Rajasekhar364622
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
KrithikaTM
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
Python Functions
Python Functions
Mohammed Sikander
functions new.pptx
functions new.pptx
bhuvanalakshmik2
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore

More Related Content

Similar to functionnotes.pdf (20)

python slides introduction interrupt.ppt
python slides introduction interrupt.ppt
Vinod Deenathayalan
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
Osama Ghandour Geris
functions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
Lecture 08.pptx
Lecture 08.pptx
Mohammad Hassan
Python Function.pdf
Python Function.pdf
NehaSpillai1
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
Python Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Functions.pptx
Python Functions.pptx
AnuragBharti27
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
Functions in C++
Functions in C++
home
UNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
Python programming variables and comment
Python programming variables and comment
MalligaarjunanN
Functions in C
Functions in C
Kamal Acharya
function of C.pptx
function of C.pptx
shivas379526
use of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
Rajasekhar364622
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
KrithikaTM
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
Python Functions
Python Functions
Mohammed Sikander
functions new.pptx
functions new.pptx
bhuvanalakshmik2
python slides introduction interrupt.ppt
python slides introduction interrupt.ppt
Vinod Deenathayalan
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
Osama Ghandour Geris
functions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
Python Function.pdf
Python Function.pdf
NehaSpillai1
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
Python Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Functions.pptx
Python Functions.pptx
AnuragBharti27
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
Functions in C++
Functions in C++
home
UNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
Python programming variables and comment
Python programming variables and comment
MalligaarjunanN
function of C.pptx
function of C.pptx
shivas379526
use of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
Rajasekhar364622
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
KrithikaTM
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19

Recently uploaded (20)

9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
Ad

functionnotes.pdf

  • 1. AXL IT Academy Functions In Python, a function can be defined as a named block of code. We use functions mainly to avoid code repetition. We can define that action only once using a function and call that function whenever required to do the same activity. The benefit of using a function is code reusability and modularity. Function improves efficiency and reduces errors because of the reusability of a code. Once we create a function, we can call it anywhere and anytime. Creating a Function In Python a function is defined using the def keyword. The def stands for define. It is used for telling the interpreter that it is a user defined function. The def is followed by function name, which is followed by parameters(Optional). Define the function body with a block of code. Syntax: functionNameHere(parameters): def #your code here
  • 2. AXL IT Academy Note: There is no need to specify curly braces for the function body, in python. Only indentation is needed to separate code blocks. Calling a Function To call a function, use the function name followed by parenthesis, which is followed by arguments (if any). : Note function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error. Arguments
  • 3. AXL IT Academy Arguments are specified after the function name, inside the parentheses, for passing information into function. You can add as many arguments as you want, just separate them with a comma. Parameters and Arguments 1. A parameter is the variable listed inside the parentheses in the function definition. 2. An argument is the value that is sent to the function when it is called. Recursion Recursion means that a defined function calls itself. When written correctly recursion can be a very useful and effective.
  • 4. AXL IT Academy Example: #Recursion function which prints upto 99 without for/while recur (k): def if(k <100): (k) print k+=1 ( k ) recur #calling recur recur (1) Function Assignment We can assign a function to a variable in the same way as we assign a value to it. You can even use those variables to call the function as well. def proc(): return "Welcome" procAlias= proc print(proc) print(procAlias) print(proc ())
  • 5. AXL IT Academy print(procAlias()) Return in Function Return means the task is completed and now you need to return to where you came, with the specified data. You can return any type of data, you can even return a function. This should be the last statement in a function. def proc(): return "Bon Voyage" print(proc ()) Inner Function A function which is defined inside another function is known as inner function or nested function. Nested functions are able to access variables of the enclosing scope. Types of function arguments. There are various ways to use arguments in a function. In Python, we have the following 4 types of function arguments.
  • 6. AXL IT Academy 1. Default argument 2. Keyword arguments (named arguments) 3. Positional arguments 4. Arbitrary arguments (variable-length arguments *args and **kwargs)
  • 7. AXL IT Academy Default Arguments Default arguments are arguments those have default values. We assign default values to the argument using the = (assignment) operator at the time of function definition. You can define a function with any number of default arguments. The default value of an argument will be used inside a function if we do not pass a value to that argument at the time of the function call. Due to this, the default arguments become optional during the function call. It overrides the default value if we provide a value to the default arguments during function calls. Keyword Arguments
  • 8. AXL IT Academy Usually, at the time of the function call, values get assigned to the arguments according to their position. So we must pass values in the same sequence defined in a function definition. For example, when we call show('dass', 19), the value dass gets assigned to the argument name, and similarly, 19 to age and so on as per the sequence. We can alter this behavior using a keyword argument. Keyword arguments are those arguments where values get assigned to the arguments by their keyword (name) when the function is called. It is preceded by the variable name and an (=) assignment operator. The Keyword Argument is also called a named argument. Change the sequence of keyword arguments Also, you can change the sequence of keyword arguments by using their name in function calls. Python allows functions to be called using keyword arguments. But all the keyword arguments should match the parameters in the function definition. When we call functions in this way, the order (position) of the arguments can be changed. Positional Arguments Positional arguments are arguments that can be called by their position in the function definition By default, Python functions are called using the positional arguments. Variable-length arguments
  • 9. AXL IT Academy We use variable-length arguments if we dont know the number of arguments needed for the function in advance. Types of Arbitrary Arguments: arbitrary positional arguments (*args) arbitrary keyword arguments (**kwargs) The *args and **kwargs allow you to pass multiple positional arguments or keyword arguments to a function. Arbitrary positional arguments (*args) We can declare a variable-length argument with the * (asterisk) symbol. Place an asterisk (*) before a parameter in the function definition to define an arbitrary positional argument. we can pass multiple arguments to the function. Internally all these values are represented in the form of a tuple.