Class objectives

Syntax table

A module is a file containing Python definitions and statements.
Standard Modules
Python comes with a library of standard modules, described in a separate document, the Python Library Reference (“Library Reference” hereafter). Some modules are built into the interpreter; these provide access to operations that are not part of the core of the language but are nevertheless built in, either for efficiency or to provide access to operating system primitives such as system calls. The set of such modules is a configuration option which also depends on the underlying platform.
The built-in function dir() is used to find out which names a module defines.


A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called in the current directory with the following contents:

How to create a module?

from turtle import *

def drawsquare(sidelength):
	for each_line in range(4):

def drawtriangle(sidelength):

How to import a module?

Now enter the Python interpreter and import this module with the following command:

import toolbox


from toolbox import *


from toolbox import drawsquare, drawtriangle

How to call module functions?

Create another .py file for example is Call toolbox functions from

from toolbox import *



import toolbox



import toolbox

drawsquare = toolbox.drawsquare

How to call a module from command line?

When you run a Python module with

python3 <arguments>

the code in the module will be executed, just as if you imported it, but with the name set to “main”. That means that by adding this code at the end of your module:

if __name__ == "__main__":
	import sys

Execute the module from Terminal for example:

python3 100

Execution result will be a square with side length of 100.

Standard Modules

The dir() Function

The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings:

import toolbox, sys


dir() lists the names you have defined currently:

['Pen', 'RawPen', 'RawTurtle', 'Screen', 'ScrolledCanvas', 'Shape', 'Terminator', 'Turtle', 'TurtleScreen', 'Vec2D', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add', 'addshape', 'back', 'backward', 'begin_fill', 'begin_poly', 'bgcolor', 'bgpic', 'bk', 'bye', 'circle', 'clear', 'clearscreen', 'clearstamp', 'clearstamps', 'clone', 'color', 'colormode', 'degrees', 'delay', 'distance', 'divi', 'done', 'dot', 'down', 'drawsquare', 'drawtriangle', 'end_fill', 'end_poly', 'exitonclick', 'fd', 'fillcolor', 'filling', 'floordivi', 'forward', 'get_poly', 'get_shapepoly', 'getcanvas', 'getpen', 'getscreen', 'getshapes', 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown', 'isvisible', 'left', 'listen', 'lt', 'mainloop', 'mode', 'mul', 'numinput', 'onclick', 'ondrag', 'onkey', 'onkeypress', 'onkeyrelease', 'onrelease', 'onscreenclick', 'ontimer', 'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'register_shape', 'reset', 'resetscreen', 'resizemode', 'right', 'rt', 'screensize', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setup', 'setworldcoordinates', 'setx', 'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'speed', 'st', 'stamp', 'sub', 'textinput', 'tilt', 'tiltangle', 'title', 'towards', 'tracer', 'turtles', 'turtlesize', 'undo', 'undobufferentries', 'up', 'update', 'width', 'window_height', 'window_width', 'write', 'write_docstringdict', 'xcor', 'ycor']

Looking for more fun?

We have a software called pip, from where we can install unlimited sources of Python modules and packages.

What is Pip?

pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes. Pip usually comes with Python3 by default. Look at the simple usage of pip below:

pip3 install SomePackage

How to install a package?

Take Freegames package as an example. Open Terminal(in Mac) / CMD(in Windows) software copy/paste following command:

python3 -m pip install freegames

Installation session ends with following information if it’s succussful.

Collecting freegames
  Downloading (108kB)
    100% |████████████████████████████████| 112kB 413kB/s 
Installing collected packages: freegames
Successfully installed freegames-2.3.1

Let’s try our first game

python3 -m freegames.pacman


Create a calculator module, import it from another Python file called

Required features of the calculator:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Floor division code probably looks like below:

from calculator import add, sub, mul, divi, floordivi

print(add(8, 13))