Modules Flashcards
(8 cards)
How do you connect a file to another?
if I have a file sandwiches.py and another file hungry_people.py, how do I give my hungry people access to all the sandwiches I defined?
Well, files are actually modules, so you can give a file access to another file’s content using that glorious import statement.
With a single line of from sandwiches import sandwiches at the top of hungry_people.py, the hungry people will have all the sandwiches they could ever want.
Ejemplo:
Si tengo dos archivos, uno script.py y otro library.py donde hay una función llamada always_three, puedo importar esa función en script:
Import library below:
from library import always_three
Call your function below:
always_three()
How do you put an alias to a module?
import module_name as name_you_pick_for_the_module
How do you use random.sample() ?
The function random.sample() takes a range and a number as its arguments. It will return the specified number of random numbers from that range. #random.sample takes a list and randomly selects k items from it new_list = random . sample (list, k ) #for example: nums = [ 1 , 2 , 3 , 4 , 5 ] sample_nums = random . sample ( nums , 3 ) print( sample_nums ) # 2, 5, 1
What is a module in Python?
A module is a collection of Python declarations intended broadly to be used as a tool. Modules are also often referred to as ‘libraries’ or ‘packages’.
What is the “wildcard” for importing?
You might also occasionally encounter import *.
The * is known as a “wildcard” and matches anything and everything.
This syntax is considered dangerous because it could pollute our local namespace.
Pollution occurs when the same name could apply to two possible things.
For example, if you happen to have a function floor() focused on floor tiles, using from math import * would also import a function floor() that rounds down floats.
What is the syntax to use a module?
To use a module in a file, you need to put at the top of that file:
from module_name import object_name
Ejemplo
from datetime import datetime
current_time=datetime.now()
==> current_time es igual a 2022-07-05 13:55:42.559504
Si vamos a usar más de un objeto del módulo podemos importar el módulo entero:
import random
Why would you use the module decimal?
If you used Python’s built-in floating-point arithmetic to calculate a sum, it would result in a weirdly formatted number.
cost_of_gum = 0.10
cost_of_gumdrop = 0.35
cost_of_transaction = cost_of_gum + cost_of_gumdrop
# Returns 0.44999999999999996
Being familiar with rounding errors in floating-point arithmetic you want to use a data type that performs decimal arithmetic more accurately. You could do the following:
from decimal import Decimal
cost_of_gum = Decimal(‘0.10’)
cost_of_gumdrop = Decimal(‘0.35’)
cost_of_transaction = cost_of_gum + cost_of_gumdrop
# Returns 0.45 instead of 0.44999999999999996
name two common random functions
random.randint() which takes two numbers as arguments and generates a random number between the two numbers you passed in.
Ejemplo:
random_list=[random.randint(1,100) for num in range(101)]
print(random_list)
Imprime:
[93, 30, 23, 83, 68, 96, 19, 91, 32, 88, 46, 31, 6, 20, 100, 82, 86, 78, 23, 53, 13, 33, 56, 75, 93, 80, 5, 47, 45, 75, 32, 100, 57, 34, 4, 90, 99, 79, 80, 19, 46, 57, 61, 3, 78, 17, 14, 52, 26, 61, 63, 91, 1, 64, 75, 51, 30, 25, 11, 6, 11, 42, 33, 67, 22, 94, 97, 52, 7, 51, 33, 75, 69, 77, 12, 30, 91, 50, 50, 18, 93, 91, 83, 41, 51, 13, 84, 64, 10, 33, 14, 54, 52, 25, 60, 62, 85, 96, 17, 85, 57]
random.choice() which takes a list as an argument and returns a number from the list
Ejemplo:
randomer_number=random.choice(random_list)
print(randomer_number) –> imprime un numero random de la lista