SlideShare a Scribd company logo
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Functions in Python
Pavan Verma
@YinYangPavan
1
Founder, P3 InfoTech Solutions Pvt. Ltd.
Python Programming Essentials
© SkillBrew http://skillbrew.com
What is a function
A function is a block of organized, reusable code
that is used to perform a single, related action
2
© SkillBrew http://skillbrew.com
Advantages of using functions
3
Reusability. Reducing
duplication of code
Decomposing
complex problems
into simpler pieces
Abstraction Cleaner code
© SkillBrew http://skillbrew.com
Types of functions
Types of
Functions
Built-in
Functions
User Defined
Functions
4
© SkillBrew http://skillbrew.com
Defining a function
5
def functionName():
# do something
def printList():
num_list = range(3)
print num_list
1. def keyword is used to define a function
2. def keyword is followed by a function name
3. Function name is followed by parenthesis ()
4. After this a block of python statements
© SkillBrew http://skillbrew.com
Calling a Function
6
def printList():
num_list = range(3)
print num_list
printList()
Output:
[0, 1, 2]
To call a function, we specify the function name
with the round brackets
Call to function
© SkillBrew http://skillbrew.com
return keyword
7
def printList():
num_list = range(3)
return num_list
my_list = printList()
print my_list
Output:
[0, 1, 2]
1. The return keyword is
used to return values
from a function
2. A function implicitly
returns None by default
even if you don’t use
return keyword
© SkillBrew http://skillbrew.com
Parameterized functions
8
def printList(upper_limit):
num_list = range(upper_limit)
print "list: %s" % num_list
printList(5)
printList(2)
Output:
list: [0, 1, 2, 3, 4]
list: [0, 1]
1. Names given in the
function definition
are called
parameters
2. The values you
supply in the
function call are
called
arguments
© SkillBrew http://skillbrew.com
Default arguments
9
def printList(upper_limit=4):
print "upper limit: %d" % upper_limit
num_list = range(upper_limit)
print "list: %s" % num_list
printList(5)
printList()
Output:
upper limit: 5
list: [0, 1, 2, 3, 4]
upper limit: 4
list: [0, 1, 2, 3]
value 4
1. When function is called
without any arguments default
value is used
2. During a function call if
argument is passed then the
passed value is used
© SkillBrew http://skillbrew.com
Default arguments (2)
10
A non-default argument may not follow default
arguments while defining a function
def printList(upper_limit=4, step):
© SkillBrew http://skillbrew.com
Keyword arguments
11
def printList(upper_limit, step=1):
print "upper limit: %d" % upper_limit
num_list = range(0, upper_limit, step)
print "list: %s" % num_list
printList(upper_limit=5, step=2)
Output:
upper limit: 5
list: [0, 2, 4]
range() function has two variations:
1.range(stop)
2.range(start, stop[, step])
© SkillBrew http://skillbrew.com
Keyword arguments (2)
12
printList(step=2, upper_limit=5)
printList(upper_limit=5, step=2)
Advantages of using keyword arguments
Using the function is easier since you don’t have to worry
about or remember the order of the arguments
© SkillBrew http://skillbrew.com
Keyword arguments (3)
13
You can give values to only those parameters which you
want, provided that the other parameters have default
argument value
printList(upper_limit=5)
© SkillBrew http://skillbrew.com
Keyword arguments (4)
14
A non-keyword argument cannot follow keyword
arguments while calling a function
printList(upper_limit=4, 10)
© SkillBrew http://skillbrew.com
global variables
15
counter = 10
def printVar():
print counter
printVar()
print "counter: %d" % counter
Output:
10
counter: 10
Variable defined outside a function or class is a
global variable
counter is a
global variable
© SkillBrew http://skillbrew.com
global variables (2)
16
counter = 10
def printVar():
counter = 20
print "Counter in: %d" % counter
printVar()
print "Counter out: %d" % counter
Output:
Counter in: 20
Counter out: 10
counter in function definition is
local variable tied to function’s
namespace
Hence the value of global
counter does not gets modified
© SkillBrew http://skillbrew.com
global variables (3)
17
counter = 10
def printVar():
global counter
counter = 20
print "Counter in: %d" % counter
printVar()
print "Counter out: %d" % counter
Output:
Counter in: 20
Counter out: 20
In order to use the global
counter variable inside the function
you have to explicitly tell python to use
global variable using global keyword
© SkillBrew http://skillbrew.com
docstrings
18
def add(x, y):
""" Return the sum of inputs x, y
"""
return x + y
print add.__doc__
Python documentation strings (or docstrings) provide a
convenient way of associating documentation with Python
modules, functions, classes, and methods
docstring
Access a docstring using
object.__doc__
© SkillBrew http://skillbrew.com
Summary
 What is a function
 Advantages of using
functions
 Defining a function
 Calling a function
 return keyword
19
 Parameterized functions
 Default arguments
 Keyword arguments
 global variables
 docstrings
© SkillBrew http://skillbrew.com
Resources
 Default arguments
http://www.ibiblio.org/g2swap/byteofpython/read/default-argument-
values.html
 Keyword arguments
http://www.ibiblio.org/g2swap/byteofpython/read/keyword-
arguments.html
 return keyword
http://www.ibiblio.org/g2swap/byteofpython/read/return.html
 local variables http://www.ibiblio.org/g2swap/byteofpython/read/local-
variables.html
 Docstrings
http://www.ibiblio.org/g2swap/byteofpython/read/docstrings.html
20
21

More Related Content

What's hot (20)

Function in c
Function in c
CGC Technical campus,Mohali
 
This pointer
This pointer
Kamal Acharya
 
46630497 fun-pointer-1
46630497 fun-pointer-1
AmIt Prasad
 
Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
Ch7 C++
Ch7 C++
Venkateswarlu Vuggam
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
仕事で使うF#
仕事で使うF#
bleis tift
 
Unit 6 pointers
Unit 6 pointers
George Erfesoglou
 
Fp201 unit5 1
Fp201 unit5 1
rohassanie
 
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
IoT Code Lab
 
Function in c program
Function in c program
umesh patil
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Functions in c language
Functions in c language
tanmaymodi4
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
Advanced C - Part 2
Advanced C - Part 2
Emertxe Information Technologies Pvt Ltd
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
RECURSION IN C
RECURSION IN C
v_jk
 
Python programming
Python programming
Ashwin Kumar Ramasamy
 
Simple Java Programs
Simple Java Programs
AravindSankaran
 
46630497 fun-pointer-1
46630497 fun-pointer-1
AmIt Prasad
 
Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
仕事で使うF#
仕事で使うF#
bleis tift
 
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
IoT Code Lab
 
Function in c program
Function in c program
umesh patil
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Functions in c language
Functions in c language
tanmaymodi4
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
RECURSION IN C
RECURSION IN C
v_jk
 

Similar to Python Programming Essentials - M17 - Functions (20)

CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
ppl unit 3.pptx ppl unit 3 usefull can understood
ppl unit 3.pptx ppl unit 3 usefull can understood
divasivavishnu2003
 
cp Module4(1)
cp Module4(1)
Amarjith C K
 
User Defined Functions in C Language
User Defined Functions in C Language
Infinity Tech Solutions
 
Functions.pptx, programming language in c
Functions.pptx, programming language in c
floraaluoch3
 
Python_UNIT-I.pptx
Python_UNIT-I.pptx
mustafatahertotanawa1
 
Presentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Chapter 11 Function
Chapter 11 Function
Deepak Singh
 
Programming Fundamentals Functions in C and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
C function
C function
thirumalaikumar3
 
4th unit full
4th unit full
Murali Saktheeswaran
 
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
TriggeredZulkar
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
Koteswari Kasireddy
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
Plataformatec
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
Oon Arfiandwi
 
Functions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions Concepts
nitinaees
 
Functionincprogram
Functionincprogram
Sampath Kumar
 
Function in c
Function in c
Raj Tandukar
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptx
PragatheshP
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
ppl unit 3.pptx ppl unit 3 usefull can understood
ppl unit 3.pptx ppl unit 3 usefull can understood
divasivavishnu2003
 
Functions.pptx, programming language in c
Functions.pptx, programming language in c
floraaluoch3
 
Presentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Chapter 11 Function
Chapter 11 Function
Deepak Singh
 
Programming Fundamentals Functions in C and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
TriggeredZulkar
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
Plataformatec
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
Oon Arfiandwi
 
Functions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions Concepts
nitinaees
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptx
PragatheshP
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Ad

More from P3 InfoTech Solutions Pvt. Ltd. (20)

Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web Development
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External Programs
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and Files
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File Operations
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M15 - References
Python Programming Essentials - M15 - References
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - Dictionaries
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M13 - Tuples
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web Development
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External Programs
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and Files
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
P3 InfoTech Solutions Pvt. Ltd.
 
Ad

Recently uploaded (20)

FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 

Python Programming Essentials - M17 - Functions