├── .gitignore ├── age_exception.py ├── app.py ├── app1.py ├── app2.py ├── birth_year.py ├── car.py ├── converters.py ├── course strings.py ├── dice.py ├── digit_names.py ├── ecommerce ├── __init__.py └── shipping.py ├── emoji.py ├── emoji_cobverter.py ├── formatted strings.py ├── inheritance.py ├── is_hot.py ├── iterator_packing.py ├── letter_F.py ├── list_duplicates.py ├── list_methods.py ├── loan_eligibility.py ├── matrix.py ├── max_number.py ├── music.csv ├── person.py ├── point.py ├── price.py ├── random_intro.py ├── secret_number.py ├── string_methods.py ├── temperature.py ├── transactions.xlsx ├── try_dict.py ├── try_package.py ├── use_converters.py ├── use_find_max.py ├── utils.py └── weight_in_kg.py /.gitignore: -------------------------------------------------------------------------------- 1 | Python Tutorial Supplementary Materials.zip 2 | __pycache__ 3 | venv 4 | -------------------------------------------------------------------------------- /age_exception.py: -------------------------------------------------------------------------------- 1 | try: 2 | age = int(input('Age: ')) 3 | income = 20000 4 | risk = income / age 5 | print(age) 6 | except ZeroDivisionError: 7 | print('Age must not be zero') 8 | except ValueError: 9 | print("Invalid value") 10 | 11 | 12 | import sys 13 | for arg in sys.argv[1:]: 14 | try: 15 | f = open(arg, 'r') 16 | except OSError as err: 17 | print('cannot open', arg, err) 18 | else: 19 | print(arg, 'has', len(f.readlines()), 'lines') 20 | f.close() 21 | 22 | try: 23 | raise Exception('spam', 'eggs') 24 | except Exception as inst: 25 | print(type(inst)) # the exception instance 26 | print(inst.args) # arguments stored in .args 27 | print(inst) # __str__ allows args to be printed directly, 28 | # but may be overridden in exception subclasses 29 | x, y = inst.args # unpack args 30 | print('x =', x) 31 | print('y =', y) -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | class Dice: 5 | 6 | def __init__(self, number_of_sides): 7 | try: 8 | if number_of_sides > 3: 9 | self.number_of_sides = number_of_sides 10 | else: 11 | raise ValueError 12 | except ValueError: 13 | print('Dice must have at least 4 sides!') 14 | 15 | # ---------------------------------------------------------------------- 16 | 17 | def roll(self, number_of_rolls): 18 | rolls = [] 19 | for i in range(number_of_rolls): 20 | rolls.append(random.randint(1, self.number_of_sides)) 21 | return tuple(rolls) 22 | 23 | 24 | def main(): 25 | six_sided_dice = Dice(6) 26 | print(six_sided_dice.roll(2)) 27 | 28 | 29 | if __name__ == "__main__": 30 | main() 31 | -------------------------------------------------------------------------------- /app1.py: -------------------------------------------------------------------------------- 1 | 2 | print("hello me") 3 | print('o----') 4 | print(' ||||') 5 | print('*' * 10) -------------------------------------------------------------------------------- /app2.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | name = input('What is your name? ') 4 | color = input('What is your favorite color? ') 5 | 6 | print(f"{name} likes {color}") -------------------------------------------------------------------------------- /birth_year.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | birth_year = input('Birth year? ') 4 | print(type(birth_year)) 5 | age = 2019 - int(birth_year) 6 | print(type(age)) 7 | print(f"Age: {age} ") -------------------------------------------------------------------------------- /car.py: -------------------------------------------------------------------------------- 1 | # car game 2 | 3 | def command_help(): 4 | print("""start - to start the car 5 | stop - to stop the car 6 | quit - to quit""") 7 | 8 | car_started = False 9 | command = "" 10 | while True: 11 | 12 | command = input("> ").lower() 13 | if command == "help": 14 | command_help() 15 | elif command == "start": 16 | if car_started: 17 | print("Car is already started???") 18 | else: 19 | car_started = True 20 | print("Car started") 21 | elif command == "stop": 22 | if not car_started: 23 | print("Car is already stopped???") 24 | else: 25 | car_started = False 26 | print("Car stopped") 27 | elif command == "quit": 28 | break 29 | else: 30 | print("I dom't understand that ...") 31 | 32 | -------------------------------------------------------------------------------- /converters.py: -------------------------------------------------------------------------------- 1 | 2 | def lbs_to_kg(weight): 3 | return weight * 0.45 4 | 5 | def kg_to_lbs(weight): 6 | return weight / 0.45 7 | 8 | def main(): 9 | orig_lb = 20 10 | print(f"Original weight: {orig_lb} pounds") 11 | kg = lbs_to_kg(orig_lb) 12 | print(f"Converted {orig_lb} pounds to {kg} kilograms") 13 | converted_lb = kg_to_lbs(kg) 14 | print(f"Converted {kg} kilograms back to {converted_lb} pounds") 15 | 16 | if __name__ == "__main__": 17 | main() -------------------------------------------------------------------------------- /course strings.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | course = "Python's Course for Beginners" 4 | print(course) 5 | course = 'Python Course for "Beginners"' 6 | print(course) 7 | course = ''' 8 | Hi John, 9 | 10 | Here is our first email to you. 11 | 12 | Thank you, 13 | The support team 14 | 15 | ''' 16 | print(course) 17 | course = 'Python for Beginners' 18 | print(course) 19 | print('course: ' + course[:6]) 20 | 21 | another = course[:] 22 | print('another course:', another) -------------------------------------------------------------------------------- /dice.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | class Dice: 5 | 6 | def __init__(self, number_of_sides = 6): 7 | try: 8 | if number_of_sides > 3: 9 | 10 | self.number_of_sides = number_of_sides 11 | else: 12 | raise ValueError 13 | except ValueError: 14 | 15 | print('Dice must have at least 4 sides!') 16 | 17 | # ---------------------------------------------------------------------- 18 | 19 | def roll(self, number_of_rolls): 20 | rolls = [] 21 | for i in range(number_of_rolls): 22 | 23 | rolls.append(random.randint(1, self.number_of_sides)) 24 | return tuple(rolls) 25 | 26 | 27 | def main(): 28 | 29 | six_sided_dice = Dice() 30 | print(six_sided_dice.roll(2)) 31 | 32 | 33 | if __name__ == "__main__": 34 | 35 | main() 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /digit_names.py: -------------------------------------------------------------------------------- 1 | digit_names = { 2 | '0': "zero", 3 | '1': "one", 4 | '2': "two", 5 | '3': "three", 6 | '4': "four", 7 | '5': "five", 8 | '6': "six", 9 | '7': "seven", 10 | '8': "eight", 11 | '9': "nine", 12 | } 13 | 14 | numbers = input("number? ") 15 | for digit in numbers: 16 | print(digit_names[digit]) -------------------------------------------------------------------------------- /ecommerce/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BevInTX/Python_Tutorial_Beginners_2019/c830170b224af05bcda03fe4019815c56c5e2790/ecommerce/__init__.py -------------------------------------------------------------------------------- /ecommerce/shipping.py: -------------------------------------------------------------------------------- 1 | def calc_shipping(): 2 | 3 | print("calc_shipping") -------------------------------------------------------------------------------- /emoji.py: -------------------------------------------------------------------------------- 1 | words = input(">").split(' ') 2 | print(words) 3 | emojis = { 4 | ":)": "😀", 5 | ":-)": "😄", 6 | ":(": "🙁", 7 | ":-(": "😫" 8 | } 9 | output = "" 10 | for word in words: 11 | output += emojis.get(word, word) 12 | 13 | print(output) 14 | -------------------------------------------------------------------------------- /emoji_cobverter.py: -------------------------------------------------------------------------------- 1 | 2 | def emoji_converter(words): 3 | emojis = { 4 | ":)": "😀", 5 | ":-)": "😄", 6 | ":(": "🙁", 7 | ":-(": "😫" 8 | } 9 | output = "" 10 | for word in words: 11 | output += emojis.get(word, word) 12 | return output 13 | 14 | def main(): 15 | 16 | words = input(">").split(' ') 17 | print(words) 18 | 19 | print(emoji_converter(words)) 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /formatted strings.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | first = 'John' 4 | last = 'Smith' 5 | message = first + ' [' + last + '] is a coder' 6 | msg = f'{first} [{last}] is a coder' 7 | print(message) 8 | print (msg) 9 | -------------------------------------------------------------------------------- /inheritance.py: -------------------------------------------------------------------------------- 1 | class Mammal: 2 | def walk(self): 3 | print("walk") 4 | 5 | 6 | class Dog(Mammal): 7 | def bark(self): 8 | print("bark!") 9 | 10 | 11 | class Cat(Mammal): 12 | def annoy(self): 13 | print("annoy") 14 | 15 | 16 | def main(): 17 | dog = Dog() 18 | dog.walk() 19 | dog.bark() 20 | 21 | cat = Cat() 22 | cat.walk() 23 | cat.annoy() 24 | 25 | 26 | if __name__ == "__main__": 27 | 28 | main() 29 | -------------------------------------------------------------------------------- /is_hot.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | is_hot = False 4 | is_cold = False 5 | 6 | if is_hot: 7 | print("It's a hot day") 8 | print("Drink plenty of water") 9 | elif is_cold: 10 | print("It's a cold day") 11 | print("Wear plenty of clothing") 12 | else: 13 | print("It's a lovely day") 14 | print("Enjoy your day") -------------------------------------------------------------------------------- /iterator_packing.py: -------------------------------------------------------------------------------- 1 | coordinates = (1, 2, 3) 2 | x, y, z = coordinates 3 | print(x, y, z) 4 | 5 | alist = list(range(3, 6)) # normal call with separate arguments 6 | print(alist) 7 | args = [3, 6] 8 | print(list(range(*args))) # call with arguments unpacked from a list 9 | 10 | def parrot(voltage, state='a stiff', action='voom'): 11 | print("-- This parrot wouldn't", action, end=' ') 12 | print("if you put", voltage, "volts through it.", end=' ') 13 | print("E's", state, "!") 14 | 15 | d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"} 16 | parrot(**d) 17 | d = {"state": "bleedin' demised", "action": "VOOM", "voltage": "four million"} 18 | parrot(**d) 19 | 20 | released = { 21 | "iphone" : 2007, 22 | "iphone 3G" : 2008, 23 | "iphone 3GS" : 2009, 24 | "iphone 4" : 2010, 25 | "iphone 4S" : 2011, 26 | "iphone 5" : 2012 27 | } 28 | print(released) 29 | 30 | #the syntax is: mydict[key] = "value" 31 | released["iphone 5S"] = 2013 32 | print (released) 33 | 34 | my_dict = {'a' : 'one', 'b' : 'two'} 35 | print ('a' in my_dict) 36 | print('c' in my_dict) 37 | print('two' in my_dict) 38 | 39 | for item in released: 40 | if "iphone 5" in released: 41 | print ("Key found") 42 | break 43 | else: 44 | print ("No keys found") 45 | 46 | print (released.get("iphone 3G", "none")) 47 | 48 | def put_dashes(n): 49 | print("-" * n) 50 | put_dashes(10) 51 | print("iPhone releases so far:") 52 | put_dashes(10) 53 | for release in released: 54 | print(release) 55 | 56 | for key,val in released.items(): 57 | print (f"{key} => {val}") -------------------------------------------------------------------------------- /letter_F.py: -------------------------------------------------------------------------------- 1 | x_lengths = [5, 2, 5, 2, 2] 2 | 3 | for length in x_lengths: 4 | print('x'*length) 5 | -------------------------------------------------------------------------------- /list_duplicates.py: -------------------------------------------------------------------------------- 1 | alist = ['a', 'b', 'c', 'd', 'a', 'c'] 2 | 3 | # Don't care about order and don't want to allocate another list 4 | for item in alist: 5 | 6 | item_count = alist.count(item) 7 | while item_count > 1: 8 | item_count -= 1 9 | alist.remove(item) 10 | print(f'in place duplicate removal: {alist}') 11 | 12 | alist = [2, 2, 4, 6, 3, 4, 6, 1] 13 | uniques = [] 14 | # care about order and don't mind allocating another list 15 | for item in alist: 16 | 17 | if item not in uniques: 18 | uniques.append(item) 19 | 20 | print(f'duplicate removal via separate list (old one intact``): {uniques}') -------------------------------------------------------------------------------- /list_methods.py: -------------------------------------------------------------------------------- 1 | numbers =[5, 2, 7, 1, 7, 7, 4] 2 | print('count 7: ', numbers.count(7)) 3 | numbers.append(20) 4 | print('append: ', numbers) 5 | numbers.insert(0, 10) 6 | print('insert 10 at index 0: ', numbers) 7 | numbers.remove(7) 8 | print('remove 7: ', numbers) 9 | numbers.pop() 10 | print('pop last number: ', numbers) 11 | print('index of 7: ', numbers.index(7)) 12 | print('index of 99: ', 99 in numbers) 13 | numbers.sort() 14 | print('sort in place: ', numbers) 15 | numbers.reverse() 16 | print('reverse in place: ', numbers) 17 | numbers2 = numbers.copy() 18 | numbers.append(11) 19 | print("numbers2: ", numbers2, numbers) 20 | numbers.clear() 21 | print('clear: ', numbers) 22 | # print(help(numbers)) -------------------------------------------------------------------------------- /loan_eligibility.py: -------------------------------------------------------------------------------- 1 | has_high_income = False 2 | has_good_credit = False 3 | 4 | #if has_high_income and has_good_credit: 5 | if has_high_income or has_good_credit: 6 | print("Eligible for loan") 7 | 8 | has_criminal_record = True 9 | 10 | if has_good_credit and not has_criminal_record: 11 | print("Eligible for loan") -------------------------------------------------------------------------------- /matrix.py: -------------------------------------------------------------------------------- 1 | matrix_3_3 = [ 2 | [1, 2, 3], 3 | [4, 5, 6], 4 | [7, 8, 9] 5 | ] 6 | matrix_3_3[0][1] = 20 7 | print (matrix_3_3) 8 | print (matrix_3_3[0][1]) 9 | 10 | for row in matrix_3_3: 11 | for item in row: 12 | print(item) -------------------------------------------------------------------------------- /max_number.py: -------------------------------------------------------------------------------- 1 | numbers = [10, 2, 3, 455, 5, 6] 2 | 3 | max = numbers[0] 4 | for number in numbers: 5 | if number > max: 6 | max = number 7 | 8 | print(max) 9 | 10 | 11 | -------------------------------------------------------------------------------- /music.csv: -------------------------------------------------------------------------------- 1 | age,gender,genre 2 | 20,1,HipHop 3 | 23,1,HipHop 4 | 25,1,HipHop 5 | 26,1,Jazz 6 | 29,1,Jazz 7 | 30,1,Jazz 8 | 31,1,Classical 9 | 33,1,Classical 10 | 37,1,Classical 11 | 20,0,Dance 12 | 21,0,Dance 13 | 25,0,Dance 14 | 26,0,Acoustic 15 | 27,0,Acoustic 16 | 30,0,Acoustic 17 | 31,0,Classical 18 | 34,0,Classical 19 | 35,0,Classical -------------------------------------------------------------------------------- /person.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | 3 | def __init__(self, name): 4 | self.name = name 5 | 6 | def talk(self): 7 | print(f"hello, I am {self.name}") 8 | 9 | def main(): 10 | 11 | bev = Person("Bev") 12 | bev.talk() 13 | 14 | if __name__ == "__main__": 15 | 16 | main() -------------------------------------------------------------------------------- /point.py: -------------------------------------------------------------------------------- 1 | ######################################################################## 2 | class Point: 3 | """""" 4 | 5 | #---------------------------------------------------------------------- 6 | def __init__(self, x, y): 7 | self.x = x 8 | self.y = y 9 | def move(self): 10 | print("move") 11 | def draw(self): 12 | print("draw") 13 | 14 | def main(): 15 | point1 = Point(10, 20) 16 | print(point1.x, point1.y) 17 | point1.draw() 18 | point1.move() 19 | 20 | if __name__ == "__main__": 21 | 22 | main() 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /price.py: -------------------------------------------------------------------------------- 1 | price = 1_000_000 2 | 3 | has_good_credit = True 4 | 5 | if has_good_credit: 6 | down_payment = 0.1 * price 7 | else: 8 | down_payment = 0.2 * price 9 | print(f"Down payment: ${down_payment}") -------------------------------------------------------------------------------- /random_intro.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | for i in range(3): 4 | print(random.random()) 5 | 6 | print(random.randint(10, 20)) 7 | 8 | members = ['John', 'Mary', 'Bob', 'Mosh'] 9 | leader = random.choice(members) 10 | print(f'leader: {leader}') 11 | 12 | -------------------------------------------------------------------------------- /secret_number.py: -------------------------------------------------------------------------------- 1 | secret_number = 9 2 | 3 | guess_count = 0 4 | guess_limit = 3 5 | 6 | while guess_count < guess_limit: 7 | guess = int(input('Guess: ')) 8 | guess_count += 1 9 | if guess == secret_number: 10 | print("You won!") 11 | 12 | break 13 | else: 14 | print("You lost :-(") -------------------------------------------------------------------------------- /string_methods.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | course = 'Python for Beginners' 5 | 6 | print(len(course)) 7 | 8 | print(course.upper()) 9 | 10 | print(course.lower()) 11 | print(course) 12 | 13 | print(course.find('Beginners')) 14 | print(course.replace('Beginners', 'Absolute Beginners')) 15 | 16 | print('Python' in course) 17 | print('python' in course) -------------------------------------------------------------------------------- /temperature.py: -------------------------------------------------------------------------------- 1 | temperature = 30 2 | 3 | if temperature > 30: 4 | print("it's a hot day") 5 | elif temperature < 10: 6 | print("It's a cold day") 7 | else: 8 | print("it's neither hot nor cold") -------------------------------------------------------------------------------- /transactions.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BevInTX/Python_Tutorial_Beginners_2019/c830170b224af05bcda03fe4019815c56c5e2790/transactions.xlsx -------------------------------------------------------------------------------- /try_dict.py: -------------------------------------------------------------------------------- 1 | 2 | released = { 3 | "iphone": 2007, 4 | "iphone 3G": 2008, 5 | "iphone 3GS": 2009, 6 | "iphone 4": 2010, 7 | "iphone 4S": 2011, 8 | "iphone 5": 2012 9 | } 10 | print(released) 11 | 12 | # the syntax is: mydict[key] = "value" 13 | released["iphone 5S"] = 2013 14 | print (released) 15 | 16 | my_dict = {'a': 'one', 'b': 'two'} 17 | print ('a' in my_dict) 18 | print('c' in my_dict) 19 | print('two' in my_dict) 20 | 21 | for item in released: 22 | if "iphone 5" in released: 23 | print ("Key found") 24 | break 25 | else: 26 | print ("No keys found") 27 | 28 | print (released.get("iphone 3G", "none")) 29 | 30 | 31 | def put_dashes(n): 32 | print("-" * n) 33 | 34 | 35 | put_dashes(10) 36 | print("iPhone releases so far:") 37 | put_dashes(10) 38 | for release in released: 39 | print(release) 40 | 41 | for key, val in released.items(): 42 | print (f"{key} => {val}") 43 | 44 | customer = { 45 | "name": "John Smith", 46 | "age": 30, 47 | "age": 40, 48 | "is_verified": True 49 | } 50 | for key, val in customer.items(): 51 | print(f"{key}: {val}") 52 | -------------------------------------------------------------------------------- /try_package.py: -------------------------------------------------------------------------------- 1 | #import ecommerce.shipping 2 | #ecommerce.shipping.calc_shipping() 3 | from ecommerce import shipping 4 | shipping.calc_shipping() 5 | -------------------------------------------------------------------------------- /use_converters.py: -------------------------------------------------------------------------------- 1 | import converters 2 | from converters import lbs_to_kg 3 | 4 | print(converters.kg_to_lbs(70)) 5 | print(lbs_to_kg(44)) 6 | -------------------------------------------------------------------------------- /use_find_max.py: -------------------------------------------------------------------------------- 1 | import utils 2 | 3 | list_max = utils.find_max([200, 17, 1, 11, 300, 400, 1]) 4 | print(list_max) 5 | 6 | try_alpha = utils.find_max(['C', 'T', 'a', 'r', 't', 'R']) 7 | try_alpha2 = utils.find_max(['C', 'T', 'R']) 8 | try_alpha3 = utils.find_max(["Zebra", "Cat", "Dog", "Elephant", "Anteater"]) 9 | print(try_alpha3) -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | # Variety of utilities 2 | 3 | def find_max(a_list): 4 | 5 | max = a_list[0] 6 | for item in a_list: 7 | if item > max: 8 | max = item 9 | 10 | return max 11 | 12 | def main(): 13 | 14 | print(find_max((100, 1, 2, 3, 11, 14))) 15 | 16 | if __name__ == "__main__": 17 | main() 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /weight_in_kg.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | pounds = input('Weight in pounts? ') 4 | kg = int(pounds) / 2.204 5 | 6 | print(f"Weight in Kg: {kg} ") --------------------------------------------------------------------------------