PYTHON-FUNCTIONS.pptx
PYTHON-FUNCTIONS.pptx
This code defines a function called `subject()` which, when called, prints the
message "CpE 401 Computer Programming I", essentially providing
information about a course.
Scope of variables:
Global variables are accessible throughout the entire program, while Local
variables are confined to the block of code (such as a function) where they
are defined.
Function with parameter
In this version, the subject() function takes one parameter course, and when
called with an argument, it prints "Course:" followed by the value of the
course parameter.
Function with multiple parameters
The return statement ends the function execution and “sends back” the result
of the function
Reusable Function
ExceptionType: This can be a specific type of exception that you expect might
occur, such as TypeError, ValueError, or IOError. You can also use a general
Exception to catch any type of exception.
Handling the Exception: Inside the "except" block, you write code to handle
the exception. This could involve logging the error, providing a user-friendly
message, or attempting to recover from the error.
ZeroDivisionError exception
IndexError: This error occurs when you try to access an index that is outside
the bounds of a sequence (e.g., list, tuple, string). In Python, indexing starts
from 0, so my_list[4] tries to access the fifth element of my_list, which
doesn't exist, resulting in an IndexError.
TypeError
KeyError: Raised when a dictionary key is not found. "c" is not a key in
my_dict. Attempting to access it using my_dict["c"] raises a KeyError because
the key does not exist in the dictionary.
THANK YOU