Outline
Functions
Error Handling
Python: Function and Error Handling
Prashant K. Sharma
Diptesh Kanojia
e-Yantra Team
ERTS Lab, IIT Bombay
IIT Bombay
October 24, 2020
eYRDC e-Yantra Foundations Course One 1/13
Outline
Functions
Error Handling
Agenda for Discussion
1 Functions
What is Function ?
Creating Functions
Definition vs. Calling
Testing and Documenting
2 Error Handling
Errors
Syntax Error
Index Errors
Variable Name Errors
Error Handling
eYRDC e-Yantra Foundations Course One 2/13
What is Function ?
Outline
Creating Functions
Functions
Definition vs. Calling
Error Handling
Testing and Documenting
What is Function ?
eYRDC e-Yantra Foundations Course One 3/13
What is Function ?
Outline
Creating Functions
Functions
Definition vs. Calling
Error Handling
Testing and Documenting
Creating Functions
eYRDC e-Yantra Foundations Course One 4/13
What is Function ?
Outline
Creating Functions
Functions
Definition vs. Calling
Error Handling
Testing and Documenting
Definition vs. Calling
Definition: The code block which defines the statements to be executed
by the function is called definition of the function. It can take various
parameters and input and returns the output. In python, a function
needn’t always return something.
eYRDC e-Yantra Foundations Course One 5/13
What is Function ?
Outline
Creating Functions
Functions
Definition vs. Calling
Error Handling
Testing and Documenting
Definition vs. Calling
Definition: The code block which defines the statements to be executed
by the function is called definition of the function. It can take various
parameters and input and returns the output. In python, a function
needn’t always return something.
Calling:When the defined function is called by passing the required
parameters.
Calling fahr to celsius function
fahr to celsius(32)
eYRDC e-Yantra Foundations Course One 5/13
What is Function ?
Outline
Creating Functions
Functions
Definition vs. Calling
Error Handling
Testing and Documenting
Testing and Documenting
Testing: To check the how robust our implementation of the define
function is, we write extra code for testing. This testing can be done by
defining a test function.
eYRDC e-Yantra Foundations Course One 6/13
What is Function ?
Outline
Creating Functions
Functions
Definition vs. Calling
Error Handling
Testing and Documenting
Testing and Documenting
Testing: To check the how robust our implementation of the define
function is, we write extra code for testing. This testing can be done by
defining a test function.
Documenting: When we put docstring inside a function which describes
input, output, what the function is doing, and sample input/ouputs etc.
For making it easier to re-use.
eYRDC e-Yantra Foundations Course One 6/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
What are errors ?
Errors: Whenever something goes wrong during execution of a program,
it’s said an error has occurred.
eYRDC e-Yantra Foundations Course One 7/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
What are errors ?
Errors: Whenever something goes wrong during execution of a program,
it’s said an error has occurred.
In python errors have a form called traceback.
eYRDC e-Yantra Foundations Course One 7/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Syntax Error
Syntax Error: If the error is due to grammar or syntax of the program
written it’s called syntax error.
eYRDC e-Yantra Foundations Course One 8/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Syntax Error
Syntax Error: If the error is due to grammar or syntax of the program
written it’s called syntax error.
Example: wrong indentation, missing parenthesis, colon etc.
Syntax Error
def hello():
print(”hello world !”)
print(”hello python!”)
eYRDC e-Yantra Foundations Course One 8/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Index Error
Index Error: If the error is due to accessing of a list or string which does
not exist, then that error is called index error
eYRDC e-Yantra Foundations Course One 9/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Index Error
Index Error: If the error is due to accessing of a list or string which does
not exist, then that error is called index error
Index Error
primes = [ 2, 3 , 5, 7 ]
print( primes[4] )
eYRDC e-Yantra Foundations Course One 9/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Variable Name Error
Variable Name Error: If the error is due to accessing a variable whose
name is misspelled its called variable name error.
Variable Name Error
primes = [ 2, 3 , 5, 7 ]
print( prime )
eYRDC e-Yantra Foundations Course One 10/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Try Except
When an error occurs also called exceptions, the python code stops
executing, we can handle such scenarios using try statements.
eYRDC e-Yantra Foundations Course One 11/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Try Except
When an error occurs also called exceptions, the python code stops
executing, we can handle such scenarios using try statements.
Figure 1: Example of Try Except
eYRDC e-Yantra Foundations Course One 11/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
References
• Python Errors
• Google Python Style Guide
• PEP 8 Style Guide
eYRDC e-Yantra Foundations Course One 12/13
Errors
Outline Syntax Error
Functions Index Errors
Error Handling Variable Name Errors
Error Handling
Thank You!
Author: Prashant K. Sharma
Contributor: Diptesh Kanojia
Post your queries at: [email protected]/
eYRDC e-Yantra Foundations Course One 13/13