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 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
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
External 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 built-in modules and external m
5 min read
Python 3 basics Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.11.0, we can simply call it Python3. Python 3.0 was released in 2008. and is interpreted language i.e it's not compiled and the interpreter will check the code line by line. This article can be used to learn the
10 min read
Inspect Module in Python The inspect module in Python is useful for examining objects in your code. Since Python is an object-oriented language, this module helps inspect modules, functions and other objects to better understand their structure. It also allows for detailed analysis of function calls and tracebacks, making d
4 min read
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
How to Install a Python Module? A module is simply a file containing Python code. Functions, groups, and variables can all be described in a module. Runnable code can also be used in a module. What is a Python Module?A module can be imported by multiple programs for their application, hence a single code can be used by multiple pr
4 min read