Learn Python 3_ Modules Cheatsheet _ Codecademy
Learn Python 3_ Modules Cheatsheet _ Codecademy
Modules
time_13_48min_5sec =
datetime.time(hour=13, minute=48,
second=5)
time_13_48min_5sec = datetime.time(13, 48,
5)
print(time_13_48min_5sec) #13:48:05
timestamp= datetime.datetime(year=2019,
month=2, day=16, hour=13, minute=48,
second=5)
timestamp = datetime.datetime(2019, 2, 16,
13, 48, 5)
print (timestamp) #2019-01-02 13:48:05
# Aliasing calendar as c
import calendar as c
print(c.month_name[1])
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-modules/cheatsheet 1/3
12/3/24, 3:18 PM Learn Python 3: Modules Cheatsheet | Codecademy
The Python import statement can be used to import # Three different ways to import modules:
Python modules from other files.
# First way
Modules can be imported in three different ways: import
module , from module import functions , or from import module
module import * . from module import * is module.function()
discouraged, as it can lead to a cluttered local namespace
and can make the namespace unclear.
# Second way
from module import function
function()
# Third way
from module import *
function()
In Python, the random module offers methods to # Returns a random integer N in a given
simulate non-deterministic behavior in selecting a
range, such that start <= N <= end
random number from a range and choosing a random
item from a list. # random.randint(start, end)
The randint() method provides a uniform random r1 = random.randint(0, 10)
selection from a range of integers. The choice() method
print(r1) # Random integer where 0 <= r1
provides a uniform selection of a random element from a
sequence. <= 10
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-modules/cheatsheet 2/3
12/3/24, 3:18 PM Learn Python 3: Modules Cheatsheet | Codecademy
Module importing
In Python, you can import and use the content of another # file1 content
file using import filename , provided that it is in the
# def f1_function():
same folder as the current file you are writing.
# return "Hello World"
# file2
import file1
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-modules/cheatsheet 3/3