def calcular_cargamento(toneladas): # 1ton -> 1000 kg # 1 box -> 10kg # 1 hass -> 0.25 kg # 1 tree -> 40 hass -> 10kg kg = multiply(toneladas, 1000) arboles = divide(kg, 10) aguacates = multiply(arboles, 40) return_str = f"Para completar {toneladas} toneladas del cargamento se recogieron {aguacates} aguacates de {arboles} arboles" return return_str
from utility import multiply, divide, max from shopping.more_shopping.shopping_cart import buy import random import sys if __name__ == '__main__': # executes if this file is being run print(multiply(2, 3)) # => 6 print(divide(2, 3)) # => 0.666666666666666 print(buy('apple')) # ['apple'] print(max()) # oops print(__name__) # __main__ print(random) # <module'random' from 'C:\\Users\\alexc\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\random.py'> help(random) # gives helpful information about the random package print(dir(random)) # lists every method you can use within the random package print(random.random()) # gives a random number between 0 and 1 print(random.randint(1, 10)) # gives a random integer inclusively between two parameters print(random.choice([1, 2, 3, 4, 5])) # picks a random value within the array my_list = [1, 2, 3, 4, 5] random.shuffle(my_list) print(my_list) # shuffled list print(sys) # <module 'sys' (built-in)> sys.argv # runs files from the terminal
from utility import multiply, divide from shopping.more_shopping import shopping_cart import random print(random) print(shopping_cart.buy('apple')) print(divide(4, 3)) print(multiply(5, 4)) print(max([1, 2, 3])) if __name__ == '__main__': print('please run this')
# Package - a folder containing modules # from shopping import shopping_cart import shopping.shopping_cart import utility print(utility) print(utility.multiply(2, 3)) print(utility.divide(2, 3)) print(shopping.shopping_cart) print(shopping.shopping_cart.buy('apple'))
from utility import multiply, divide from shopping import shopping_cart if __name__ == "__main__": print(shopping_cart.buy('apple')) print(divide(5, 2)) print(multiply(5, 2)) print(max([1, 2, 3]))
from utility import multiply, divide from shopping.shopping_cart.more_shopping.cart import shopping_cart print(multiply(3, 3)) print(shopping_cart.buy('apple')) if __name__ == '__main__': print('this is the main file')
import random import utility import my_package.utils as utils from my_package.stuff import stuff, more_stuff # The pycache is created everytime we use modules. It contains compiled files. # The file that we run has __name__ set to __main__. # A package is simply a folder containing multiple modules inside. At the # root of the package there's always a file named __init__.py, used by the # interpreter to recognize a Python package. # the reason for you may see something similar is that you want a code to # be executed only if you run this file if __name__ == '__main__': print(utility.multiply(5, 6)) utils.mypack([1, 2, 3]) stuff() more_stuff() print(random.random())
import utility import shopping.shopping_card print(utility.multiply(10,20)) print(shopping.shopping_card.buy('hola')) print('prueba')
import utility # a package is a folder containing modules # use . syntax to go down a directory to load modules from a package import shopping.shopping_cart # or to access functions directly use from package.module import functioname from shopping.shopping_cart import buy from utility import multiply, divide # use * syntax to import every function from utility import * # prints the filepath print(utility) # use . syntax to run methods print(utility.divide(10, 5)) print(multiply(2, 5)) # use . syntax to go down a directory to run methods print(shopping.shopping_cart.buy('apple')) print(buy('banana')) # imported files return their name when they contain print(__name__) # when the statement is not imported, but run directly from the file it will return "__main__" # wont run because buy method was imported from shopping cart and not the main module if __name__ == "__main__": buy('strawberry pi') def add(num1, num2): return num1 + num2
import utility import shopping.shopping_cart if __name__ == "__main__": print(utility.multiply(2, 4)) print(shopping.shopping_cart.buy('Banana'))
import utility #another way to import modules from utility import multiply, divide #( we can anso use * to import all modules at a time) import shopping.more_shopping.shopping_cart # another way to import packages from shopping.more_shopping import shopping_cart print(utility.multiply(3, 4)) print(utility.divide(4, 2)) print(utility.multiply(5, 10)) print(utility.divide(25, 5)) print(shopping.more_shopping.shopping_cart.buy('apple')) print(shopping_cart.buy('orange')) if __name__ == '__main__': print('please run this')
import utility print(utility) utility.multiply(2, 3)