Learn Python 3 - Modules Cheatsheet - Codecademy
Learn Python 3 - Modules Cheatsheet - Codecademy
Modules
Python provides a module named datetime to deal with dates and times. import datetime
It allows you to set date , time or both date and time using the
feb_16_2019 = datetime.date(year=2019, month=2, day=16)
date() , time() and datetime() functions respectively, after importing the datetime
module . feb_16_2019 = datetime.date(2019, 2, 16)
print(feb_16_2019) #2019-02-16
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-modules/cheatsheet 1/4
09/12/2023, 08:12 Learn Python 3: Modules Cheatsheet | Codecademy
In Python, the as keyword can be used to give an alternative name as an alias for a # Aliasing matplotlib.pyplot as plt
Python module or function.
from matplotlib import pyplot as plt
plt.plot(x, y)
# Aliasing calendar as c
import calendar as c
print(c.month_name[1])
The Python import statement can be used to import Python modules from other files. # Three different ways to import modules:
Modules can be imported in three different ways: import module , from module
# First way
import functions , or from module import * . from module import * is
discouraged, as it can lead to a cluttered local namespace and can make the import module
namespace unclear. module.function()
# Second way
from module import function
function()
# Third way
from module import *
function()
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-modules/cheatsheet 2/4
09/12/2023, 08:12 Learn Python 3: Modules Cheatsheet | Codecademy
In Python, the random module offers methods to simulate non-deterministic # Returns a random integer N in a given range, such that
behavior in selecting a random number from a range and choosing a random item from
start <= N <= end
a list.
The randint() method provides a uniform random selection from a range of integers. # random.randint(start, end)
The choice() method provides a uniform selection of a random element from a r1 = random.randint(0, 10)
sequence.
print(r1) # Random integer where 0 <= r1 <= 10
Module importing
In Python, you can import and use the content of another file using import filename , # file1 content
provided that it is in the same folder as the current file you are writing.
# def f1_function():
# return "Hello World"
# file2
import file1
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-modules/cheatsheet 3/4
09/12/2023, 08:12 Learn Python 3: Modules Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-modules/cheatsheet 4/4