import myHeaders myHeaders.printl("Random Number Generator", 24) import random for i in range(3): print(random.random()) for i in range(3): print(random.randint(10, 20)) people = ["Jimmy", "Kim", "Henry"] print(f"Leader:\t{random.choice(people)}") class Dice: def roll(self): return (random.randint(1, 6), random.randint(1, 6)) dice = Dice() print(dice.roll()) print(dice.roll()) print(dice.roll()) print(type(dice.roll()))
import myHeaders myHeaders.printl("If Statements", 10) is_hot = bool(int(input('Is it warm? '))) is_cold = bool(int(input('Is it cold? '))) if is_hot: print("It's a hot day") print("Drink plenty of water") elif is_cold: print("It's a cold day\nWear warm clothes") else: print("It's a lovely day!") print('Enjoy your day') if is_hot and is_cold: print("It's some anomaly") if (is_hot or is_cold) and not (is_hot == is_cold): print("Extreme weather") temperature = int(input("What is the temperature? ")) if temperature > 30: print("It's a hot day") else: print("It's not a hot day") while True: nick = input("Try a nick: ") if len(nick) < 3:
import myHeaders myHeaders.printl("String Methods", 7) course = 'Python for beginners' print(len(course)) #String length (list length) print(course.upper()) #Returns string in uppercase print(course.lower()) print(course.title() ) #Returns string with first letter of each word in uppercase print(course.find('gin')) #Returns indeks of the first occurence of arg #Returns -1 if not found print(course.replace('beginners', 'absolute beginners')) #replaces arg1 in string with arg2 print('Python' in course ) #Expretion returns <Bool> True if first string is in the second one
import myHeaders myHeaders.printl("While Loops", 11) i = 1 while i <= 5: print('*' * i) i += 1 print("Done")
import myHeaders myHeaders.printl("Functions", 17) def greeting(): print("Hello there!") print("General Kenobi!") print("Start") greeting() print("Finish") def personalized_greeting(name): print(f"Hello {name}!") print("Nice shoes.") personalized_greeting("Susan") def personalized_greeting_defaulted(name="Operator"): print(f"Hello {name}!") print("Nice shoes.") personalized_greeting_defaulted()
import myHeaders myHeaders.printl("Getting input", 3) name = input('What is your name? ') color = input('What is your favourite color? ') print(name + "'s favourite color is " + color)
import myHeaders myHeaders.printl("Files and Directories", 25) from pathlib import Path # Absolute path #/home/documents # Relative path - from current directory path = Path("ecommerce") print(path.exists()) #true path = Path("Emails") print(path.exists()) #false path.mkdir() print(path.exists()) #true path.rmdir() print(path.exists()) #false path = Path("..") for file in path.glob("*"): print(file)
import myHeaders myHeaders.printl("For Loops", 12) for item in 'Python': print(item) for item in ['Mosh', 'John', 'Sarah']: print(item) for item in range(5, 10, 2): print(item) prices = [10, 42, 13, 27, 43] value = 0 for price in prices: value += price print(f"Sum: {value}") for x in range(2): for y in range(3): print(f"({x},{y})") numbers = [2, 2, 2, 2, 5] for nr in numbers: line = '' for i in range(nr): line += 'x' print(line)
import myHeaders myHeaders.printl("Dictionaries", 16) customer = { "name": "John Smith", "age": 30, "is_verified": True, } print(customer["name"]) print(customer.get("birthdate")) print(customer.get("birthdate", "jan 1 1980")) #jan 1 1980 default value print(customer.get("birthdate")) customer["name"] = "kapitan John Smith" print(customer.get("name")) customer["birthdate"] = "feb 2 1990" print(customer.get("birthdate")) #Number spell numbers = { "0": "zero", "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six",
import myHeaders myHeaders.printl("Type conversion", 4) birth_year = input('Birth year: ') print(type(birth_year)) age = 2019 - int(birth_year) print(type(age)) print(age) weight_p = float(input("What is your weight in punds? ")) print(weight_p * 0.4536)
import myHeaders myHeaders.printl("Arythmetic Operations", 8) print(10 / 3) print(10 // 3) print(10**3) x = 10 x += 3 print(x) x = 10 + 3 * 2**2 print(x)
import myHeaders myHeaders.printl("Classes", 19) class Point: def move(self): print("move") def draw(self): print("draw") point1 = Point() point1.draw() point1.x = 10 point1.y = 20 print(point1.x) point2 = Point() #print(point2.x) Error. No x in point2
import myHeaders myHeaders.printl("Lists2d", 14) matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(f"Element (1,2): {matrix[1][2]}") for row in matrix: for element in row: print(element)
import myHeaders myHeaders.printl("printing", 1) print("Karol Kocierz") print("o----") print(" ||||") print('*' * 10)
import myHeaders myHeaders.printl("Inheritance", 21) class Mammal: def walk(self): print("walk") class Dog(Mammal): def bark(self): print("Woof") class Cat(Mammal): def screech(self): print("Meow") dog1 = Dog() dog1.walk() cat = Cat() cat.walk() cat.screech()
import myHeaders myHeaders.printl("Construtors", 20) class Point: def __init__(self, x, y): self.x = x self.y = y def move(self): print("move") def draw(self): print("draw") point = Point(10, 20) print(f"Point coords: ({point.x}, {point.y})") point.x = 15 print(f"Point coords: ({point.x}, {point.y})") class Person: def __init__(self, name): self.name = name def talk(self): print(f"I am {self.name}!") tony = Person("Iron Man")
import myHeaders myHeaders.printl("Mathematic Functions", 9) import math x = 2.9 print(round(x)) #Rounds the number print(abs(-2.9)) #Returns absolute print(math.ceil(2.9)) #ceiling print(math.floor(2.9)) #floor
import myHeaders myHeaders.printl("Modules", 22) import converters print(converters.kg_to_lbs(70)) from converters import kg_to_lbs print(kg_to_lbs(70)) import utils number_list = [3, 5, 3, 4, 6, 4, 5] print(f"Maximum from list:\t{number_list}\nIs {utils.find_max(number_list)}")
import myHeaders myHeaders.printl("Unpacking", 15) coordinates = (1, 2, 3) x, y, z = coordinates print(coordinates) print(f"x = {x}, y = {y}, z = {z}")
import myHeaders myHeaders.printl("Strings", 5) course = 'Python for "beginners"' print(course) course = "Beginner's course of python" print(course) course = ''' Hi hello lot of lines ''' print(course) course = 'Python for beginners' print(course[0]) # p course = 'Python for beginners' print(course[-1]) # s course = 'Python for beginners' print(course[-2]) # r course = 'Python for beginners' print(course[0:3]) # Pyt course = 'Python for beginners'
import myHeaders myHeaders.printl("Exceptions", 18) try: age = int(input("Age: ")) income = 20000 risk = income / age print(age) except ValueError: print("You antered a nan.") except ZeroDivisionError: print("0 is an invalid value.")
import myHeaders myHeaders.printl("Formatted Strngs", 6) first = 'John' last = 'smith' message = first + ' [' + last + '] is a coder.' msg = f'{first} [{last}] is a coder.' #'f' before string makes formatted string #Allows adding variables to the string with {} print(message) print(msg)
import myHeaders myHeaders.printl("Lists", 13) names = ['Walt', 'Jesse', 'Mike', 'Saul', 'Hank'] print(names) print(names[-2]) print(names[2:4]) numbers = [3, 1, 4, 4, 8, 2, 1] maximum = numbers[0] for value in numbers: if maximum < value: maximum = value print(maximum) print(numbers) numbers.append(13) print(numbers) numbers.insert(0, 10) print(numbers) numbers.remove(1) print(numbers) numbers.pop() print(numbers) print(numbers.index(8)) print(50 in numbers) print(numbers.count(4)) numbers.sort() print(numbers) numbers.reverse()
import myHeaders myHeaders.printl("variables", 2) price = 10 print('price = ' + str(price)) price = 20 print('price = ' + str(price)) rating = 4.9 name = "karol" is_published = True #Pacient data name = 'John Doe' age = 20 is_new = True #kornelka hehe to ja i mój kot