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

Learn Python 3 - Modules Cheatsheet - Codecademy

The document discusses several topics related to working with modules in Python: 1) The datetime module allows working with dates and times using functions like date(), time(), and datetime() to create date, time, and timestamp objects. 2) The as keyword can be used to create aliases for imported modules or functions for brevity. 3) Modules can be imported using import, from import, or from import * syntax. from import * is discouraged due to namespace issues. 4) The random module provides randint() and choice() functions to generate random integers within a range and select random elements from sequences. 5) Modules from other files can be imported and used if they are in the same folder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Learn Python 3 - Modules Cheatsheet - Codecademy

The document discusses several topics related to working with modules in Python: 1) The datetime module allows working with dates and times using functions like date(), time(), and datetime() to create date, time, and timestamp objects. 2) The as keyword can be used to create aliases for imported modules or functions for brevity. 3) Modules can be imported using import, from import, or from import * syntax. from import * is discouraged due to namespace issues. 4) The random module provides randint() and choice() functions to generate random integers within a range and select random elements from sequences. 5) Modules from other files can be imported and used if they are in the same folder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Learn Python 3

Módulos
Fecha y hora en Python
Python proporciona un módulo llamado datetime para
tratar fechas y horas. import datetime
Le permite con gurar date , time o ambas date y feb_16_2019 = datetime.date(year=2019,
time usar las funciones date() , time() y month=2, day=16)
datetime() respectivamente, después de importar el feb_16_2019 = datetime.date(2019, 2, 16)
datetime módulo.
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

Alias con la palabra clave "como"


En Python, la as palabra clave se puede usar para dar un
nombre alternativo como alias para un módulo o función # Aliasing matplotlib.pyplot as plt
de Python. from matplotlib import pyplot as plt
plt.plot(x, y)

# Aliasing calendar as c
import calendar as c
print(c.month_name[1])
Importar módulos de Python
The Python import statement can be used to import
Python modules from other les. # Three different ways to import modules:
Modules can be imported in three di erent ways: import # First way
module , from module import functions , or from module import module
import * . from module import * is discouraged, as it can
module.function()
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()

random.randint() and random.choice()


In Python, the random module o ers methods to
simulate non-deterministic behavior in selecting a # Returns a random integer N in a given
random number from a range and choosing a random range, such that start <= N <= end
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
<= 10
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 another
le using import filename , provided that it is in the same # file1 content
folder as the current le you are writing. # def f1_function():
# return "Hello World"

# file2
import file1

# Now we can use f1_function, because we


imported file1
f1_function()

You might also like