Data Science

Python Modules: Creating, Importing, and Using Built-in Modules

A Practical Beginners Guide To Using Python Modules

Vishnu Arun
5 min readApr 5, 2022
Created By Author

In today’s fast-paced digital world, almost every corporate organization, irrespective of its domain of operation, requires a data scientist to oversee and guide them through the sea of data-related challenges.

With the myriad of unique challenges that a firm comes across on a daily basis, it becomes extremely difficult to track and debug as the firm’s code repository grows. This is where “Python Modules” comes into the picture. It essentially allows the user to organize and store all the related code blocks into a single module.

Index Of Contents
· Introduction
· 1) What is a Python module?
· 2) How to create & import a custom module?
· 3) How to rename our Python Module?
· 4) Some widely used Python modules
4.1) Random Module
· 4.2) OS Module
· 4.3) Math Module
· 4.4) Sys Module
· 4.5) Requests Module
· 4.6) Datetime Module
· 4.7) Calendar Module
· 4.8) Tkinter Module
· Conclusion

1) What is a Python module?

Simply put, a Python module is an executable (”.py”) file consisting of one or more functions, classes, calls, methods, etc.

While working on large data science and machine learning problems comprising several thousands of lines of code, it’s always a good idea to put similar code lines into a group. This makes it easy for a third person or another team to follow along, locate, and debug any errors.

Read along to learn how to import your own custom modules and have a look at some of the most popular built-in modules!

2) How to create & import a custom module?

  1. Create and save an executable “.py” file.
    For this example, let’s create and save a file named case-counter.py
  2. Define a function count_upcase_and_lwcase() inside the module case-counter. The function takes a user-defined “word” as the input and returns the count of Upper-case and Lower_case letters in it.

3. Use the keyword import and call the file that we saved as with .py extension.

In compilers like Jupyter, on the first run, this single line of code imports the file executes it, and subsequently calls the function.

Note: In other IDE, after importing the file, you will have to run the following lines of code additionally:

syntax:file_name.function_call(user_input)

3) How to rename our Python Module?

  1. To rename the file to a new name; we import the previous case_counter, use the keyword as followed by the new filename magic_counter

2. Then, as discussed above, follow the syntax and call the module using the new name and give in the user inputs!

4) Some widely used Python modules

4.1) Random Module

The random module is a built-in module that, when called, returns pseudo-random variables. It is used in a number of applications that need the use of randomly generated values, such as password creation, shuffling values in a deck of cards, or selecting a winner in a giveaway-style event.
Code:

Take a look at the official guide to see all of the many sorts of functions that the random module provides: Link

4.2) OS Module

The OS moduleis a built-in utility module that allows users to interact with the operating system directly. It is used in determining the current directory, creating, deleting, and retrieving files from a directory, among other things.
Code:

Take a look at the official guide to see all of the different sorts of functions that the OS module provides: Link

4.3) Math Module

The math moduleis one of the most popular python modules. It comprises a wide range of mathematical functions such as trigonometric functions, logarithmic functions, mathematical constant values, etc.
Code:

To take a look at all the different types of functions that the Math module supports; take a look at the official guide: Link

4.4) Sys Module

The Sys module is a built-in module with several functions and variables that allow the user to influence parameters like interpreter runtime, the path of all built-in modules, command-line arguments, etc.
Code:

To take a look at all the different types of functions that the sys module supports; take a look at the official guide: Link

4.5) Requests Module

The Requests module is a module that allows users to scrape data from websites. It essentially allows Python to send and receive HTTPS requests. Apart from this, it also allows users to work with Rest API.
Code:

To take a look at all the different types of functions that the Requests module supports; take a look at the official guide: Link

4.6) Datetime Module

The datetime module allows us to play around, manipulate, format date and time intervals, etc. This is achieved with the help of several types of class methods.
Code:

To take a look at all the different types of functions that the Requests module supports; take a look at the official guide: Link

4.7) Calendar Module

The calender module has several functions and classes that allow the user to customize various aspects of a calendar. For example, it lets the user decide the first day of the week, return a particular month’s calendar as an HTML table, etc
Code:

To take a look at all the different types of functions and classes that the calender module supports; take a look at the official guide: Link

4.8) Tkinter Module

The tkinter is one of the most widespread and versatile modules used for developing GUI (Graphical User Interface). Nevertheless, critics always point the GUI’s built on Tkinter looks quite outdated.
Note: This module has to be run locally i.e. will not run on online IDE’s like Google Collab or Jupyter. So let us run it in Spyder IDE.
Code:

The output it returns is a dialog box that looks like this

To take a look at all the different use cases of Tkinter; take a look at the official guide: Link

Conclusion

I hope you enjoyed reading and learned a thing or two from this article. Please feel free to drop your thoughts in the comments section on any other “Python Module” that you think must feature on this list!
Summarising all the topics that we covered:
1.
A Basic understanding of Python modules.
2. Creating & importing custom modules.
3. Steps involved in renaming our Python Modules.
4. Some commonly used and must know Python modules.

--

--

Vishnu Arun
Vishnu Arun

Written by Vishnu Arun

A magical swordsman chasing butterflies through lores, breathing life into the shadows of our collective past!

No responses yet