Categories: Hostripples Featured

Python: Modules For Beginners – Hostripples

Today we are going to talk about modules in Python and here is the list of topics we are covering today:

1. Introduction to Modules
2. Dir command
3. Math and Calendar Modules
4. Can you write your own Modules

OK! Let’s begin with the introduction to Modules:

In our day to day life, we use many devices and tools created by other people. For Ex: – Computers, car, calculators etc.
These are the devices created or invented by other people. But once they are invented we can use it. This whole idea about reusing something applies to programme world as well.

In Python modules is a way to reuse a code written by someone else. Think about a calculator if you want to write a code that does basic mathematical operations you can write functions to find out those operations. For Ex: – if you want to find out a square root of a number or power etc. you can write your own functions. But if someone else has already done that why bother? And the best part about Python is you can use those modules for free you don’t have to pay anything.

Let me give you an example: of Math Module:-

If you want to use math module you ill have to import it using import command:
so you will say
>>> import math

This imports this math module which is a written by someone else locally into your code.

Now if you want to call a function called square root, so let’s see you are finding a square root of no .16

>>> math.sqrt(16)
4.00

You will do this above.

Similarly, now you want to find out 2 raise to the power of 4
>>> math.pow(2,4)
16

How do I know all the functions which are present in math module? You can use “dir” command for that:
>>> dir(math)

This will give you all the functions which are available in the math module.

Another way to finding these functions is just to simply Google it.
You can just type in Python 3 math module in your web browser. Here I am using Python version 3 so I am using Python 3 here.
The first link that opens is providing you have all in math module. You can see that description here and below it is listing all the functions which are available in this module. You can read through it and you can use any of this module.

We just used math.pow(x,y) function lets see few more functions in math module:-

If you want to look at the value of Pi
>>> math.pi
This function is used.

Let’s find out the log of number 100 to the base 10
>>> math.log10(100)
This function is used.

You can also find the floor and ceiling of numbers using
>>> math.floor(2.3) and
>>> math.ceil(2.3)

let’s go over to another module called Calendar:-

Again to import any module you have to use import command.
>>> import calendar

If you want to print a calendar of the year 2018 for the month of November then we will just have to type in
>>> cal=calendar.month(2018,11)
>>> print (cal)

It will print the calendar of November month for the year 2018.

If you want to check if this year is a leap year or not then type in
>>> calendar.isleap(2018)
it will give you the answer.

If you want to see if the last year was a leap year or not then simply type in
>>> calendar.isleap(2017)
It wasn’t.

Again you can use “dir” command to look at all the functions available in calendar command now how do you find the list of all the Python modules available for use?

Again – Google it- Python3.5 modules list

And the first link that opens if you click on it, this will give you all the modules available for you in alphabetical order. So you can look at any alphabet. There are tons of modules and these are by the way all the standard modules. Other than these there are many more third party modules available for use. For using them you have to import them and download those modules.
What if I have a custom need and I want to write my own Python module?

That’s possible. You can write your own custom made a module in Python.

Visit – Hostripples

Best USA Hosting Company


Vishwajit Kale
Vishwajit Kale blazed onto the digital marketing scene back in 2015 and is the digital marketing strategist of Hostripples, a company that aims to provide affordable web hosting solutions. Vishwajit is experienced in digital and content marketing along with SEO. He's fond of writing technology blogs, traveling and reading.

Recent Posts

Crafting a Professional Email: Step-by-Step Guide

Due to growing digitalization, Email Communication has become the backbone of professional interactions. Yet, surprisingly, many professionals struggle to craft…

1 day ago

Demystifying SSL: What Every Website Owner Should Know

As the digital landscape continues to evolve, securing your website has never been more crucial. SSL, or Secure Sockets Layer,…

1 week ago

Cyberduck and FileZilla: A Comprehensive Comparison

As a web designer and web developer your experience must have reached to new height, right? Further, you need to…

1 month ago

The Science Behind Social Media Posting Times

In today's digital landscape, timing is everything. Whether you're a social media manager, business owner, or content creator, the success…

1 month ago

Mastering Google Search Console: Tips for New Users

Are you a website owner? Maintaining the website is the prime concern for any website owner. Yes, it’s equally important…

1 month ago