SlideShare a Scribd company logo
Python Functions,
class & object,
Error Handling
Functions
 Piece of reusable code
 Solves particular task
 Call function instead of writing code yourself
 Built-in function
 User defined function
Type of function:
Built-in Functions
Syntax of Function
print(): Outputs data to the console.
Function Call
 Once we have defined a function, we can call it from
another function, program or even the Python prompt.
 To call a function we simply type the function name with
appropriate parameters.
Calling the
Function
my_function()
Square Function:Take one arguments and
prints its square
Scope and Lifetime of variables
 Scope of a variable is the portion of a program where the variable is recognized.
 Parameters and variables defined inside a function is not visible from outside.
Hence, they have a local scope.
 Lifetime of a variable is the period throughout which the variable exits in the
memory. The lifetime of variables inside a function is as long as the function
executes.
 They are destroyed once we return from the function. Hence, a function does
not remember the value of a variable from its previous calls.
Difference Between ScopeVariable
ScopeVariable
Global Scope
Default Arguments
 In this function, the parameter amount does not have a
default value and is required (mandatory) during a call.
 On the other hand, the parameter
discountPercentage has a default value of 0. So, it is
optional during a call.
 If a value is provided, it will overwrite the default value.
 Any number of arguments in a function can have a
default value.
Default Arguments
 Once we have a default argument, all the arguments to its
right must also have default values.
 Syntax-Error : non-default argument follows default
argument
Default Arguments
 Function arguments can have default values in Python.
 We can provide a default value to an argument by using
the assignment operator (=).
Variable number of arguments
*a allows the function to accept any number of positional arguments as a tuple.
Functions as Objects
● Although functions are created differently from normal
variables, functions are just like any other kind of value.
● They can be assigned and reassigned to variables, and
later referenced by those names.
Python OOPs Concepts
 Like other general-purpose programming languages, Python is also
an object-oriented language since its beginning. It allows us to
develop applications using an Object-Oriented approach. In Python,
we can easily create and use classes and objects.
 An object-oriented paradigm is to design the program using classes
and objects. The object is related to real-word entities such as book,
house, pencil, etc.
Class:
 The class can be defined as a collection of objects. It is a logical
entity that has some specific attributes and methods. For example: if
you have an employee class, then it should contain an attribute and
method, i.e. an email id, name, age, salary, etc.
 Syntax:
class ClassName:
<statement-1>
.
.
<statement-N>
Object:
 The object is an entity that has state and behavior. It may be any real-world
object like the mouse, keyboard, chair, table, pen, etc.
 Everything in Python is an object, and almost everything has attributes and
methods. All functions have a built-in attribute __doc__, which returns the
docstring defined in the function source code.
 When we define a class, it needs to create an object to allocate the memory.
Consider the following
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
example.
Python Exception Handling-
 Error in Python can be of two types i.e. Syntax errors and Exceptions.
Errors are problems in a program due to which the program will stop the
execution. On the other hand, exceptions are raised when some internal
events occur which change the normal flow of the program.
 Difference between Syntax Error and Exceptions
 Syntax Error: As the name suggests this error is caused by the wrong
syntax in the code. It leads to the termination of the program.
 amount = 10000
 if(amount > 2999)
 print("You are eligible to purchase Dsa Self Paced")
Python Exception Handling-
 Exceptions: Exceptions are raised when the program is syntactically
correct, but the code results in an error. This error does not stop the
execution of the program, however, it changes the normal flow of the
program.
 Example:- Here in this code as we are dividing the ‘marks’ by zero so a
error will occur known as ‘ZeroDivisionError’. ‘ZeroDivisionError’ occurs
when we try to divide any number by 0.
 marks = 10000
 a = marks / 0
 print(a)
Python Exception Handling-
 Example:- TypeError: This exception is raised when an operation or
function is applied to an object of the wrong type. Here’s an example:
 x = 5
 y = "hello"
 z = x + y

More Related Content

PDF
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
PPTX
Learn more about the concepts Functions of Python
PrathamKandari
 
PPTX
FUNCTION CPU
Krushal Kakadia
 
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
PPTX
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
PPTX
pythontraining-201jn026043638.pptx
RohitKumar639388
 
PPTX
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
Learn more about the concepts Functions of Python
PrathamKandari
 
FUNCTION CPU
Krushal Kakadia
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
pythontraining-201jn026043638.pptx
RohitKumar639388
 
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 

Similar to python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx (20)

PPTX
Functions Programming in Python Language
BalaSubramanian376976
 
PPTX
Introduction To Programming with Python-1
Syed Farjad Zia Zaidi
 
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
PPTX
Functions
Lakshmi Sarvani Videla
 
PPTX
Functions in Python and its types for beginners
Mohammad Usman
 
PPTX
Decided to go to the 65 and the value of the number
harshoberoi2050
 
PDF
VIT351 Software Development VI Unit1
YOGESH SINGH
 
PDF
Python functions
Learnbay Datascience
 
PPTX
Python training
Kunalchauhan76
 
PPT
arrays.ppt
Bharath904863
 
PPTX
Docketrun's Python Course for beginners.pptx
wafoxeg441
 
PPTX
Chapter One Function.pptx
miki304759
 
PPTX
functions.pptx
KavithaChekuri3
 
PDF
3-Python Functions.pdf in simple.........
mxdsnaps
 
PPT
Chapter 1.ppt
ethiouniverse
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPTX
program fundamentals using python1 2 3 4.pptx
ibrahimsoryjalloh91
 
PDF
Python_Unit_2.pdf
alaparthi
 
PPTX
Functions and Modules.pptx
Ashwini Raut
 
PPTX
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Functions Programming in Python Language
BalaSubramanian376976
 
Introduction To Programming with Python-1
Syed Farjad Zia Zaidi
 
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
Functions in Python and its types for beginners
Mohammad Usman
 
Decided to go to the 65 and the value of the number
harshoberoi2050
 
VIT351 Software Development VI Unit1
YOGESH SINGH
 
Python functions
Learnbay Datascience
 
Python training
Kunalchauhan76
 
arrays.ppt
Bharath904863
 
Docketrun's Python Course for beginners.pptx
wafoxeg441
 
Chapter One Function.pptx
miki304759
 
functions.pptx
KavithaChekuri3
 
3-Python Functions.pdf in simple.........
mxdsnaps
 
Chapter 1.ppt
ethiouniverse
 
Functions in Python Programming Language
BeulahS2
 
program fundamentals using python1 2 3 4.pptx
ibrahimsoryjalloh91
 
Python_Unit_2.pdf
alaparthi
 
Functions and Modules.pptx
Ashwini Raut
 
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Ad

More from avishekpradhan24 (13)

PPTX
Lab-V-N.pptxvfxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
avishekpradhan24
 
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
avishekpradhan24
 
PPTX
Halloween-themed Best in Costume Certificates.pptx
avishekpradhan24
 
PPTX
what.pptxvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
avishekpradhan24
 
PPTX
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
PPTX
EduWehbhjbjnbbbbbbbbbhhhhjhjjbppt[1].pptx
avishekpradhan24
 
PPTX
Website Design Consulting by Slidesgo.pptx
avishekpradhan24
 
PPTX
Technology Consulting _ by Slidesgo.pptx
avishekpradhan24
 
PPTX
SGMC_Data_Breach_Case_Study_with_Graphics.pptx
avishekpradhan24
 
PDF
wepik-demystifying-data-structures-understanding-queues-20240417143621GPlM.pdf
avishekpradhan24
 
PDF
Software vjhghjjkhjkkkghhjhEngineering.pdf
avishekpradhan24
 
PPTX
wepik-mastering-function-in-c-a-comprehensive-guide-20231220121719HZHU.pptx
avishekpradhan24
 
PPTX
wepik-securing-networks-understanding-the-power-of-https-202402081449138j2r.pptx
avishekpradhan24
 
Lab-V-N.pptxvfxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
avishekpradhan24
 
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
avishekpradhan24
 
Halloween-themed Best in Costume Certificates.pptx
avishekpradhan24
 
what.pptxvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
avishekpradhan24
 
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
EduWehbhjbjnbbbbbbbbbhhhhjhjjbppt[1].pptx
avishekpradhan24
 
Website Design Consulting by Slidesgo.pptx
avishekpradhan24
 
Technology Consulting _ by Slidesgo.pptx
avishekpradhan24
 
SGMC_Data_Breach_Case_Study_with_Graphics.pptx
avishekpradhan24
 
wepik-demystifying-data-structures-understanding-queues-20240417143621GPlM.pdf
avishekpradhan24
 
Software vjhghjjkhjkkkghhjhEngineering.pdf
avishekpradhan24
 
wepik-mastering-function-in-c-a-comprehensive-guide-20231220121719HZHU.pptx
avishekpradhan24
 
wepik-securing-networks-understanding-the-power-of-https-202402081449138j2r.pptx
avishekpradhan24
 
Ad

Recently uploaded (20)

PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
PPTX
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Trends in pediatric nursing .pptx
AneetaSharma15
 
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
CDH. pptx
AneetaSharma15
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 

python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx

  • 1. Python Functions, class & object, Error Handling
  • 2. Functions  Piece of reusable code  Solves particular task  Call function instead of writing code yourself  Built-in function  User defined function Type of function:
  • 3. Built-in Functions Syntax of Function print(): Outputs data to the console.
  • 4. Function Call  Once we have defined a function, we can call it from another function, program or even the Python prompt.  To call a function we simply type the function name with appropriate parameters. Calling the Function my_function()
  • 5. Square Function:Take one arguments and prints its square
  • 6. Scope and Lifetime of variables  Scope of a variable is the portion of a program where the variable is recognized.  Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope.  Lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of variables inside a function is as long as the function executes.  They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.
  • 10. Default Arguments  In this function, the parameter amount does not have a default value and is required (mandatory) during a call.  On the other hand, the parameter discountPercentage has a default value of 0. So, it is optional during a call.  If a value is provided, it will overwrite the default value.  Any number of arguments in a function can have a default value.
  • 11. Default Arguments  Once we have a default argument, all the arguments to its right must also have default values.  Syntax-Error : non-default argument follows default argument
  • 12. Default Arguments  Function arguments can have default values in Python.  We can provide a default value to an argument by using the assignment operator (=).
  • 13. Variable number of arguments *a allows the function to accept any number of positional arguments as a tuple.
  • 14. Functions as Objects ● Although functions are created differently from normal variables, functions are just like any other kind of value. ● They can be assigned and reassigned to variables, and later referenced by those names.
  • 15. Python OOPs Concepts  Like other general-purpose programming languages, Python is also an object-oriented language since its beginning. It allows us to develop applications using an Object-Oriented approach. In Python, we can easily create and use classes and objects.  An object-oriented paradigm is to design the program using classes and objects. The object is related to real-word entities such as book, house, pencil, etc.
  • 16. Class:  The class can be defined as a collection of objects. It is a logical entity that has some specific attributes and methods. For example: if you have an employee class, then it should contain an attribute and method, i.e. an email id, name, age, salary, etc.  Syntax: class ClassName: <statement-1> . . <statement-N>
  • 17. Object:  The object is an entity that has state and behavior. It may be any real-world object like the mouse, keyboard, chair, table, pen, etc.  Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the docstring defined in the function source code.  When we define a class, it needs to create an object to allocate the memory. Consider the following class car: def __init__(self,modelname, year): self.modelname = modelname self.year = year def display(self): print(self.modelname,self.year) example.
  • 18. Python Exception Handling-  Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which change the normal flow of the program.  Difference between Syntax Error and Exceptions  Syntax Error: As the name suggests this error is caused by the wrong syntax in the code. It leads to the termination of the program.  amount = 10000  if(amount > 2999)  print("You are eligible to purchase Dsa Self Paced")
  • 19. Python Exception Handling-  Exceptions: Exceptions are raised when the program is syntactically correct, but the code results in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program.  Example:- Here in this code as we are dividing the ‘marks’ by zero so a error will occur known as ‘ZeroDivisionError’. ‘ZeroDivisionError’ occurs when we try to divide any number by 0.  marks = 10000  a = marks / 0  print(a)
  • 20. Python Exception Handling-  Example:- TypeError: This exception is raised when an operation or function is applied to an object of the wrong type. Here’s an example:  x = 5  y = "hello"  z = x + y

Editor's Notes

  • #2: Function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes code reusable.
  • #6: def my_func(): x = 10 print("Value inside function:",x) x = 20 my_func() print("Value outside function:",x)
  • #9: total = 0 def add(a , b): global total total = a + b print('Inside add total = ', total) add(4, 9) print('Main Block total = ', total)
  • #12: def calculateTotal(amount , discountPercentage = 0): discountAmount = discountPercentage / 100 * amount return amount - discountAmount amount = 500 totalBillAmount = calculateTotal(amount , 10) print(totalBillAmount) amount = 500 totalBillAmount = calculateTotal(amount) print(totalBillAmount) 450 500
  • #13: def mysum(*a): total = 0 for ele in a: print(total,"+",ele,"=", end='') total += ele print(total) return total print(mysum(5, 2, 9, 4)) ------------------------------------- Built-in sum function works with list (not variable number of arguments) Max function works with variable number of arguments --------------------------------------------------------------- def mymax(a,*b): large = a for ele in b: if ele > large: large = ele return large print(mymax(4,5,7,3,1,8,2))
  • #14: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #15: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #16: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #17: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #18: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #19: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #20: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))