# modules in python is a file (eg as sections in supermarket) #we refer to each file as a module import converters print(converters.kg_to_lbs(90)) print(converters.lbs_to_kg(150)) from utils import find_max numbers = [1, 4, 5, 8, 10] max = find_max(numbers) print(max)
# A simple use of user modules in python from utils import find_max numbers = [0, 2, 8, 10, 280, 8, 90, 800, 7000, 29, 5] max_number = find_max(numbers) print(max_number)
import converters import utils from ecommerce import shipping from converters import kg_to_lbs from utils import find_max kg_to_lbs(100) print(converters.kg_to_lbs(70)) numbers = [10, 3, 6, 2] maximum = find_max(numbers) print(maximum) shipping.calc_shipping()
from utils import find_max myList = [10, 3, 5, 8] maximum = find_max(myList) print(maximum) # or print(max(myList))
import utils number_list = [2, 4, 3, 5, 6, 4, 3] print(utils.find_max(number_list))
import utils as util import platform import datetime globalVar: str = "WELCOME" max_numb = util.find_max([23, 45, 100, 56]) lbs = util.kg_to_lbs(63) print(platform.system()) # Tuple x = ('AAA', 'BBB', 'CCC') y = list(x) # change Tuple to list dic = {"Name": "Stalin", "Age": 30, "IsMale": True} for key, val in dic.items(): print(key, val) # Class class Person: def __init__(self, fname, lname): self.fname = fname self.lname = lname def print_name(self): print(f'-> {self.fname}, {self.lname}') class Student(Person):
from utils import find_max table = [10, 3, 6, 2] print(find_max(table))
from utils import find_max print(find_max([10,2,8,9,3,4])) print(max([10,2,8,9,3,4])) #max is a builtin functions
from utils import find_max names = ['Rajeev', 'Anjali', 'Kriti', 'Sarthak'] names[2] = 'Kittu' # Updates list's elements print(names) # Exercise - Maximum number in list myList = [2, 4, 3, 6, 7] print(find_max(myList)) # 2D Lists lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(lists)
import utils arr = [1, 2, 3, 4, 5, 33, 22, 7, 13, 18] print(utils.find_max(arr))
# import utils as ut from utils import find_max print(find_max([5, 20, 10, 14]))
print() class Mammal: def walk(self): print('Walk') class Dog(Mammal): def bark(self): print('Bark') class Cat(Mammal): def mew(self): print('Mew') d1 = Dog() d1.walk() d1.bark() c1 = Cat() c1.walk() c1.mew() print(converters.lbs_to_kg(145)) print(kgs_to_lbs(49)) print(find_max([1, 34, 56, 2, 3, 67, 299, 1, 23])) calc_shipping()
# modules - organize CODE (2 approaches) import converters from converters import kg_to_lbs # all functions print(kg_to_lbs(100)) print(converters.kg_to_lbs(70)) # Exercise import utils numbers = [8, 1, 19, 20, 30, 40, 1, 5, 6, 7, 99, 2, 20] print(utils.find_max(numbers)) print(utils.find_max2(numbers)) print(max(numbers))
from utils import find_max from importMe import import_me as test from importMe import import_me from ecommerce.shipping import calculate_shipping numbers = [0, 5, 2, 1, 6, 909, 10, 77, 100] print(test()) print(import_me()) print(find_max(numbers)) print(find_max([]))
# write a function find_max to calculate the maximum # should accept a list as input, find the maximum and return the largest number # put this in a separate module called utils # import utils into the current module and call this function. Print the result import utils import random source_list = input("Enter a set of numbers : ") source_list_copy = source_list.split(' ') max_value_copy = utils.find_max(source_list_copy) print(utils.find_max(source_list_copy)) print(max_value_copy) print(random.random()) # Rolling a die. # To define a class called Dice; have a method called roll which should generate 2 random values class Dice: def roll(self): first_roll = random.randint(1, 6) second_roll = random.randint(1, 6) return first_roll, second_roll dice_copy = Dice() print(dice_copy.roll())
print(sum) # print 'F' numbers=[5,2,5,2,2] for x in numbers: str='' for y in range(x): str+='X' print(str) #print 'L' numbers=[2,2,2,2,7] for x in numbers: str='' for y in range(x): str+='X' print(str) ''' # calling import from utils import utils maxim = utils.find_max([24, 2, 3, 12]) print(maxim)
import utils print(utils.find_max(range(1100)))
import utils print(utils.find_max())
#para poder usar los modulos de otro archivo lo que hay que hacer es colocar la palabra import y luego el nombre del archivo que se desea importar en este caso se llama convertidor # y como se puede ver ya se pueden utilizar los metodos que estan dentro de convertidor import convertidor #ademas tambien se puede llamar a una funcion en especifico de la siguiente manera y de esa manera ya no es necsario que se coloque el convertidor al inicio sino que solo se manda a llamar la opcion from convertidor import lbs_to_kgs import utils print(convertidor.kg_to_lbs(70)) print(lbs_to_kgs(100)) utils.find_max([15, 75, 85, 65])
print('I am running') a_man = Man('John') a_man.talk() import converters # from converters import kg_to_lbs # kg_to_lbs(100) print(converters.kg_to_lbs(70)) from utils import find_max numbers = [10, 3, 4, 6, 2] maxNumber = find_max(numbers) print(maxNumber) print(max(numbers)) # package # import ecommerce.shipping from ecommerce.shipping import calc_shipping from ecommerce import shipping calc_shipping() shipping.calc_shipping() members = ["Andy", "Li", "Claire", "Helo"] print(random.choice(members))
import converters #you can import entire module 2 ways from converters import lbs_to_kg #u can use method directly import utils from utils import find_max kg20 = converters.kg_to_lbs(20) print(kg20) lbs20 = lbs_to_kg(70) print(lbs20) num = [34, 33, 55, 77, 88, 33, 22] num2 = 4 utils.find_max(num) utils.find_max(num2) find_max(num)
from utils import find_max num = [10, 4, 6, 20, 5] high = find_max(num) print(high)
# ## import converters from converters import kg_to_lbs print(kg_to_lbs(20)) print(converters.kg_to_lbs(2)) ### # # # Execise # # ## from utils import find_max print(find_max()) exit() #----------------------------End of the Tutorial ---------------------------------# ### # # # Classes Inheritance # It is a mechanism for using code # dry: dont repeat yourself # ## class Mammal: def walk(self): print("Walk")
import math import converters from converters import kg_to_lbs from converters import lbs_to_kg from utils import find_max weight = int(input("Your Weight: ")) unit = input("L(bs) or K(g): ") convert = 0 if unit.lower() == 'k': convert = converters.kg_to_lbs(weight) print(f'Your Weight is {convert} Lbs') elif unit.lower() == 'l': convert = lbs_to_kg(weight) print(f'Your Weight is {convert} kg') else: print('something went wrong') my_list = [4, 32, 100, 112, 21, 43] print(find_max(my_list))
from utils import find_max numbers = [10, 20, 30] print(find_max(numbers))
import converter import utils print(converter.kg_to_lbs(120)) print(converter.lbs_to_kg(120)) numbers = [12, 3, 4, 5, 60, 78, 9] max_num = utils.find_max(numbers) print(max_num)
import utils list_max = utils.find_max([200, 17, 1, 11, 300, 400, 1]) print(list_max) try_alpha = utils.find_max(['C', 'T', 'a', 'r', 't', 'R']) try_alpha2 = utils.find_max(['C', 'T', 'R']) try_alpha3 = utils.find_max(["Zebra", "Cat", "Dog", "Elephant", "Anteater"]) print(try_alpha3)
class Cat(Mammal): pass dog1 = Dog() dog1.walk() dog1.bark() # Thirtieth-second tutorial: Modules import converters # You can also use: from converters import kg_to_lbs. No need to prefix with module name. print(converters.kg_to_lbs(70)) # Exercise: defining module import utils list = [2, 34, 25, 2, 56, 23, 6, 54, 3] print(utils.find_max(list)) # Thirtieth-third tutorial: Packages # Add a _init_.py file in the directory. Or add directly a new package import ecommerce.shipping ecommerce.shipping.calc_shipping() from ecommerce.shipping import calc_shipping # Also: from ecommerce import shipping calc_shipping() # Thirtieth-forth tutorial: Generating Random Values # Search Python 3 module index to see all python built-in modules import random for i in range(3): print(random.random())
import utils numbers = [245,512,51] maximum = utils.find_max(numbers) minimum = utils.find_min(numbers) ave = utils.average(numbers) print(ave)
import utils from utils import find_max print(utils.find_max([1,10,5,6,3,2,0])) print(find_max([100,1234,12,1324,22])) from ecommerce import shipping shipping.calc_shipping() from ecommerce.shipping import calc_shipping,calc_tax calc_tax() calc_shipping()
numbers = [10, 3, 6, 2] print(max) # New python file called utils (code won't work as I didn't actually create the file) # utils: def find_max(numbers): max = numbers[0] for number in numbers: if number > max: max = number return max # here import utils utils.find_max() # OR from utils import find_max numbers = [10, 3, 6, 2] max = find_max(numbers) print(max) # warning as overwritten built-in max function numbers = [10, 3, 6, 2] print(max(numbers)) # above no longer works as have overwritten max function. If want to do this, call max function maximum