Python Modules
Python Modules
Python Modules
A python module can be defined as a python program file which contains a python code
including python functions, class, or variables.
Example
Let's create the module named as file.py.
#displayMsg prints a message to the name being passed.
def displayMsg(name)
print("Hi "+name);
Loading the module in our python code
We need to load the module in our python code to use its functionality. Python provides two
types of statements as defined below.
The import statement
The from-import statement
The import statement
The import statement is used to import all the functionality of one module into another.
import module1,module2,........ module n
#displayMsg prints a message to the name being passed.
def displayMsg(name)
print("Hi "+name);
------------
import file;
name = input("Enter the name?")
file.displayMsg(name)
Output:
Enter the name?John
Hi John
Enter the name?John
Hi John
The from-import statement
from < module-name> import <name 1>, <name 2>..,<name n>
calculation.py:
#place the code in the calculation.py
def summation(a,b):
return a+b
def multiplication(a,b):
return a*b;
def divide(a,b):
return a/b;
Main.py:
from calculation import summation
#it will import only the summation() from calculation.py
a = int(input("Enter the first number"))
b = int(input("Enter the second number"))
print("Sum = ",summation(a,b))
Output:
Enter the first number10
Enter the second number20
Sum = 30
Python Exception
An exception can be defined as an unusual condition in a program resulting in the interruption
in the flow of the program.
Whenever an exception occurs, the program stops the execution, and thus the further code is
not executed. Therefore, an exception is the run-time errors that are unable to handle to Python
script. An exception is a Python object that represents an error
Python has many built-in exceptions that enable our program to run without interruption and
give the output. These exceptions are given below:
Common Exceptions
Python provides the number of built-in exceptions, but here we are describing the common
standard exceptions. A list of common exceptions that can be thrown from a standard Python
program is given below.
ZeroDivisionError: Occurs when a number is divided by zero.
NameError: It occurs when a name is not found. It may be local or global.
IndentationError: If incorrect indentation is given.
IOError: It occurs when Input Output operation fails.
EOFError: It occurs when the end of the file is reached, and yet operations are being performed.
Syntax:
try:
#block of code
except Exception1:
#block of code
except Exception2:
#block of code
#other code
Example 1
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
except:
print("Can't divide with zero")
Example 2
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
print("a/b = %d"%c)
# Using Exception with except statement. If we print(Exception) it will return exception class
except Exception:
print("can't divide by zero")
print(Exception)
else:
print("Hi I am else block")
Example
try:
#this will throw an exception if the file doesn't exist.
fileptr = open("file.txt","r")
except IOError:
print("File not found")
else:
print("The file opened successfully")
fileptr.close()
Example
try:
age = int(input("Enter the age:"))
if(age<18):
raise ValueError
else:
print("the age is valid")
except ValueError:
print("The age is not valid")
Example 3
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
if b is 0:
raise ArithmeticError
else:
print("a/b = ",a/b)
except ArithmeticError:
print("The value of b can't be 0")