0% found this document useful (0 votes)
1 views

Learn Python 3_ Modules Cheatsheet _ Codecademy

The document provides an overview of using modules in Python, specifically focusing on the datetime module for handling dates and times, and the random module for generating random numbers. It explains how to import modules using different methods and demonstrates aliasing with the 'as' keyword. Additionally, it includes examples of creating date and time objects, as well as generating random integers and selecting random items from a list.

Uploaded by

tw8qsqf9n7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Learn Python 3_ Modules Cheatsheet _ Codecademy

The document provides an overview of using modules in Python, specifically focusing on the datetime module for handling dates and times, and the random module for generating random numbers. It explains how to import modules using different methods and demonstrates aliasing with the 'as' keyword. Additionally, it includes examples of creating date and time objects, as well as generating random integers and selecting random items from a list.

Uploaded by

tw8qsqf9n7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Learn Python 3

Modules

Date and Time in Python


Python provides a module named datetime to import datetime
deal with dates and times.
feb_16_2019 = datetime.date(year=2019,
It allows you to set date , time or both date
month=2, day=16)
and time using the date() , time() and
datetime() functions respectively, after feb_16_2019 = datetime.date(2019, 2, 16)
importing the datetime module . print(feb_16_2019) #2019-02-16

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 with ‘as’ keyword


In Python, the as keyword can be used to give an # Aliasing matplotlib.pyplot as plt
alternative name as an alias for a Python module or
from matplotlib import pyplot as plt
function.
plt.plot(x, y)

# Aliasing calendar as c
import calendar as c
print(c.month_name[1])
Import Python Modules
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 import module
functions , or from module import * . module.function()
from module import * is discouraged, as it
can lead to a cluttered local namespace and can make
# Second way
the namespace unclear.
from module import function
function()

# Third way
from module import *
function()

random.randint() and random.choice()


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 r1 = random.randint(0, 10)
random selection from a range of integers. The
print(r1) # Random integer where 0 <= r1
choice() method provides a uniform selection of
<= 10
a random element from a sequence.

# Prints a random element from a sequence


seq = ["a", "b", "c", "d", "e"]
r2 = random.choice(seq)
print(r2) # Random element in the
sequence

Module importing
In Python, you can import and use the content of # file1 content
another file using import filename , provided
# def f1_function():
that it is in the same folder as the current file you are
writing. # return "Hello World"

# file2
import file1

# Now we can use f1_function, because we


imported file1
f1_function()

Print Share

You might also like