SlideShare una empresa de Scribd logo
1 de 6
Function – A block of organized, reusable code that is used to perform a single, related action and is executed when we
call it.
For example we have different buttons on remote control each one of which have different functionality in the same way
function is used for different functionalities and that particular button of remote works when we press it.
Types of Python Functions
Built in functions defined in modules User Defined
(already predefined) ( used by calling particular module) (design by user )
Why Write Functions? (Advantages)
 Reduces complexity of code- It helps to divide the large programs into small groups so that we can read the code, and
debug the program faster and better.
 Reusability-Python Functions stop us from writing the same logic again and again. We can write the logic in one
function and then call it again and again.
 Efficient maintainability-The code is usually well organized, easy to maintain.
Library functions(Built in):-pre-defined, present in standard library and no need to use import statement.
Programming code output Function used
a=int(input("enter any int value"))
b=str(20) enter any int value
Input ()– used for text input
Type conversion :- int, str, float
int() used to convert text into int
print (eval(b+”30”)) 2030 print()- displays on the screen
eval()-adds two string and returns
result as integer
li=[1,2,3,4]
print (max(li), min(li))
4
1
max()- gives maximum element in list
min()-gives minimum element in list
30
for i in range(0, len(li)):
print (li[i]),
1 2 3 4 range()-used in loops
x,y=20.25,-30
print(round(x,0))
20.0 round()-rounds up argument to give
no. of places
print (abs(y)) 30 abs()-gives absolute value
print (type(x)) float type()-gives data type of arguement
print (id(x)) Id()-gives identity of object
Functions defined in modules:- These are precompiled functions which are defined in particular module and in order to use
these functions we have to import the particular module(in simple terms file containing that function).
Functions of math & tring library:- We have to write import math or import string in our program before using module
Math Module String Module
Name of
Function
What it does Name of Function What it does
ceil(n) It returns the smallest integer
greater than or equal to n. capitalize() Converts the first character to
upper case
floor(n)
It returns the largest integer less
than or equal to n
count() Returns the number of times a
specified value occurs in a
string
factorial(n) It returns the factorial of value n
find() Searches the string for a
specified value and returns the
position of where it was found
exp(n) It returns exp isalnum() Returns True if all characters in
the string are alphanumeric
1og2(n)
It returns the base-2 logarithm
of n
isalpha() Returns True if all characters in
the string are in the alphabet
1og10(n)
It returns the base-10 logarithm
of n
isdigit() Returns True if all characters in
the string are digits
pow(n, y) It returns n raised to the power y title() Converts the first character of
each word to upper case
sqrt(n) It returns the square root of n islower() Returns True if all characters in
the string are lower case
fmod(x, y)
It returns the remainder when n
is divided by y
isnumeric() Returns True if all characters in
the string are numeric
cos(n) It returns the cosine of n lstrip() Returns a left trim version of the
string
sin(n) It returns the sine of n isspace() Returns True if all characters in
the string are whitespaces
tan(n) It returns the tangent of n istitle() Returns True if the string
follows the rules of a title
pi It is pi value (3 14159) isupper() Returns True if all characters in
the string are upper case
e
It is mathernaticalconstante (2
71828
lower() Converts a string into lower
case
User defined function:-
Syntax
keyword
identifier
variables
function header terminator
def functionname( parameters ):
function_statements function code
return [expression]
 To define your own Python function, you use the ‘def’ keyword before its name. And its name is to be followed by
parentheses, before a colon(:)
For example def cal():
 Any input parameters or arguments should be placed within these parentheses. You can also define
For example def cal(a, b): parameters inside these parentheses.
 The code block within every function starts after a colon (:) and is indented.
def cal(a, b):
print (a*b)
 The statement return [expression] exits a function, optionally passing back an expression to the caller. A return
statement with no arguments is the same as return None.
def cal(a, b):
return a*b
Rules fornaming python function (identifier)
1. It can begin with either of the following: A-Z, a-z, and underscore(_).
2. The rest of it can contain either of the following: A-Z, a-z, digits(0-9), and underscore(_).
3. A reserved keyword may not be chosen as an identifier.
(It is good practice to name a Python function according to what it does)
Execution of function output
def my_own_function():
(3) print("Hello from a function")
(1)print("hello before calling a function")
(2)my_own_function() #function calling.
(4)print("hello after calling a function")
hello before calling a function
Hello from a function
hello after calling a function
Explaination
When function call occurs, program control transfers to function block and when last statement of function block is executed, then
control is transferred again to calling block of code
How to write function
A simple program Simple function
A=int(input(“enter first variable”))
B= int(input(“enter second variable”))
def disp(A, B) : #formal parameters
print (A+B)
C=A+B
print ( C)
A=int(input(“enter first variable”))
B= int(input(“enter second variable”))
disp(A,B) #Actual parameters
NOTE: Formal parameters are written in function definition and actual parameters are passed at the time of function calling and name
of formal and actual parameters can be different.

Más contenido relacionado

Similar a Functions.docx

Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfprasnt1
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsMegha V
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxKavitha713564
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptxSulekhJangra
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfDaddy84
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionARVIND PANDE
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptRajasekhar364622
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdfNehaSpillai1
 

Similar a Functions.docx (20)

Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
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
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
 
functions
functionsfunctions
functions
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 

Más de VandanaGoyal21

Más de VandanaGoyal21 (9)

Database Query Using SQL_ip.docx
Database Query Using SQL_ip.docxDatabase Query Using SQL_ip.docx
Database Query Using SQL_ip.docx
 
sql_data.pdf
sql_data.pdfsql_data.pdf
sql_data.pdf
 
sample project-binary file.docx
sample project-binary file.docxsample project-binary file.docx
sample project-binary file.docx
 
STACK.docx
STACK.docxSTACK.docx
STACK.docx
 
Computer Networks.docx
Computer Networks.docxComputer Networks.docx
Computer Networks.docx
 
Computer Networks.docx
Computer Networks.docxComputer Networks.docx
Computer Networks.docx
 
functions.docx
functions.docxfunctions.docx
functions.docx
 
CLASS 10 IT PRACTICAL FILE.pdf
CLASS 10 IT PRACTICAL FILE.pdfCLASS 10 IT PRACTICAL FILE.pdf
CLASS 10 IT PRACTICAL FILE.pdf
 
WEB APPLICATIONS 1.pdf
WEB APPLICATIONS 1.pdfWEB APPLICATIONS 1.pdf
WEB APPLICATIONS 1.pdf
 

Último

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Último (20)

FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

Functions.docx

  • 1. Function – A block of organized, reusable code that is used to perform a single, related action and is executed when we call it. For example we have different buttons on remote control each one of which have different functionality in the same way function is used for different functionalities and that particular button of remote works when we press it. Types of Python Functions Built in functions defined in modules User Defined (already predefined) ( used by calling particular module) (design by user ) Why Write Functions? (Advantages)  Reduces complexity of code- It helps to divide the large programs into small groups so that we can read the code, and debug the program faster and better.  Reusability-Python Functions stop us from writing the same logic again and again. We can write the logic in one function and then call it again and again.  Efficient maintainability-The code is usually well organized, easy to maintain. Library functions(Built in):-pre-defined, present in standard library and no need to use import statement. Programming code output Function used a=int(input("enter any int value")) b=str(20) enter any int value Input ()– used for text input Type conversion :- int, str, float int() used to convert text into int print (eval(b+”30”)) 2030 print()- displays on the screen eval()-adds two string and returns result as integer li=[1,2,3,4] print (max(li), min(li)) 4 1 max()- gives maximum element in list min()-gives minimum element in list 30
  • 2. for i in range(0, len(li)): print (li[i]), 1 2 3 4 range()-used in loops x,y=20.25,-30 print(round(x,0)) 20.0 round()-rounds up argument to give no. of places print (abs(y)) 30 abs()-gives absolute value print (type(x)) float type()-gives data type of arguement print (id(x)) Id()-gives identity of object Functions defined in modules:- These are precompiled functions which are defined in particular module and in order to use these functions we have to import the particular module(in simple terms file containing that function). Functions of math & tring library:- We have to write import math or import string in our program before using module Math Module String Module Name of Function What it does Name of Function What it does ceil(n) It returns the smallest integer greater than or equal to n. capitalize() Converts the first character to upper case floor(n) It returns the largest integer less than or equal to n count() Returns the number of times a specified value occurs in a string factorial(n) It returns the factorial of value n find() Searches the string for a specified value and returns the position of where it was found
  • 3. exp(n) It returns exp isalnum() Returns True if all characters in the string are alphanumeric 1og2(n) It returns the base-2 logarithm of n isalpha() Returns True if all characters in the string are in the alphabet 1og10(n) It returns the base-10 logarithm of n isdigit() Returns True if all characters in the string are digits pow(n, y) It returns n raised to the power y title() Converts the first character of each word to upper case sqrt(n) It returns the square root of n islower() Returns True if all characters in the string are lower case fmod(x, y) It returns the remainder when n is divided by y isnumeric() Returns True if all characters in the string are numeric cos(n) It returns the cosine of n lstrip() Returns a left trim version of the string sin(n) It returns the sine of n isspace() Returns True if all characters in the string are whitespaces tan(n) It returns the tangent of n istitle() Returns True if the string follows the rules of a title
  • 4. pi It is pi value (3 14159) isupper() Returns True if all characters in the string are upper case e It is mathernaticalconstante (2 71828 lower() Converts a string into lower case User defined function:- Syntax keyword identifier variables function header terminator def functionname( parameters ): function_statements function code return [expression]  To define your own Python function, you use the ‘def’ keyword before its name. And its name is to be followed by parentheses, before a colon(:) For example def cal():  Any input parameters or arguments should be placed within these parentheses. You can also define For example def cal(a, b): parameters inside these parentheses.  The code block within every function starts after a colon (:) and is indented. def cal(a, b): print (a*b)
  • 5.  The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None. def cal(a, b): return a*b Rules fornaming python function (identifier) 1. It can begin with either of the following: A-Z, a-z, and underscore(_). 2. The rest of it can contain either of the following: A-Z, a-z, digits(0-9), and underscore(_). 3. A reserved keyword may not be chosen as an identifier. (It is good practice to name a Python function according to what it does) Execution of function output def my_own_function(): (3) print("Hello from a function") (1)print("hello before calling a function") (2)my_own_function() #function calling. (4)print("hello after calling a function") hello before calling a function Hello from a function hello after calling a function Explaination When function call occurs, program control transfers to function block and when last statement of function block is executed, then control is transferred again to calling block of code How to write function A simple program Simple function A=int(input(“enter first variable”)) B= int(input(“enter second variable”)) def disp(A, B) : #formal parameters print (A+B)
  • 6. C=A+B print ( C) A=int(input(“enter first variable”)) B= int(input(“enter second variable”)) disp(A,B) #Actual parameters NOTE: Formal parameters are written in function definition and actual parameters are passed at the time of function calling and name of formal and actual parameters can be different.