ARE YOU A LIBRARY MOUSE?

--Originally published at Coding The Future

Image by Dmitrij Paskevic

Hello everyone! Guess what? Partials are over! That means that today we're back on track... And today's topic is libraries, a.k.a. modules in Python.

Let's begin by defining modules. TutorialsPoint defines a module as a logical way to "organize your Python code. Grouping related code into a module makes the code easier to understand and use".1 In short, a module is a separate file where you define functions related to a specific action and then import it to your code. The best thing about it all is that it makes coding less confusing and functions re-usable across several programs!

Let's get started!

Creating a module

Creating a module is easy. All you have to do is create a new Python file and define the functions that will belong to that module.2 For example, if you wanted to create a module containing all the functions for Tic Tac Toe, you would create a module named tictactoe and define all the functions inside.

Using a module

Once you wanted to use the library you just created or downloaded from the internet, the first step would be to import it. In the file where you will use it, type the word import followed by the name of the module. Let's say we wanted to import the tictactoe.py module we imaginarily created above:

import tictactoe

That imports the module, but in order to use the functions inside it, you need to call the function by using the name of the module (dot) name of the function. Let's say I wanted to use the newGame function within the tictactoe module:

tictactoe.newGame()

That's it for today... I know, that was easy! Happy Mexican Independence Day! ??

@emamex98

--

  1. Definition by TutorialsPoint

  2. Tutorial by Python Software

comments powered by Disqus