30 Days of Python (20 Part Series)
If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.
Modules are chunks of code, saved in a file, that are made to help you do things easier. A module could be a file containing a single variable, or function, a massive codebase.
A module is a python script, saved as filename.py
Here's an example of something you may want to use later.
# fullname.py def generate_full_name(firstname, lastname): space = ' ' fullname = firstname + space + lastname return fullname
In order to use a module that you've created, you'll need to
import it in a different file. Once imported, it can be used anywhere in your new file.
The syntax for using an imported module is
import fullname print(fullname.generate_full_name('Vicki', 'Langer'))
You can use multiple functions from a module, by importing each one.
To do this, you'll follow the below syntax. Each script may have many functions. If you don't want to import allllllll of them, this is a great option.
from modulename import feed_pet, generate_full_name
There are modules that are already included with Python. Here are some common ones.
import math print(math.pi) >>> 3.141592653589793
random gives pseudo-random outputs
from random import random, randint print(random()) # returns something bewtween 0 and 0.9999 print(randint(3, 72)) # returns a number between two arguments
Series based on