際際滷

際際滷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

PDF
Python functions
Learnbay Datascience
PPTX
Learn more about the concepts Functions of Python
PrathamKandari
PPT
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
PPTX
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
PPT
Powerpoint presentation for Python Functions
BalaSubramanian376976
PPTX
Functions and Modules.pptx
Ashwini Raut
PPTX
Functions in python slide share
Devashish Kumar
PPTX
Python programming - Functions and list and tuples
MalligaarjunanN
PPT
python slides introduction interrupt.ppt
Vinod Deenathayalan
PPTX
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
Osama Ghandour Geris
PDF
functions notes.pdf python functions and opp
KirtiGarg71
PPTX
Lecture 08.pptx
Mohammad Hassan
PDF
Python Function.pdf
NehaSpillai1
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
PPTX
Python Functions.pptx
AnuragBharti27
PPTX
Python Functions.pptx
AnuragBharti27
PDF
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
PPTX
Functions in C++
home
PPTX
UNIT 3 python.pptx
TKSanthoshRao
PPT
Python programming variables and comment
MalligaarjunanN
PPTX
Functions in C
Kamal Acharya
PPTX
function of C.pptx
shivas379526
PPTX
use of Functions to write python program.pptx
rahulsinghsikarwar2
PPT
functions modules and exceptions handlings.ppt
Rajasekhar364622
PDF
Chapter Functions for grade 12 computer Science
KrithikaTM
PDF
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
PPTX
Python Functions
Mohammed Sikander
PPTX
functions new.pptx
bhuvanalakshmik2
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) 際際滷s
Ravi Tamada
PDF
Unlocking FME Flows Potential: Architecture Design for Modern Enterprises
Safe Software

More Related Content

Similar to functionnotes.pdf (20)

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

Recently uploaded (20)

PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) 際際滷s
Ravi Tamada
PDF
Unlocking FME Flows Potential: Architecture Design for Modern Enterprises
Safe Software
PPTX
Enabling the Digital Artisan keynote at ICOCI 2025
Alan Dix
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
PDF
Open Source Milvus Vector Database v 2.6
Zilliz
PDF
UiPath Agentic AI ile Ak脹ll脹 Otomasyonun Yeni a脹
UiPathCommunity
PDF
How to Visualize the Spatio-Temporal Data Using CesiumJS
SANGHEE SHIN
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) 際際滷s
Ravi Tamada
Unlocking FME Flows Potential: Architecture Design for Modern Enterprises
Safe Software
Enabling the Digital Artisan keynote at ICOCI 2025
Alan Dix
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
My Journey from CAD to BIM: A True Underdog Story
Safe Software
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
Python Conference Singapore - 19 Jun 2025
ninefyi
Open Source Milvus Vector Database v 2.6
Zilliz
UiPath Agentic AI ile Ak脹ll脹 Otomasyonun Yeni a脹
UiPathCommunity
How to Visualize the Spatio-Temporal Data Using CesiumJS
SANGHEE SHIN
UserCon Belgium: Honey, VMware increased my bill
stijn40
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
Why aren't you using FME Flow's CPU Time?
Safe Software
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.