Basics Of Python Modules - GeeksforGeeks

24 Jan, 2022
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.  
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 Cdef to_centigrade(x):      return 5 * (x - 32) / 9.0 # function to convert C to Fdef 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. 
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 : 
To import the entire module : 
import module_name
To import only a certain portion of the module : 
To import only a certain portion of the module : 
from module_name import object_name
To import all the objects of the module : 
To import all the objects of the module : 
from module_name import *
 
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 moduleimport tempConversion # using a function of the moduleprint(tempConversion.to_centigrade(12)) # fetching an object of the moduleprint(tempConversion.FREEZING_F)
Output :  
-11.11111111111111
32.0
Example 2 : Importing particular components of the module :  
Python3
# importing the to_fahrenheit() methodfrom tempConversion import to_fahrenheit # using the imported methodprint(to_fahrenheit(20)) # importing the FREEZING_C objectfrom tempConversion import FREEZING_C # printing the imported variableprint(FREEZING_C)
Output :  
68.0
0.0
 
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 = 5print("Number entered = ", num) # oct() converts to octal number-stringonum = 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
 
sooda367
clintra
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()