Python Unit 4 Part 2
Python Unit 4 Part 2
A python module can be defined as a python program file which contains a python code
including python functions, class, or variables. In other words, we can say that our python code
file saved with the extension (.py) is treated as the module. We may have a runnable code inside
the python module.
For example, print() and input() for I/O, number conversion functions int(), float(), complex(),
data type conversions list(), tuple(), set(), etc.
Fisrt we will import math module by writing it on console or editor and then we will use its
functions.
1. Pie (π) is a well-known mathematical constant, which is defined as the ratio of the
circumference to the diameter of a circle and its value is 3.141592653589793.
>>>math.pi
3.141592653589793
2. The following statements show sin, cos and tan ratios for the angle of 30 degrees
(0.5235987755982988 radians):
>>math.sin(0.5235987755982988)
0.49999999999999994
>>>math.cos(0.5235987755982988)
0.8660254037844387
>>>math.tan(0.5235987755982988)
0.5773502691896257
You may recall that sin(30)=0.5, cos(30)=32 (which is 0.8660254037844387) and tan(30)= 13
(which is 0.5773502691896257).
3. math.log()
The math.log() method returns the natural logarithm of a given number. The natural logarithm is
calculated to the base e.
>>>math.log(10)
2.302585092994046
4. math.exp()
The math.exp() method returns a float number after raising e (math.e) to given number. In other
words, exp(x) gives e**x.
>>>math.exp(10)
1.0
5. math.pow()
The math.pow() method receives two float arguments, raises the first to the second and returns
the result. In other words, pow(4,4) is equivalent to 4**4.
>>>math.pow(2,4)
16.0
>>>2**4
16
6. math.sqrt()
The math.sqrt() method returns the square root of a given number.
>>>math.sqrt(100)
10.0
>>>math.sqrt(3)
1.7320508075688772
The following two functions are called representation functions. The ceil() function
approximates the given number to the smallest integer, greater than or equal to the given floating
point number. The floor() function returns the largest integer less than or equal to the given
>>>math.ceil(4.5867)
5
>>>math.floor(4.5687)
4
Create a Module
To create a module you want to just save the code you want in a file with the file extension .py:
Example
def greetings(name):
print("Hello, " + name)
Here we are creating a function with the function name greetings and will save it and will use it
as a module.
The import statement is used to import all the functionality of one module into another. Here, we
must notice that we can use the functionality of any python source file by importing that file as
the module into another python source file.
We can import multiple modules with a single import statement, but a module is loaded once
regardless of the number of times, it has been imported into our file.
For example, if we are using ms word, in which there are many graphical and logical functions
used, and there are also some shortcut keys, while working on ms word, we call many such
functions to use it in our work.
Also, take the example of calculator, lets consider that addition, subtraction, multiplication and
division are the modules whose functions are pre defined in the calculator and while we work,
we will call these modules.
Example
Import the module named firstmodule, and call the greeting function:
import mymodule
firstmodule.greeting("Raj")
Output
Hello Raj
Note: When using a function from a module, use the syntax: module_name.function_name.
Example:
print add(10, 2)
Output:
12
Instead of importing the whole module into the namespace, python provides the flexibility to
import only the specific attributes of a module. This can be done by using from-import
statement. The syntax to use the from-import statement is given below.
Output:
4.0
720
Consider the following module named as calculation which contains three functions as
summation, multiplication, and divide.
calculation.py:
Main.py:
Output:
Renaming a module
Python provides us the flexibility to import some module with a specific name so that we can use
this name to use that module in our python source file.
Example
1. #the module calculation of previous example is imported in this example as cal.
2. import calculation as cal;
3. a = int(input("Enter a:"));
4. b = int(input("Enter b:"));
5. print("Sum = ",cal.addition(a,b))
Output:
Enter a1:0
Enter b:20
Sum = 30
Python Classes/Objects
Python is an object oriented programming language. Almost everything in Python is an object,
with its properties and methods.
A class is a user-defined blueprint or prototype from which objects are created. Classes provide
a means of bundling data and functionality together. Creating a new class creates a new type of
object, allowing new instances of that type to be made. Each class instance can have attributes
attached to it for maintaining its state. Class instances can also have methods (defined by its
class) for modifying its state.
Class creates a user-defined data structure, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class. A class is like
a blueprint for an object.
class ClassName:
# Statement-1
.
.
.
# Statement-N
Create a Class
To create a class, use the keyword class:
Example
class MyClass:
x=5
Output
<class '__main__.MyClass'>
Class Objects
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the
class with actual values.
It’s not an idea anymore, it’s an actual snake. We can have many snakes to create many different
instances, but without the class ‘Snake’ as a guide, we would be lost, not knowing what
information is required.
An object consists of :
• State : It is represented by attributes of an object. It also reflects the properties of an object.
• Behavior : It is represented by methods of an object. It also reflects the response of an
object with other objects.
• Identity : It gives a unique name to an object and enables one object to interact with other
objects.
Identity
• Name of
Snake
Attributes
• Breed
• Color
Behavior
• Eat
• Bite
Create Object
Now we can use the class named MyClass to create objects:
Example
class MyClass:
x=5
p1 = MyClass()
print(p1.x)
Output