SlideShare a Scribd company logo
Python Certification Training https://www.edureka.co/python
Agenda
Python Functions
Python Certification Training https://www.edureka.co/python
Agenda
Python Functions
Python Certification Training https://www.edureka.co/python
Agenda
Introduction 01
Why use Functions?
Getting Started 02
Concepts 03
Practical Approach 04
What are functions?
Looking at code to
understand theory
Types of functions
Python Certification Training https://www.edureka.co/python
Why Use Functions
Python Functions
Python Certification Training https://www.edureka.co/python
Why Use Functions?
Fahrenheit = (9/5)Celsius + 32
#collect input from user
celsius = float(input(“Enter Celsius value:
"))
#calculate value in Fahrenheit
Fahrenheit = (celsius*1.8) + 32
print(“Fahrenheit value is “,fahrenheit)
Logic to calculate Fahrenheit
Program to calculate Fahrenheit
You write a program in which Celsius must be converted to Fahrenheit multiple times
#collect input from user
celsius = float(input(“Enter Celsius
value: "))
#calculate value in Fahrenheit
Fahrenheit = (celsius*1.8) + 32
print(“Fahrenheit value is “,fahrenheit)
#collect input from user
celsius = float(input(“Enter Celsius
value: "))
#calculate value in Fahrenheit
Fahrenheit = (celsius*1.8) + 32
print(“Fahrenheit value is “,fahrenheit)
#collect input from user
celsius = float(input(“Enter Celsius
value: "))
#calculate value in Fahrenheit
Fahrenheit = (celsius*1.8) + 32
print(“Fahrenheit value is “,fahrenheit)
#collect input from user
celsius = float(input(“Enter Celsius
value: "))
#calculate value in Fahrenheit
Fahrenheit = (celsius*1.8) + 32
print(“Fahrenheit value is “,fahrenheit)
#collect input from user
celsius = float(input(“Enter Celsius
value: "))
#calculate value in Fahrenheit
Fahrenheit = (celsius*1.8) + 32
print(“Fahrenheit value is “,fahrenheit)
You wouldn’t want to repeat
those same lines of code every
time a value needed conversion
Reuse:
Python Certification Training https://www.edureka.co/python
Why Use Functions?
Flip Channels
Adjust Volume
Functions are reusable tasks
DRY – Don’t Repeat Yourself
Functions reduce lines of code in your main program by letting you avail predefined
features multiple times without having to repeat its set of codes again.
Python Certification Training https://www.edureka.co/python
What are Functions?
Python Functions
Python Certification Training https://www.edureka.co/python
What Are Functions?
A function is a block of organized, reusable code that is used to perform some task.
• It is usually called by its name when its task needs execution.
• You can also pass values to it or have it return results to you.
‘def’ keyword before its name. And its name is to be followed by parentheses, before a colon(:).
def function_name():
“””This function does nothing.”””
pass
Functions are tasks that one wants to perform.
Def keyword:
Functions provide a way to break problems or processes down into smaller and independent blocks of code.
Python Certification Training https://www.edureka.co/python
Docstring
>>> print(greet.__doc__)
This function greets to
the person passed into the
name parameter
Example
Remember this!
The first string after the function header is called the docstring and is short for documentation string.
Python Certification Training https://www.edureka.co/python
Types of Functions
Python Functions
Python Certification Training https://www.edureka.co/python
Functions
Functions
Built-in functions User defined functions
Python Certification Training https://www.edureka.co/python
Built-in Functions in Python
Python Functions
Python Certification Training https://www.edureka.co/python
abs() function
The abs() function returns the absolute value of the specified number.Definition
Syntax abs(n)
Example x = abs(3+5j)
C:UsersMy Name>python demo_abs_complex.py
5.830951894845301
Python Certification Training https://www.edureka.co/python
all() function
The all() function returns True if all items in an iterable are true,
otherwise it returns False.Definition
Syntax all(iterable)
Example
mylist = [True, True, True]
x = all(mylist)
C:UsersMy Name>python demo_all.py
True
Same for lists, tuples
and dictionaries as well!
Python Certification Training https://www.edureka.co/python
ascii() function
The ascii() function returns a readable version of any object (Strings,
Tuples, Lists, etc).Definition
Syntax ascii(object)
Example x = ascii("My name is
Ståle")
C:UsersMy Name>python demo_ascii.py
'My name is Ste5le'
Python Certification Training https://www.edureka.co/python
bool() function
The bool() function returns the boolean value of a specified object.Definition
Syntax bool(object)
Example x = bool(1)
C:UsersMy Name>python demo_bool.py
True
Python Certification Training https://www.edureka.co/python
enumerate() function
The enumerate() function takes a collection (e.g. a tuple) and returns it
as an enumerate object.Definition
Syntax enumerate(iterable, start)
Example x = ('apple', 'banana', 'cherry')
y = enumerate(x)
C:UsersMy Name>python demo_enumerate.py
[(0, 'apple'), (1, 'banana'), (2, 'cherry')]
Python Certification Training https://www.edureka.co/python
format() function
The format() function formats a specified value into a specified format.Definition
Syntax format(value, format)
Example x = format(0.5, '%')
C:UsersMy Name>python demo_format.py
50.000000%
Python Certification Training https://www.edureka.co/python
getattr() function
The getattr() function returns the value of the specified attribute from
the specified object.Definition
Syntax getattr(object, attribute, default)
Example
class Person:
name = "John"
age = 36
country = "Norway"
x = getattr(Person, 'age')
C:UsersMy Name>python demo_getattr.py
36
Python Certification Training https://www.edureka.co/python
id() function
The id() function returns a unique id for the specified object.Definition
Syntax id(object)
Example
x = ('apple', 'banana', 'cherry')
y = id(x)
C:UsersMy Name>python demo_id.py
56450738
Python Certification Training https://www.edureka.co/python
len() function
The len() function returns the number of items in an object.Definition
Syntax len(object)
Example
mylist = "Hello"
x = len(mylist)
C:UsersMy Name>python demo_len2.py
5
Python Certification Training https://www.edureka.co/python
map() function
The map() function executes a specified function for each item in a
iterable. The item is sent to the function as a parameter.Definition
Syntax map(function, iterables)
Example
def myfunc(n):
return len(n)
x = map(myfunc, ('apple', 'banana’, 'cherry'))
C:UsersMy Name>python demo_map.py
<map object at 0x056D44F0>
['5', '6', '6']
Python Certification Training https://www.edureka.co/python
min() function
The min() function returns the item with the lowest value, or the item
with the lowest value in an iterable.Definition
Syntax min(n1, n2, n3, ...)
Example x = min(5, 10)
C:UsersMy Name>python demo_min.py
5
Python Certification Training https://www.edureka.co/python
pow() function
The pow() function returns the value of x to the power of y (x^y).Definition
Syntax pow(x, y, z)
Example x = pow(4, 3)
C:UsersMy Name>python demo_pow.py
64
Python Certification Training https://www.edureka.co/python
print() function
The print() function prints the specified message to the screen, or other
standard output device.Definition
Syntax print(object(s), separator=separator, end=end, file=file, flush=flush)
Example print("Hello World")
C:UsersMy Name>python demo_print.py
Hello World
Python Certification Training https://www.edureka.co/python
setattr() function
The setattr() function sets the value of the specified attribute of the
specified object.Definition
Syntax setattr(object, attribute, value)
Example
class Person:
name = "John"
age = 36
country = "Norway"
setattr(Person, 'age', 40)
C:UsersMy Name>python demo_setattr.py
40
Python Certification Training https://www.edureka.co/python
sorted() function
The sorted() function returns a sorted list of the specified iterable
object.Definition
Syntax sorted(iterable, key=key, reverse=reverse)
Example
a = ("b", "g", "a", "d", "f", "c", "h", "e")
x = sorted(a)
print(x)
C:UsersMy Name>python demo_sorted.py
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Python Certification Training https://www.edureka.co/python
type() function
The type() function returns the type of the specified objectDefinition
Syntax type(object, bases, dict)
Example
a = ('apple', 'banana', 'cherry')
b = "Hello World"
c = 33
x = type(a)
y = type(b)
z = type(c)
C:UsersMy Name>python demo_type.py
<class 'tuple'>
<class 'str'>
<class 'int'>
Python Certification Training https://www.edureka.co/python
User Defined Functions in Python
Python Functions
Python Certification Training https://www.edureka.co/python
User-Defined Functions In Python
Code first approach, let’s begin
def my_function():
print("Hello from a function")
Creating a function
def my_function():
print("Hello from a function")
my_function()
Calling a function
Python Certification Training https://www.edureka.co/python
Parameters
Information passed to functions
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Example
C:UsersMy Name>python demo_function_param.py
Emil Refsnes
Tobias Refsnes
Linus Refsnes
Python Certification Training https://www.edureka.co/python
Parameters
Default parameter value
def my_function(country =
"Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Example
C:UsersMy Name>python
demo_function_param2.py
I am from Sweden
I am from India
I am from Norway
I am from Brazil
Python Certification Training https://www.edureka.co/python
Parameters
Return values
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Example
C:UsersMy Name>python
demo_function_return.py
15
25
45
Python Certification Training https://www.edureka.co/python
Parameters
Recursion
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("nnRecursion Example Results")
tri_recursion(6)
Example
C:UsersMy Name>python demo_recursion.py
Recursion Example Results
1
3
6
10
15
21
Function
Python Certification Training https://www.edureka.co/python
Python Lambda Function
Python Functions
Python Certification Training https://www.edureka.co/python
Lambda Function
A lambda function is a small anonymous function. It can take any
number of arguments, but can only have one expression.
What is Lambda?
lambda arguments : expression
Syntax
x = lambda a : a + 10
print(x(5))
Example
C:UsersMy Name>python demo_lambda.py
15
Python Certification Training https://www.edureka.co/python
Lambda Function
x = lambda a, b : a * b
print(x(5, 6))
A lambda function that multiplies argument a
with argument b and print the result: C:UsersMy Name>python demo_lambda2.py
30
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
A lambda function that sums argument a, b,
and c and print the result: C:UsersMy Name>python demo_lambda3.py
13
Python Certification Training https://www.edureka.co/python
Why Use Lambda Function?
The power of lambda is better shown when you use them
as an anonymous function inside another function.
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
Use that function definition to make a
function that always doubles the number you
send in: C:UsersMy Name>python demo_lambda_double.py
22
def myfunc(n):
return lambda a : a * n
Python Certification Training https://www.edureka.co/python
Why Use Lambda Function?
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
Or, use the same function definition to make
a function that always triples the number you
send in: C:UsersMy Name>python demo_lambda_both.py
22
33
Python Certification Training https://www.edureka.co/python
Conclusion
Python Functions
Python Certification Training https://www.edureka.co/python
Conclusion
Python Functions, yay!
Python Functions Tutorial | Working With Functions In Python | Python Training | Edureka

More Related Content

What's hot (20)

Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
Girish Khanzode
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
sambitmandal
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
baabtra.com - No. 1 supplier of quality freshers
 
Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
Emertxe Information Technologies Pvt Ltd
 
Python
PythonPython
Python
Aashish Jain
 
Exception handling and function in python
Exception handling and function in pythonException handling and function in python
Exception handling and function in python
TMARAGATHAM
 
python Function
python Function python Function
python Function
Ronak Rathi
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ayshwarya Baburam
 
NUMPY
NUMPY NUMPY
NUMPY
Global Academy of Technology
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Python Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaPython Collections Tutorial | Edureka
Python Collections Tutorial | Edureka
Edureka!
 

Similar to Python Functions Tutorial | Working With Functions In Python | Python Training | Edureka (20)

Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
Oon Arfiandwi
 
Data Structure and Algorithms (DSA) with Python
Data Structure and Algorithms (DSA) with PythonData Structure and Algorithms (DSA) with Python
Data Structure and Algorithms (DSA) with Python
epsilonice
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
Python Functions 1
Python Functions 1Python Functions 1
Python Functions 1
gsdhindsa
 
W-334535VBE242 Using Python Libraries.pdf
W-334535VBE242 Using Python Libraries.pdfW-334535VBE242 Using Python Libraries.pdf
W-334535VBE242 Using Python Libraries.pdf
manassingh1509
 
Functions.pdf
Functions.pdfFunctions.pdf
Functions.pdf
kailashGusain3
 
Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
RiteshKumarPradhan1
 
Python tour
Python tourPython tour
Python tour
Tamer Abdul-Radi
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
Functions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions ConceptsFunctions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions Concepts
nitinaees
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
Daddy84
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
paijitk
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
Henry Schreiner
 
Header files in c
Header files in cHeader files in c
Header files in c
HoneyChintal
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
headerfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdfheaderfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdf
jazzcashlimit
 
C463_02_python.ppt
C463_02_python.pptC463_02_python.ppt
C463_02_python.ppt
KapilMighani
 
kapil presentation.ppt
kapil presentation.pptkapil presentation.ppt
kapil presentation.ppt
KapilMighani
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Introduction to Python Programming – Part I.pptx
Introduction to Python Programming  –  Part I.pptxIntroduction to Python Programming  –  Part I.pptx
Introduction to Python Programming – Part I.pptx
shakkarikondas
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
Oon Arfiandwi
 
Data Structure and Algorithms (DSA) with Python
Data Structure and Algorithms (DSA) with PythonData Structure and Algorithms (DSA) with Python
Data Structure and Algorithms (DSA) with Python
epsilonice
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
Python Functions 1
Python Functions 1Python Functions 1
Python Functions 1
gsdhindsa
 
W-334535VBE242 Using Python Libraries.pdf
W-334535VBE242 Using Python Libraries.pdfW-334535VBE242 Using Python Libraries.pdf
W-334535VBE242 Using Python Libraries.pdf
manassingh1509
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
Functions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions ConceptsFunctions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions Concepts
nitinaees
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
Daddy84
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
paijitk
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
Henry Schreiner
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
headerfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdfheaderfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdf
jazzcashlimit
 
C463_02_python.ppt
C463_02_python.pptC463_02_python.ppt
C463_02_python.ppt
KapilMighani
 
kapil presentation.ppt
kapil presentation.pptkapil presentation.ppt
kapil presentation.ppt
KapilMighani
 
Introduction to Python Programming – Part I.pptx
Introduction to Python Programming  –  Part I.pptxIntroduction to Python Programming  –  Part I.pptx
Introduction to Python Programming – Part I.pptx
shakkarikondas
 
Ad

More from Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
Ad

Recently uploaded (20)

The case for on-premises AI
The case for on-premises AIThe case for on-premises AI
The case for on-premises AI
Principled Technologies
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
European Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility TestingEuropean Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility Testing
Julia Undeutsch
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
STKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 versionSTKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 version
Dr. Jimmy Schwarzkopf
 
AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Peter Bittner
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 ADr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr. Jimmy Schwarzkopf
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
European Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility TestingEuropean Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility Testing
Julia Undeutsch
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
STKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 versionSTKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 version
Dr. Jimmy Schwarzkopf
 
AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Peter Bittner
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 ADr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr. Jimmy Schwarzkopf
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 

Python Functions Tutorial | Working With Functions In Python | Python Training | Edureka

  • 1. Python Certification Training https://www.edureka.co/python Agenda Python Functions
  • 2. Python Certification Training https://www.edureka.co/python Agenda Python Functions
  • 3. Python Certification Training https://www.edureka.co/python Agenda Introduction 01 Why use Functions? Getting Started 02 Concepts 03 Practical Approach 04 What are functions? Looking at code to understand theory Types of functions
  • 4. Python Certification Training https://www.edureka.co/python Why Use Functions Python Functions
  • 5. Python Certification Training https://www.edureka.co/python Why Use Functions? Fahrenheit = (9/5)Celsius + 32 #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) Logic to calculate Fahrenheit Program to calculate Fahrenheit You write a program in which Celsius must be converted to Fahrenheit multiple times #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) You wouldn’t want to repeat those same lines of code every time a value needed conversion Reuse:
  • 6. Python Certification Training https://www.edureka.co/python Why Use Functions? Flip Channels Adjust Volume Functions are reusable tasks DRY – Don’t Repeat Yourself Functions reduce lines of code in your main program by letting you avail predefined features multiple times without having to repeat its set of codes again.
  • 7. Python Certification Training https://www.edureka.co/python What are Functions? Python Functions
  • 8. Python Certification Training https://www.edureka.co/python What Are Functions? A function is a block of organized, reusable code that is used to perform some task. • It is usually called by its name when its task needs execution. • You can also pass values to it or have it return results to you. ‘def’ keyword before its name. And its name is to be followed by parentheses, before a colon(:). def function_name(): “””This function does nothing.””” pass Functions are tasks that one wants to perform. Def keyword: Functions provide a way to break problems or processes down into smaller and independent blocks of code.
  • 9. Python Certification Training https://www.edureka.co/python Docstring >>> print(greet.__doc__) This function greets to the person passed into the name parameter Example Remember this! The first string after the function header is called the docstring and is short for documentation string.
  • 10. Python Certification Training https://www.edureka.co/python Types of Functions Python Functions
  • 11. Python Certification Training https://www.edureka.co/python Functions Functions Built-in functions User defined functions
  • 12. Python Certification Training https://www.edureka.co/python Built-in Functions in Python Python Functions
  • 13. Python Certification Training https://www.edureka.co/python abs() function The abs() function returns the absolute value of the specified number.Definition Syntax abs(n) Example x = abs(3+5j) C:UsersMy Name>python demo_abs_complex.py 5.830951894845301
  • 14. Python Certification Training https://www.edureka.co/python all() function The all() function returns True if all items in an iterable are true, otherwise it returns False.Definition Syntax all(iterable) Example mylist = [True, True, True] x = all(mylist) C:UsersMy Name>python demo_all.py True Same for lists, tuples and dictionaries as well!
  • 15. Python Certification Training https://www.edureka.co/python ascii() function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc).Definition Syntax ascii(object) Example x = ascii("My name is Ståle") C:UsersMy Name>python demo_ascii.py 'My name is Ste5le'
  • 16. Python Certification Training https://www.edureka.co/python bool() function The bool() function returns the boolean value of a specified object.Definition Syntax bool(object) Example x = bool(1) C:UsersMy Name>python demo_bool.py True
  • 17. Python Certification Training https://www.edureka.co/python enumerate() function The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object.Definition Syntax enumerate(iterable, start) Example x = ('apple', 'banana', 'cherry') y = enumerate(x) C:UsersMy Name>python demo_enumerate.py [(0, 'apple'), (1, 'banana'), (2, 'cherry')]
  • 18. Python Certification Training https://www.edureka.co/python format() function The format() function formats a specified value into a specified format.Definition Syntax format(value, format) Example x = format(0.5, '%') C:UsersMy Name>python demo_format.py 50.000000%
  • 19. Python Certification Training https://www.edureka.co/python getattr() function The getattr() function returns the value of the specified attribute from the specified object.Definition Syntax getattr(object, attribute, default) Example class Person: name = "John" age = 36 country = "Norway" x = getattr(Person, 'age') C:UsersMy Name>python demo_getattr.py 36
  • 20. Python Certification Training https://www.edureka.co/python id() function The id() function returns a unique id for the specified object.Definition Syntax id(object) Example x = ('apple', 'banana', 'cherry') y = id(x) C:UsersMy Name>python demo_id.py 56450738
  • 21. Python Certification Training https://www.edureka.co/python len() function The len() function returns the number of items in an object.Definition Syntax len(object) Example mylist = "Hello" x = len(mylist) C:UsersMy Name>python demo_len2.py 5
  • 22. Python Certification Training https://www.edureka.co/python map() function The map() function executes a specified function for each item in a iterable. The item is sent to the function as a parameter.Definition Syntax map(function, iterables) Example def myfunc(n): return len(n) x = map(myfunc, ('apple', 'banana’, 'cherry')) C:UsersMy Name>python demo_map.py <map object at 0x056D44F0> ['5', '6', '6']
  • 23. Python Certification Training https://www.edureka.co/python min() function The min() function returns the item with the lowest value, or the item with the lowest value in an iterable.Definition Syntax min(n1, n2, n3, ...) Example x = min(5, 10) C:UsersMy Name>python demo_min.py 5
  • 24. Python Certification Training https://www.edureka.co/python pow() function The pow() function returns the value of x to the power of y (x^y).Definition Syntax pow(x, y, z) Example x = pow(4, 3) C:UsersMy Name>python demo_pow.py 64
  • 25. Python Certification Training https://www.edureka.co/python print() function The print() function prints the specified message to the screen, or other standard output device.Definition Syntax print(object(s), separator=separator, end=end, file=file, flush=flush) Example print("Hello World") C:UsersMy Name>python demo_print.py Hello World
  • 26. Python Certification Training https://www.edureka.co/python setattr() function The setattr() function sets the value of the specified attribute of the specified object.Definition Syntax setattr(object, attribute, value) Example class Person: name = "John" age = 36 country = "Norway" setattr(Person, 'age', 40) C:UsersMy Name>python demo_setattr.py 40
  • 27. Python Certification Training https://www.edureka.co/python sorted() function The sorted() function returns a sorted list of the specified iterable object.Definition Syntax sorted(iterable, key=key, reverse=reverse) Example a = ("b", "g", "a", "d", "f", "c", "h", "e") x = sorted(a) print(x) C:UsersMy Name>python demo_sorted.py ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  • 28. Python Certification Training https://www.edureka.co/python type() function The type() function returns the type of the specified objectDefinition Syntax type(object, bases, dict) Example a = ('apple', 'banana', 'cherry') b = "Hello World" c = 33 x = type(a) y = type(b) z = type(c) C:UsersMy Name>python demo_type.py <class 'tuple'> <class 'str'> <class 'int'>
  • 29. Python Certification Training https://www.edureka.co/python User Defined Functions in Python Python Functions
  • 30. Python Certification Training https://www.edureka.co/python User-Defined Functions In Python Code first approach, let’s begin def my_function(): print("Hello from a function") Creating a function def my_function(): print("Hello from a function") my_function() Calling a function
  • 31. Python Certification Training https://www.edureka.co/python Parameters Information passed to functions def my_function(fname): print(fname + " Refsnes") my_function("Emil") my_function("Tobias") my_function("Linus") Example C:UsersMy Name>python demo_function_param.py Emil Refsnes Tobias Refsnes Linus Refsnes
  • 32. Python Certification Training https://www.edureka.co/python Parameters Default parameter value def my_function(country = "Norway"): print("I am from " + country) my_function("Sweden") my_function("India") my_function() my_function("Brazil") Example C:UsersMy Name>python demo_function_param2.py I am from Sweden I am from India I am from Norway I am from Brazil
  • 33. Python Certification Training https://www.edureka.co/python Parameters Return values def my_function(x): return 5 * x print(my_function(3)) print(my_function(5)) print(my_function(9)) Example C:UsersMy Name>python demo_function_return.py 15 25 45
  • 34. Python Certification Training https://www.edureka.co/python Parameters Recursion def tri_recursion(k): if(k>0): result = k+tri_recursion(k-1) print(result) else: result = 0 return result print("nnRecursion Example Results") tri_recursion(6) Example C:UsersMy Name>python demo_recursion.py Recursion Example Results 1 3 6 10 15 21 Function
  • 35. Python Certification Training https://www.edureka.co/python Python Lambda Function Python Functions
  • 36. Python Certification Training https://www.edureka.co/python Lambda Function A lambda function is a small anonymous function. It can take any number of arguments, but can only have one expression. What is Lambda? lambda arguments : expression Syntax x = lambda a : a + 10 print(x(5)) Example C:UsersMy Name>python demo_lambda.py 15
  • 37. Python Certification Training https://www.edureka.co/python Lambda Function x = lambda a, b : a * b print(x(5, 6)) A lambda function that multiplies argument a with argument b and print the result: C:UsersMy Name>python demo_lambda2.py 30 x = lambda a, b, c : a + b + c print(x(5, 6, 2)) A lambda function that sums argument a, b, and c and print the result: C:UsersMy Name>python demo_lambda3.py 13
  • 38. Python Certification Training https://www.edureka.co/python Why Use Lambda Function? The power of lambda is better shown when you use them as an anonymous function inside another function. def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11)) Use that function definition to make a function that always doubles the number you send in: C:UsersMy Name>python demo_lambda_double.py 22 def myfunc(n): return lambda a : a * n
  • 39. Python Certification Training https://www.edureka.co/python Why Use Lambda Function? def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) mytripler = myfunc(3) print(mydoubler(11)) print(mytripler(11)) Or, use the same function definition to make a function that always triples the number you send in: C:UsersMy Name>python demo_lambda_both.py 22 33
  • 40. Python Certification Training https://www.edureka.co/python Conclusion Python Functions
  • 41. Python Certification Training https://www.edureka.co/python Conclusion Python Functions, yay!