├── .gitignore ├── 1-hello-world ├── 02_hello_world.py ├── 03_pattern.py ├── 04_initials.py ├── 05_letter.py └── hello_world.mov ├── 2-variables ├── 07_temperature.py ├── 08_bmi.py ├── 09_hypotenuse.py ├── 09_quadratic.py └── 10_currency.py ├── 3-control-flow ├── 11_coin_flip.py ├── 12_grades.py ├── 13_ph_levels.py ├── 14_magic_8_ball.py ├── 15_the_cyclone_1.py ├── 15_the_cyclone_2.py ├── 16_sorting_hat_1.py └── 16_sorting_hat_2.py ├── 4-loops ├── 17_enter_pin.py ├── 18_guess_number.py ├── 19_detention.py ├── 20_99_bottles.py └── 21_fizz_buzz.py ├── 5-lists ├── 22_grocery.py ├── 23_todo.py ├── 24_inventory.py ├── 25_reading_list.py ├── 26_mixtape.py └── 27_bucket_list.py ├── 6-functions ├── 28_dry.py ├── 29_fortune_cookie_1.py ├── 29_fortune_cookie_2.py ├── 30_rocket.py ├── 31_calculator.py ├── 32_stonks.py └── 33_drive_thru.py ├── 7-classes-objects ├── 34_restaurants.py ├── 35_bobs_burgers.py ├── 36_favorite_cities.py ├── 37_bank_accounts.py ├── 38_pokedex_1.py └── 38_pokedex_2.py ├── 8-modules ├── 39_slot_machine_1.py ├── 39_slot_machine_2.py ├── 40_solar_system.py ├── 41_bday_messages.py ├── 41_main.py ├── 42_forty_two.py └── 43_zen.py ├── LICENSE ├── README.md ├── assets ├── badge-6-functions.png ├── badge_classes_and_objects.png ├── badge_earth.png ├── badge_equal.png ├── badge_fork.png ├── badge_lists.png ├── badge_loop.png └── badge_modules.png └── projects.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # Other files and folders 4 | .settings/ 5 | 6 | # Executables 7 | *.swf 8 | *.air 9 | *.ipa 10 | *.apk 11 | -------------------------------------------------------------------------------- /1-hello-world/02_hello_world.py: -------------------------------------------------------------------------------- 1 | print('Hello World!') 2 | -------------------------------------------------------------------------------- /1-hello-world/03_pattern.py: -------------------------------------------------------------------------------- 1 | print(' 1') 2 | print(' 2 3') 3 | print(' 4 5 6') 4 | print('7 8 9 10') 5 | -------------------------------------------------------------------------------- /1-hello-world/04_initials.py: -------------------------------------------------------------------------------- 1 | # Fun fact: My high school band Attica was signed to an indie record label. 2 | 3 | print(' SSS L ') 4 | print('S S L ') 5 | print('S L ') 6 | print(' SSS L ') 7 | print(' S L ') 8 | print('S S L ') 9 | print(' SSS LLLLL') 10 | -------------------------------------------------------------------------------- /1-hello-world/05_letter.py: -------------------------------------------------------------------------------- 1 | # Snail Mail 💌 2 | # Codédex 3 | 4 | print('+---------------------------------------------------------------------+') 5 | print('| June 2022 |') 6 | print('| Brooklyn, NY |') 7 | print('| Dear Self, |') 8 | print('| |') 9 | print('| Build the learn to code platform that you always dreamed of. |') 10 | print('| Give more than you take. |') 11 | print('| Care > capital. |') 12 | print('| Five-second funerals for all the Ls. |') 13 | print('| And always get back up. |') 14 | print('| |') 15 | print('| Sonny Li 🤠 |') 16 | print('| |') 17 | print('+---------------------------------------------------------------------+') 18 | -------------------------------------------------------------------------------- /1-hello-world/hello_world.mov: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codedex-io/python-101/c0ef0d4921767b0311be25cb8cc4c85eb2c2e054/1-hello-world/hello_world.mov -------------------------------------------------------------------------------- /2-variables/07_temperature.py: -------------------------------------------------------------------------------- 1 | # Temperature 🌡 2 | # Codédex 3 | 4 | temp_f = 56 5 | temp_c = (temp_f - 32) / 1.8 6 | 7 | print(temp_c) -------------------------------------------------------------------------------- /2-variables/08_bmi.py: -------------------------------------------------------------------------------- 1 | # BMI 🏋️♀️ 2 | # Codédex 3 | 4 | weight = 92.3 5 | height = 1.86 6 | 7 | bmi = weight / (height**2) 8 | 9 | print(bmi) -------------------------------------------------------------------------------- /2-variables/09_hypotenuse.py: -------------------------------------------------------------------------------- 1 | # Pythagorean Theorem 📐 2 | # Codédex 3 | 4 | a = int(input("Enter a: ")) 5 | b = int(input("Enter b: ")) 6 | 7 | c = (a**2 + b**2) ** 0.5 8 | 9 | print(c) 10 | -------------------------------------------------------------------------------- /2-variables/09_quadratic.py: -------------------------------------------------------------------------------- 1 | # Quadratic Formula 🧮 2 | # Codédex 3 | 4 | a = int(input("Enter a: ")) 5 | b = int(input("Enter b: ")) 6 | c = int(input("Enter c: ")) 7 | 8 | root1 = (-b + (b*b - 4*a*c)**0.5) / (2*a) 9 | root2 = (-b - (b*b - 4*a*c)**0.5) / (2*a) 10 | 11 | print(root1) 12 | print(root2) 13 | -------------------------------------------------------------------------------- /2-variables/10_currency.py: -------------------------------------------------------------------------------- 1 | # Currency 💵 2 | # Codédex 3 | 4 | pesos = int(input('What do you have left in pesos? ')) 5 | soles = int(input('What do you have left in soles? ')) 6 | reais = int(input('What do you have left in reais? ')) 7 | 8 | total = pesos * 0.00025 + soles * 0.28 + reais * 0.21 9 | 10 | print(total) 11 | -------------------------------------------------------------------------------- /3-control-flow/11_coin_flip.py: -------------------------------------------------------------------------------- 1 | # Coin Flip 🪙 2 | # Codédex 3 | 4 | import random 5 | 6 | num = random.randint(0, 1) 7 | 8 | if num > 0.5: 9 | print('Heads') 10 | else: 11 | print('Tails') 12 | 13 | -------------------------------------------------------------------------------- /3-control-flow/12_grades.py: -------------------------------------------------------------------------------- 1 | # Grades 💯 2 | # Codédex 3 | 4 | grade = 58 5 | 6 | if grade >= 55: 7 | print('You passed.') 8 | else: 9 | print('You failed.') 10 | -------------------------------------------------------------------------------- /3-control-flow/13_ph_levels.py: -------------------------------------------------------------------------------- 1 | # pH Levels 🧪 2 | # Codédex 3 | 4 | ph = int(input('Enter a pH level (0-14): ')) 5 | 6 | if ph > 7: 7 | print('Basic') 8 | elif ph < 7: 9 | print('Acidic') 10 | else: 11 | print('Neutral') 12 | -------------------------------------------------------------------------------- /3-control-flow/14_magic_8_ball.py: -------------------------------------------------------------------------------- 1 | # Magic 8 Ball 🎱 2 | # Codédex 3 | 4 | import random 5 | 6 | question = input('Question: ') 7 | 8 | random_number = random.randint(1, 9) 9 | 10 | if random_number == 1: 11 | answer = 'Yes - definitely' 12 | elif random_number == 2: 13 | answer = 'It is decidedly so' 14 | elif random_number == 3: 15 | answer = 'Without a doubt' 16 | elif random_number == 4: 17 | answer = 'Reply hazy, try again' 18 | elif random_number == 5: 19 | answer = 'Ask again later' 20 | elif random_number == 6: 21 | answer = 'Better not tell you now' 22 | elif random_number == 7: 23 | answer = 'My sources say no' 24 | elif random_number == 8: 25 | answer = 'Outlook not so good' 26 | elif random_number == 9: 27 | answer = 'Very doubtful' 28 | else: 29 | answer = 'Error' 30 | 31 | print('Magic 8 Ball: ' + answer) 32 | -------------------------------------------------------------------------------- /3-control-flow/15_the_cyclone_1.py: -------------------------------------------------------------------------------- 1 | # The Cyclone 🎢 2 | # Codédex 3 | 4 | height = int(input('What is your height (cm)? ')) 5 | credits = int(input('How many credits do you have? ')) 6 | 7 | if height >= 137 and credits >= 10: 8 | print("Enjoy the ride!") 9 | elif height < 137 and credits >= 10: 10 | print("You are not tall enough to ride.") 11 | elif credits < 10 and height >= 137: 12 | print("You don't have enough credits to ride.") 13 | else: 14 | print("You are not tall enough for this ride, nor do you have enough credits.") 15 | -------------------------------------------------------------------------------- /3-control-flow/15_the_cyclone_2.py: -------------------------------------------------------------------------------- 1 | # The Cyclone 🎢 2 | # Codédex 3 | 4 | ride_is_open = True 5 | 6 | height = int(input('What is your height (cm)? ')) 7 | credits = int(input('How many credits do you have? ')) 8 | 9 | tall_enough = height >= 137 10 | enough_credits = credits >= 10 11 | 12 | if ride_is_open and tall_enough and enough_credits: 13 | print("Enjoy the ride!") 14 | elif not tall_enough or not enough_credits: 15 | print("You are either not tall enough to ride or you don't have enough credits.") 16 | else: 17 | print("Sorry! The ride is currently closed!") 18 | -------------------------------------------------------------------------------- /3-control-flow/16_sorting_hat_1.py: -------------------------------------------------------------------------------- 1 | # Sorting Hat 🧙♂️ 2 | # Codédex 3 | 4 | gryffindor = 0 5 | hufflepuff = 0 6 | ravenclaw = 0 7 | slytherin = 0 8 | 9 | print('===============') 10 | print('The Sorting Hat') 11 | print('===============') 12 | 13 | # ~~~~~~~~~~~~~~~ Question 1 ~~~~~~~~~~~~~~~ 14 | 15 | print('Q1) Do you like Dawn or Dusk?') 16 | 17 | print(' 1) Dawn') 18 | print(' 2) Dusk') 19 | 20 | answer = int(input('Enter answer (1-2): ')) 21 | 22 | if answer == 1: 23 | gryffindor = gryffindor + 1 24 | ravenclaw = ravenclaw + 1 25 | elif answer == 2: 26 | hufflepuff = hufflepuff + 1 27 | slytherin = slytherin + 1 28 | else: 29 | print('Wrong input.') 30 | 31 | # ~~~~~~~~~~~~~~~ Question 2 ~~~~~~~~~~~~~~~ 32 | 33 | print("\nQ2) When I'm dead, I want people to remember me as:") 34 | 35 | print(' 1) The Good') 36 | print(' 2) The Great') 37 | print(' 3) The Wise') 38 | print(' 4) The Bold') 39 | 40 | answer = int(input('Enter your answer (1-4): ')) 41 | 42 | if answer == 1: 43 | hufflepuff = hufflepuff + 2 44 | elif answer == 2: 45 | slytherin = slytherin + 2 46 | elif answer == 3: 47 | ravenclaw = ravenclaw + 2 48 | elif answer == 4: 49 | gryffindor = gryffindor + 2 50 | else: 51 | print('Wrong input.') 52 | 53 | # ~~~~~~~~~~~~~~~ Question 3 ~~~~~~~~~~~~~~~ 54 | 55 | print('\nQ3) Which kind of instrument most pleases your ear?') 56 | 57 | print(' 1) The violin') 58 | print(' 2) The trumpet') 59 | print(' 3) The piano') 60 | print(' 4) The drum') 61 | 62 | answer = int(input('Enter your answer (1-4): ')) 63 | 64 | if answer == 1: 65 | slytherin = slytherin + 4 66 | elif answer == 2: 67 | hufflepuff = hufflepuff + 4 68 | elif answer == 3: 69 | ravenclaw = ravenclaw + 4 70 | elif answer == 4: 71 | gryffindor = gryffindor + 4 72 | else: 73 | print('Wrong input.') 74 | 75 | print("Gryffindor: ", gryffindor) 76 | print("Ravenclaw: ", ravenclaw) 77 | print("Hufflepuff: ", hufflepuff) 78 | print("Slytherin: ", slytherin) 79 | 80 | # Bonus Part 81 | 82 | if gryffindor >= ravenclaw and gryffindor >= hufflepuff and gryffindor >= slytherin: 83 | print('🦁 Gryffindor!') 84 | elif ravenclaw >= hufflepuff and ravenclaw >= slytherin: 85 | print('🦅 Ravenclaw!') 86 | elif hufflepuff >= slytherin: 87 | print('🦡 Hufflepuff!') 88 | else: 89 | print('🐍 Slytherin!') 90 | -------------------------------------------------------------------------------- /3-control-flow/16_sorting_hat_2.py: -------------------------------------------------------------------------------- 1 | # Sorting Hat 🧙♂️ 2 | # Codédex 3 | 4 | gryffindor = 0 5 | hufflepuff = 0 6 | ravenclaw = 0 7 | slytherin = 0 8 | 9 | print('===============') 10 | print('The Sorting Hat') 11 | print('===============') 12 | 13 | # ~~~~~~~~~~~~~~~ Question 1 ~~~~~~~~~~~~~~~ 14 | 15 | print('Q1) Do you like Dawn or Dusk?') 16 | 17 | print(' 1) Dawn') 18 | print(' 2) Dusk') 19 | 20 | answer = int(input('Enter answer (1-2): ')) 21 | 22 | if answer == 1: 23 | gryffindor += 1 24 | ravenclaw += 1 25 | elif answer == 2: 26 | hufflepuff += 1 27 | slytherin +=1 28 | else: 29 | print('Wrong input.') 30 | 31 | # ~~~~~~~~~~~~~~~ Question 2 ~~~~~~~~~~~~~~~ 32 | 33 | print("\nQ2) When I'm dead, I want people to remember me as:") 34 | 35 | print(' 1) The Good') 36 | print(' 2) The Great') 37 | print(' 3) The Wise') 38 | print(' 4) The Bold') 39 | 40 | answer = int(input('Enter your answer (1-4): ')) 41 | 42 | if answer == 1: 43 | hufflepuff += 2 44 | elif answer == 2: 45 | slytherin += 2 46 | elif answer == 3: 47 | ravenclaw += 2 48 | elif answer == 4: 49 | gryffindor += 2 50 | else: 51 | print('Wrong input.') 52 | 53 | # ~~~~~~~~~~~~~~~ Question 3 ~~~~~~~~~~~~~~~ 54 | 55 | print('\nQ3) Which kind of instrument most pleases your ear?') 56 | 57 | print(' 1) The violin') 58 | print(' 2) The trumpet') 59 | print(' 3) The piano') 60 | print(' 4) The drum') 61 | 62 | answer = int(input('Enter your answer (1-4): ')) 63 | 64 | if answer == 1: 65 | slytherin += 4 66 | elif answer == 2: 67 | hufflepuff += 4 68 | elif answer == 3: 69 | ravenclaw +=4 70 | elif answer == 4: 71 | gryffindor += 4 72 | else: 73 | print('Wrong input.') 74 | 75 | print("Gryffindor: ", gryffindor) 76 | print("Ravenclaw: ", ravenclaw) 77 | print("Hufflepuff: ", hufflepuff) 78 | print("Slytherin: ", slytherin) 79 | 80 | # Bonus Part 81 | 82 | most_points = max(gryffindor, ravenclaw, hufflepuff, slytherin) # We'll learn about the max() function in Chapter 6 83 | 84 | if gryffindor == most_points: 85 | print('🦁 Gryffindor!') 86 | elif ravenclaw == most_points: 87 | print('🦅 Ravenclaw!') 88 | elif hufflepuff == most_points: 89 | print('🦡 Hufflepuff!') 90 | else: 91 | print('🐍 Slytherin!') 92 | -------------------------------------------------------------------------------- /4-loops/17_enter_pin.py: -------------------------------------------------------------------------------- 1 | # Enter PIN 🏦 2 | # Codédex 3 | 4 | print('=== BANK OF CODéDEX ===') 5 | 6 | pin = int(input('Enter your PIN: ')) 7 | 8 | while pin != 1234: 9 | pin = int(input('Incorrect PIN. Enter your PIN again: ')) 10 | 11 | if pin == 1234: 12 | print('PIN accepted!') 13 | -------------------------------------------------------------------------------- /4-loops/18_guess_number.py: -------------------------------------------------------------------------------- 1 | # Guess Number 🔢 2 | # Codédex 3 | 4 | guess = 0 5 | tries = 0 6 | 7 | while guess != 6 and tries < 5: 8 | guess = int(input('Guess the number: ')) 9 | tries = tries + 1 10 | 11 | if guess != 6: 12 | print('You ran out of tries.') 13 | else: 14 | print('You got it!') 15 | -------------------------------------------------------------------------------- /4-loops/19_detention.py: -------------------------------------------------------------------------------- 1 | # Detention 🧑🏫 2 | # Codédex 3 | 4 | for x in range(100): 5 | print('I will not throw airplanes in class') 6 | -------------------------------------------------------------------------------- /4-loops/20_99_bottles.py: -------------------------------------------------------------------------------- 1 | # 99 Bottles of Beer 🍻 2 | # Codédex 3 | 4 | for i in range(99, 0, -1): 5 | print(f'{i} bottles of beer on the wall') 6 | print(f'{i} bottles of beer') 7 | print('Take one down, pass it around') 8 | print(f'{i-1} bottles of beer on the wall') 9 | -------------------------------------------------------------------------------- /4-loops/21_fizz_buzz.py: -------------------------------------------------------------------------------- 1 | # Fizz Buzz 🐝 2 | # Codédex 3 | 4 | for num in range(1, 101): 5 | if num % 3 == 0 and num % 5 == 0: 6 | print('FizzBuzz') 7 | elif num % 3 == 0: 8 | print('Fizz') 9 | elif num % 5 == 0: 10 | print('Buzz') 11 | else: 12 | print(num) 13 | -------------------------------------------------------------------------------- /5-lists/22_grocery.py: -------------------------------------------------------------------------------- 1 | # Grocery List 🛒 2 | # Codédex 3 | 4 | grocery = ['🥚 Eggs', 5 | '🥑 Avocados', 6 | '🍪 Cookies', 7 | '🌶 Hot Pepper Jam', 8 | '🫐 Blueberries', 9 | '🥦 Broccoli'] 10 | 11 | print(grocery) 12 | -------------------------------------------------------------------------------- /5-lists/23_todo.py: -------------------------------------------------------------------------------- 1 | # To-Do List ✅ 2 | # Codédex 3 | 4 | todo = ['🏦 Get quarters.', 5 | '🧺 Do laundry.', 6 | '🌳 Take a walk.', 7 | '💈 Get a haircut.', 8 | '🍵 Make some tea.', 9 | '💻 Complete Lists chapter.', 10 | '💖 Call mom.', 11 | '📺 Watch My Hero Academia.'] 12 | 13 | print(todo[0]) 14 | print(todo[1]) 15 | print(todo[2 : 5]) 16 | print(todo[9]) # IndexError 17 | -------------------------------------------------------------------------------- /5-lists/24_inventory.py: -------------------------------------------------------------------------------- 1 | # Inventory 📦 2 | # Codédex 3 | 4 | lego_parts = [8980, 7323, 5343, 82700, 92232, 1203, 7319, 8903, 2328, 1279, 679, 589] 5 | 6 | print(min(lego_parts)) 7 | print(max(lego_parts)) 8 | -------------------------------------------------------------------------------- /5-lists/25_reading_list.py: -------------------------------------------------------------------------------- 1 | # Reading List 📚 2 | # Codédex 3 | 4 | books = ['Harry Potter', 5 | '1984', 6 | 'The Fault in Our Stars', 7 | 'The Mom Test', 8 | 'Life in Code'] 9 | 10 | print(books) 11 | 12 | books.append('Pachinko') 13 | books.remove('The Fault in Our Stars') 14 | books.pop(1) 15 | 16 | print(books) 17 | -------------------------------------------------------------------------------- /5-lists/26_mixtape.py: -------------------------------------------------------------------------------- 1 | # Mixtape: Classical Relaxation Theme 💿 2 | # Codédex 3 | 4 | playlist = ['Johannes Brahms - Symphony No.1 in C Minor, Op. 68', 5 | 'Erik Satie - Gymnopédie No.1', 6 | 'Tchaikovsky - 1812 Overture', 7 | 'Edvard Grieg - In the Hall of the Mountain King', 8 | 'Ludwig van Beethoven - Moonlight Sonata', 9 | 'Antonio Vivaldi - The Four Seasons'] 10 | 11 | for song in playlist: 12 | print(song) 13 | -------------------------------------------------------------------------------- /5-lists/27_bucket_list.py: -------------------------------------------------------------------------------- 1 | # Bucket List 🪣 2 | # Codédex 3 | 4 | things_to_do = [ 5 | '🚀 Build a meaningful product for everyone.', 6 | '⛰ Try out hiking and mountain biking.', 7 | '🌏 Visit at least 10 countries in my lifetime.', 8 | '🎸 Produce an original song.', 9 | '📝 Write a short story.', 10 | '🏃 Finish a 10k marathon.' 11 | ] 12 | 13 | for thing in things_to_do: 14 | print(thing) 15 | -------------------------------------------------------------------------------- /6-functions/28_dry.py: -------------------------------------------------------------------------------- 1 | # D.R.Y. 🧩 2 | # Codédex 3 | 4 | import random 5 | 6 | list_of_foods = ['celery', 'broccoli', 'cabbage'] 7 | 8 | # This was the first function introduced in the course 9 | print('Hello, World!') 10 | 11 | # This function calculates the maximum of two numbers a and b 12 | print(max(3, 5)) 13 | 14 | # This function calculates the minimum of two numbers a and b 15 | print(min(-1, 7)) 16 | 17 | # This function calculates the length of a list 18 | print(len(list_of_foods)) 19 | 20 | # This function calculates a to the power b 21 | print(pow(2, 6)) 22 | -------------------------------------------------------------------------------- /6-functions/29_fortune_cookie_1.py: -------------------------------------------------------------------------------- 1 | # Fortune Cookie 🥠 2 | # Codédex 3 | 4 | import random 5 | 6 | options = [ 7 | 'Don’t pursue happiness – create it.', 8 | 'All things are difficult before they are easy.', 9 | 'The early bird gets the worm, but the second mouse gets the cheese.', 10 | 'If you eat something and nobody sees you eat it, it has no calories.', 11 | 'Someone in your life needs a letter from you.', 12 | 'Don’t just think. Act!', 13 | 'Your heart will skip a beat.', 14 | 'The fortune you search for is in another cookie.', 15 | 'Help! I’m being held prisoner in a Chinese bakery!' 16 | ] 17 | 18 | def fortune(): 19 | random_fortune = random.randint(0, len(options) - 1) 20 | print(options[random_fortune]) 21 | 22 | fortune() 23 | fortune() 24 | fortune() -------------------------------------------------------------------------------- /6-functions/29_fortune_cookie_2.py: -------------------------------------------------------------------------------- 1 | # Fortune Cookie 🥠 2 | # Codédex 3 | 4 | import random 5 | 6 | options = [ 7 | 'Don’t pursue happiness – create it.', 8 | 'All things are difficult before they are easy.', 9 | 'The early bird gets the worm, but the second mouse gets the cheese.', 10 | 'If you eat something and nobody sees you eat it, it has no calories.', 11 | 'Someone in your life needs a letter from you.', 12 | 'Don’t just think. Act!', 13 | 'Your heart will skip a beat.', 14 | 'The fortune you search for is in another cookie.', 15 | 'Help! I’m being held prisoner in a Chinese bakery!' 16 | ] 17 | 18 | def fortune(): 19 | random_fortune = random.randint(0, len(options) - 1) 20 | 21 | if random_fortune == 0: 22 | option = options[0] 23 | elif random_fortune == 1: 24 | option = options[1] 25 | elif random_fortune == 2: 26 | option = options[2] 27 | elif random_fortune == 3: 28 | option = options[3] 29 | elif random_fortune == 4: 30 | option = options[4] 31 | elif random_fortune == 5: 32 | option = options[5] 33 | elif random_fortune == 6: 34 | option = options[6] 35 | elif random_fortune == 7: 36 | option = options[7] 37 | elif random_fortune == 8: 38 | option = options[8] 39 | else: 40 | option = 'Error' 41 | 42 | print(option) 43 | 44 | fortune() 45 | fortune() 46 | fortune() 47 | -------------------------------------------------------------------------------- /6-functions/30_rocket.py: -------------------------------------------------------------------------------- 1 | # Mars Orbiter 🚀 2 | # Codédex 3 | 4 | def distance_to_miles(distance): 5 | miles = distance / 1.609 6 | print(miles) 7 | 8 | distance_to_miles(10000) 9 | -------------------------------------------------------------------------------- /6-functions/31_calculator.py: -------------------------------------------------------------------------------- 1 | # Calculator 🔢 2 | # Codédex 3 | 4 | def add(a, b): 5 | return a + b 6 | 7 | def subtract(a, b): 8 | return a - b 9 | 10 | def multiply(a, b): 11 | return a * b 12 | 13 | def divide(a, b): 14 | return a / b 15 | 16 | def exp(a, b): 17 | return a ** b 18 | 19 | print(add(3, 5)) 20 | print(subtract(7, 2)) 21 | print(multiply(4, 8)) 22 | print(divide(9, 3)) 23 | print(exp(2, 3)) 24 | -------------------------------------------------------------------------------- /6-functions/32_stonks.py: -------------------------------------------------------------------------------- 1 | # Stonks 📈 2 | # Codédex 3 | 4 | stock_prices = [34.68, 36.09, 34.94, 33.97, 34.68, 35.82, 43.41, 44.29, 44.65, 53.56, 49.85, 48.71, 48.71, 49.94, 48.53, 47.03, 46.59, 48.62, 44.21, 47.21] 5 | 6 | def price_at(i): 7 | return stock_prices[i-1] 8 | 9 | def max_price(a, b): 10 | mx = 0 11 | for i in range(a, b + 1): 12 | mx = max(mx, price_at(i)) 13 | return mx 14 | 15 | def min_price(a, b): 16 | mn = price_at(a) 17 | for i in range(a, b + 1): 18 | mn = min(mn, price_at(i)) 19 | return mn 20 | 21 | print(max_price(1, 15)) 22 | print(min_price(5, 10)) 23 | print(price_at(3)) 24 | -------------------------------------------------------------------------------- /6-functions/33_drive_thru.py: -------------------------------------------------------------------------------- 1 | # Drive-Thru 🚙 2 | # Codédex 3 | 4 | def get_item(x): 5 | if x == 1: 6 | return '🍔 Cheeseburger' 7 | elif x == 2: 8 | return '🍟 Fries' 9 | elif x == 3: 10 | return '🥤 Soda' 11 | elif x == 4: 12 | return '🍦 Ice Cream' 13 | elif x == 5: 14 | return '🍪 Cookie' 15 | else: 16 | return "invalid option" 17 | 18 | def welcome(): 19 | print('Welcome to Sonnyboy\'s Diner!') 20 | print('Here\'s the menu:') 21 | print('1. 🍔 Cheeseburger') 22 | print('2. 🍟 Fries') 23 | print('3. 🥤 Soda') 24 | print('4. 🍦 Ice Cream') 25 | print('5. 🍪 Cookie') 26 | 27 | welcome() 28 | 29 | option = int(input('What would you like to order? ')) 30 | print(get_item(option)) 31 | -------------------------------------------------------------------------------- /7-classes-objects/34_restaurants.py: -------------------------------------------------------------------------------- 1 | # Restaurants 🍽️ 2 | # Codédex 3 | 4 | class Restaurant: 5 | name = '' 6 | category = '' 7 | rating = 0.0 8 | delivery = True 9 | -------------------------------------------------------------------------------- /7-classes-objects/35_bobs_burgers.py: -------------------------------------------------------------------------------- 1 | # Bob's Burgers 🍔 2 | # Codédex 3 | 4 | class Restaurant: 5 | name = '' 6 | type = '' 7 | rating = 0.0 8 | delivery = False 9 | 10 | bobs_burgers = Restaurant() 11 | bobs_burgers.name = 'Bob\'s Burgers' 12 | bobs_burgers.type = 'American Diner' 13 | bobs_burgers.rating = 4.2 14 | bobs_burgers.delivery = False 15 | 16 | katz_deli = Restaurant() 17 | katz_deli.name = 'Katz\'s Deli' 18 | katz_deli.type = 'Kosher Deli' 19 | katz_deli.rating = 4.5 20 | katz_deli.delivery = True 21 | 22 | baekjeong = Restaurant() 23 | baekjeong.name = 'Baekjeong NYC' 24 | baekjeong.type = 'Korean BBQ' 25 | baekjeong.rating = 4.4 26 | baekjeong.delivery = False 27 | 28 | print(vars(bobs_burgers)) 29 | print(vars(katz_deli)) 30 | print(vars(baekjeong)) -------------------------------------------------------------------------------- /7-classes-objects/36_favorite_cities.py: -------------------------------------------------------------------------------- 1 | # Favorite Cities 🏙️ 2 | # Codédex 3 | 4 | class City: 5 | def __init__(self, name, country, population, landmarks): 6 | self.name = name 7 | self.country = country 8 | self.population = population 9 | self.landmarks = landmarks 10 | 11 | 12 | nyc = City("New York City", "USA", 8468000, ["Statue of Liberty", "Brooklyn Bridge", "Apollo Theatre"]) 13 | 14 | shanghai = City("Shanghai", "China", 26320000, ["The Bund", "Jin Mao Tower", "Tianzifang"]) 15 | 16 | print(vars(nyc)) 17 | print(vars(shanghai)) 18 | -------------------------------------------------------------------------------- /7-classes-objects/37_bank_accounts.py: -------------------------------------------------------------------------------- 1 | # Bank Accounts 🏦 2 | # Codédex 3 | 4 | class BankAccount: 5 | def __init__(self, first_name, last_name, account_id, account_type, pin, balance): 6 | self.first_name = first_name 7 | self.last_name = last_name 8 | self.account_id = account_id 9 | self.account_type = account_type 10 | self.pin = pin 11 | self.balance = balance 12 | 13 | def deposit(self, amount): 14 | self.balance = self.balance + amount 15 | return self.balance 16 | 17 | def withdraw(self, amount): 18 | self.balance = self.balance - amount 19 | return self.balance 20 | 21 | def display_balance(self): 22 | print(f"${self.balance}") 23 | 24 | checking_account = BankAccount("Jane", "Doe", 13243546, "checking", 0000, 250.00) 25 | 26 | checking_account.deposit(100) 27 | 28 | checking_account.display_balance() 29 | 30 | checking_account.withdraw(50) 31 | 32 | checking_account.display_balance() 33 | -------------------------------------------------------------------------------- /7-classes-objects/38_pokedex_1.py: -------------------------------------------------------------------------------- 1 | # Pokédex 📟 2 | # Codédex 3 | 4 | # Class definition 5 | class Pokemon: 6 | def __init__(self, entry, name, types, description, is_caught): 7 | self.entry = entry 8 | self.name = name 9 | self.types = types 10 | self.description = description 11 | self.is_caught = is_caught 12 | 13 | def speak(self): 14 | print(self.name + ', ' + self.name + '!') 15 | 16 | def display_details(self): 17 | print('Entry Number: ' + str(self.entry)) 18 | print('Name: ' + self.name) 19 | 20 | if len(self.types) == 1: 21 | print('Type: ' + self.types[0]) 22 | else: 23 | print('Type: ' + self.types[0] + '/' + self.types[1]) 24 | 25 | print('Description: ' + self.description) 26 | 27 | if self.is_caught: 28 | print(self.name + ' has already been caught!') 29 | else: 30 | print(self.name + ' hasn\'t been caught yet.') 31 | 32 | # Pokémon objects 33 | pikachu = Pokemon(25, 'Pikachu', ['Electric'], 'It has small electric sacs on both its cheeks. If threatened, it looses electric charges from the sacs.', True) 34 | charizard = Pokemon(6, 'Charizard', ['Fire', 'Flying'], 'It spits fire that is hot enough to melt boulders. It may cause forest fires by blowing flames.', False) 35 | gyarados = Pokemon(130, 'Gyarados', ['Water', 'Flying'], 'It has an extremely aggressive nature. The HYPER BEAM it shoots from its mouth totally incinerates all targets.', False) 36 | 37 | pikachu.speak() 38 | charizard.speak() 39 | gyarados.speak() -------------------------------------------------------------------------------- /7-classes-objects/38_pokedex_2.py: -------------------------------------------------------------------------------- 1 | # Pokédex 📟 2 | # Codédex 3 | 4 | # Class definition 5 | class Pokemon: 6 | def __init__(self, entry, name, types, description, level, region, is_caught): 7 | self.entry = entry 8 | self.name = name 9 | self.types = types 10 | self.description = description 11 | self.level = level 12 | self.region = region 13 | self.is_caught = is_caught 14 | 15 | def speak(self): 16 | print(self.name + ', ' + self.name + '!') 17 | 18 | def display_details(self): 19 | print('Entry Number: ' + str(self.entry)) 20 | print('Name: ' + self.name) 21 | 22 | if len(self.types) == 1: 23 | print('Type: ' + self.types[0]) 24 | else: 25 | print('Type: ' + self.types[0] + '/' + self.types[1]) 26 | 27 | print('Lv. ' + str(self.level)) 28 | print('Region: ' + self.region) 29 | print('Description: ' + self.description) 30 | 31 | if self.is_caught: 32 | print(self.name + ' has already been caught!') 33 | else: 34 | print(self.name + ' hasn\'t been caught yet.') 35 | 36 | # Pokémon objects 37 | pikachu = Pokemon(25, 'Pikachu', ['Electric'], 'It has small electric sacs on both its cheeks. If threatened, it looses electric charges from the sacs.', 25, 'Kanto', True) 38 | charizard = Pokemon(6, 'Charizard', ['Fire', 'Flying'], 'It spits fire that is hot enough to melt boulders. It may cause forest fires by blowing flames.', 36, 'Kanto', False) 39 | gyarados = Pokemon(130, 'Gyarados', ['Water', 'Flying'], 'It has an extremely aggressive nature. The HYPER BEAM it shoots from its mouth totally incinerates all targets.', 57, 'Kanto', False) 40 | 41 | pikachu.speak() 42 | charizard.speak() 43 | gyarados.speak() -------------------------------------------------------------------------------- /8-modules/39_slot_machine_1.py: -------------------------------------------------------------------------------- 1 | # Slot Machine 🎰 2 | # Codédex 3 | 4 | import random 5 | 6 | symbols = [ 7 | '🍒', 8 | '🍇', 9 | '🍉', 10 | '7️⃣' 11 | ] 12 | 13 | results = random.choices(symbols, k=3) 14 | print(f'{results[0]} | {results[1]} | {results[2]}') 15 | 16 | if (results[0] == '7️⃣' and results[1] == '7️⃣' and results[2] == '7️⃣'): 17 | print('Jackpot! 💰') 18 | else: 19 | print('Thanks for playing!') 20 | -------------------------------------------------------------------------------- /8-modules/39_slot_machine_2.py: -------------------------------------------------------------------------------- 1 | # Slot Machine 🎰 2 | # Codédex 3 | 4 | import random 5 | 6 | symbols = [ 7 | '🍒', 8 | '🍇', 9 | '🍉', 10 | '7️⃣' 11 | ] 12 | 13 | def play(): 14 | 15 | for i in range(1, 51): 16 | results = random.choices(symbols, k=3) 17 | print(f'{results[0]} | {results[1]} | {results[2]}') 18 | win = results[0] == '7️⃣' and results[1] == '7️⃣' and results[2] == '7️⃣' 19 | 20 | if win: 21 | print('Jackpot!!! 💰') 22 | break 23 | else: 24 | results = random.choices(symbols, k=3) 25 | 26 | answer = '' 27 | while answer.upper() != 'N': 28 | play() 29 | answer = input('Keep playing? (Y/N) ') 30 | 31 | print('Thanks for playing!') -------------------------------------------------------------------------------- /8-modules/40_solar_system.py: -------------------------------------------------------------------------------- 1 | # Solar System 🪐 2 | # Codédex 3 | 4 | from math import pi; from random import choice as ch 5 | 6 | planets = [ 7 | 'Mercury', 8 | 'Venus', 9 | 'Earth', 10 | 'Mars', 11 | 'Saturn' 12 | ] 13 | 14 | random_planet = ch(planets) 15 | radius = 0 16 | 17 | if random_planet == 'Mercury': 18 | radius = 2440 19 | elif random_planet == 'Venus': 20 | radius = 6052 21 | elif random_planet == 'Earth': 22 | radius = 6371 23 | elif random_planet == 'Mars': 24 | radius = 3390 25 | elif random_planet == 'Saturn': 26 | radius = 58232 27 | else: 28 | print('Oops! An error occurred.') 29 | 30 | planet_area = 4 * pi * radius * radius 31 | 32 | print(f'Area of {random_planet}: {planet_area} sq mi') 33 | -------------------------------------------------------------------------------- /8-modules/41_bday_messages.py: -------------------------------------------------------------------------------- 1 | # Countdown 🎂 2 | # Codédex 3 | 4 | import random 5 | 6 | bday_messages = [ 7 | 'Hope you have a very Happy Birthday! 🎈', 8 | 'It\'s your special day – get out there and celebrate! 🎉', 9 | 'You were born and the world got better – everybody wins! Happy Birthday! 🥳', 10 | 'Have lots of fun on your special day! 🎂', 11 | 'Another year of you going around the sun! 🌞' 12 | ] 13 | 14 | random_message = random.choice(bday_messages) -------------------------------------------------------------------------------- /8-modules/41_main.py: -------------------------------------------------------------------------------- 1 | # Countdown 🎂 2 | # Codédex 3 | 4 | import datetime, bday_messages 5 | 6 | today = datetime.date.today() 7 | 8 | my_next_birthday = datetime.date(2023, 4, 5) 9 | 10 | days_away = my_next_birthday - today 11 | 12 | if my_next_birthday == today: 13 | print(bday_messages.random_message) 14 | else: 15 | print(f'My next birthday is {days_away.days} days away!') 16 | -------------------------------------------------------------------------------- /8-modules/42_forty_two.py: -------------------------------------------------------------------------------- 1 | # Forty Two 4️⃣2️⃣ 2 | # Codédex 3 | 4 | import wikipedia 5 | 6 | print(wikipedia.search('Philosophy of life')) -------------------------------------------------------------------------------- /8-modules/43_zen.py: -------------------------------------------------------------------------------- 1 | # The Zen of Python 📜 2 | # Codédex 3 | 4 | import this 5 | 6 | """ 7 | Beautiful is better than ugly. 8 | Explicit is better than implicit. 9 | Simple is better than complex. 10 | Complex is better than complicated. 11 | Flat is better than nested. 12 | Sparse is better than dense. 13 | Readability counts. 14 | Special cases aren't special enough to break the rules. 15 | Although practicality beats purity. 16 | Errors should never pass silently. 17 | Unless explicitly silenced. 18 | In the face of ambiguity, refuse the temptation to guess. 19 | There should be one-- and preferably only one --obvious way to do it. 20 | Although that way may not be obvious at first unless you're Dutch. 21 | Now is better than never. 22 | Although never is often better than *right* now. 23 | If the implementation is hard to explain, it's a bad idea. 24 | If the implementation is easy to explain, it may be a good idea. 25 | Namespaces are one honking great idea -- let's do more of those! 26 | """ 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Codédex 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |