top of page
Search

Python - 102

Modules - those are Python's superpowers.

ree

In the previous article in the Python series we saw the 'datetime' module. In this article we will learn all about modules. And as always in a practical way.


Firstly, how do devs know which module to use or to say how would a beginner know where to look-up for these modules?



The above link is the official documentation for native Python modules. I would strongly recommend that you explore this in detail. A side note: you can also create your own module so don't get into that tangent - we will cover those when the time comes.


I use 'PyCharm' as my preferred IDE, you can pick one which you like. I also use this in a Linux distro. Now, these two already make my life very easy - however, in many instances you might be using a Windows machine or a tablet with OS like iOS or Chromebook or Android. If so, you can do the initial configurations the hardway (by doing it yourselves) or you can also let me know in the comments your OS type and hardware you are using - we can solve them together.


Now, coming back to Python modules. What is the structure of a module?


If you are beginner my strong recommendation is that you stick to this understanding:


Module --> Function

Module --> Class --> Method


It is much safer and easier to start with this understanding. Our code from the previous blog post:


ree

You are telling Python to do the following, step-by-step:

  1. Go to the datetime module. (The Toolbox)

  2. Find the date class inside it. (The Blueprint)

  3. Call the today() method on that class. (The Action)

  4. Store the resulting date object in the variable current_date. (The Result)


Now, as a developer we have 3 ways to learn about the native-modules:

  1. Official Python website --> https://docs.python.org/3/py-modindex.html#cap-d

  2. Using the built-in help() function [I will show you some examples down below].

  3. By practice - so as a beginner - do more of this.


How to use built-in help() function?


ree

There is another useful function called dir().

ree

Returns a list of strings containing all the members of the datetime module.


I hope now you have a good understanding of modules. Next article (103) we will look into functions - though we have used some of them so far. From 104 - our focus will be more practical coding to use modules/functions and along the way we will cover the fundamentals of Python programming.


Happy Learning.


 
 
 

Comments


I Sometimes Send Newsletters

Thanks for submitting!

bottom of page