A library refers to a collection of modules that together cater to a specific type of needs or application. Module is a file(.py file) containing variables, class definitions statements, and functions related to a particular task. Python modules that come preloaded with Python are called standard library modules.
Creating our module
We will be creating a module named tempConversion.py that converts values from F to C and vice-versa.
Python3
# tempConversion.py to convert between
# between Fahrenheit and Centigrade
# function to convert F to C
def to_centigrade(x):
return 5 * (x - 32) / 9.0
# function to convert C to F
def to_fahrenheit(x):
return 9 * x / 5.0 + 32
# constants
# water freezing temperature(in Celsius)
FREEZING_C = 0.0
# water freezing temperature(in Fahrenheit)
FREEZING_F = 32.0
Now save this python file and the module is created. This module can be used in other programs after importing it.
Importing a module
In python, in order to use a module, it has to be imported. Python provides multiple ways to import modules in a program :
- To import the entire module :
import module_name
- To import only a certain portion of the module :
from module_name import object_name
- To import all the objects of the module :
from module_name import *
Using an imported module
After importing the module, we can use any function/definition of the imported module as per the following syntax:
module_name.function_name()
This way of referring to the module’s object is called dot notation.
If we import a function using from, there is no need to mention the module name and the dot notation to use that function.
Example 1 : Importing the whole module :
Python3
# importing the module
import tempConversion
# using a function of the module
print(tempConversion.to_centigrade(12))
# fetching an object of the module
print(tempConversion.FREEZING_F)
Output :
-11.11111111111111
32.0
Example 2 : Importing particular components of the module :
Python3
# importing the to_fahrenheit() method
from tempConversion import to_fahrenheit
# using the imported method
print(to_fahrenheit(20))
# importing the FREEZING_C object
from tempConversion import FREEZING_C
# printing the imported variable
print(FREEZING_C)
Output :
68.0
0.0
Python standard library functions
The python interpreter has a number of functions built into it that are always available. To use these built-in functions of python directly call the functions, like function_name(). Some built-in library functions are : input(), int(), float() etc
Python3
num = 5
print("Number entered = ", num)
# oct() converts to octal number-string
onum = oct(num)
# hex() converts to hexadecimal number-string
hnum = hex(num)
print("Octal conversion yields", onum)
print("Hexadecimal conversion yields", hnum)
print(num)
Output :
Number entered = 5
Octal conversion yields 0o5
Hexadecimal conversion yields 0x5
5
Similar Reads
Python Modules Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
7 min read
Learn Python Basics âPython is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial in
9 min read
Python Math Module Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks. The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric
13 min read
Python Fire Module Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read
OS Path module in Python OS Path module contains some useful functions on pathnames. The path parameters are either strings or bytes. These functions here are used for different purposes such as for merging, normalizing, and retrieving path names in Python. All of these functions accept either only bytes or only string obje
2 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Python datetime module In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python
14 min read
Platform Module in Python Platform module in Python is a built-in library that provides a portable way to access detailed information about the underlying platform (hardware and operating system) on which your Python program is running. This can include data such as the OS name and version, machine type, processor info and P
3 min read
Cmdparse module in Python The Class which provides a simple framework for writing line-oriented command interpreters is called cmd class. These are often useful for administrative tools, prototypes and test harnesses that will later be wrapped in a more sophisticated interface. The command-line interface can be easily made u
6 min read
Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read