├── .gitignore ├── README.md ├── archivos.py ├── archivos ├── names.txt └── numbers.txt ├── assert_satatements.py ├── data_filter.py ├── debugging.py ├── dictionary_comprehensions.py ├── hangman_game.py ├── list_comprehensions.py ├── lists_and_dicts.py ├── practice_filter.py ├── practice_map.py ├── practice_reduce.py ├── teacher_solution.py └── words.txt /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | .scrapy -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Curso Intermedio de Python 2 | Todos los ejercicicios del curso de Python. Pero lo más importante es el juego del ahorcado, es el archivo llamado hangman_game.py 3 | 4 | Aquí están las notas del curso en Notion: 5 | https://www.notion.so/anthonymanotoa/Curso-de-Python-Intermedio-10df9cddb0b946798ebe7e829701b2d4 -------------------------------------------------------------------------------- /archivos.py: -------------------------------------------------------------------------------- 1 | def read(): 2 | numbers = [] 3 | with open('./archivos/numbers.txt', 'r', encoding='utf-8') as f: 4 | for i in f: 5 | numbers.append(int(i)) 6 | print(numbers) 7 | 8 | 9 | def write(): 10 | names = ['Tony', 'Julio', 'Pepe', 'Christian', 'Rocío'] 11 | with open('./archivos/names.txt', 'a', encoding='utf-8') as f: 12 | for name in names: 13 | f.write(name) 14 | f.write('\n') 15 | 16 | 17 | def run(): 18 | write() 19 | 20 | 21 | if __name__ == '__main__': 22 | run() -------------------------------------------------------------------------------- /archivos/names.txt: -------------------------------------------------------------------------------- 1 | Tony 2 | Miguel 3 | Pepe 4 | Christian 5 | Rocío 6 | Tony 7 | Miguel 8 | Pepe 9 | Christian 10 | Rocío 11 | David -------------------------------------------------------------------------------- /archivos/numbers.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 45 3 | 21 4 | 54 5 | 123 6 | 5486 7 | 5156 8 | 132 9 | 4 10 | 96 11 | 21 12 | 46 13 | 54 14 | 58 -------------------------------------------------------------------------------- /assert_satatements.py: -------------------------------------------------------------------------------- 1 | def divisors(num): 2 | divisors = [i for i in range(1, num + 1) if num % i == 0] 3 | return divisors 4 | 5 | 6 | def run(): 7 | num = input('Ingresa un número: ') 8 | # assert int(num) <= 0, 'Debe ser un numero positivo' 9 | # isnumerio() es un metodo de las str que devuelve True si es 'numero' y False si no 10 | assert num.isnumeric(), 'Debes ingresar un número' 11 | 12 | print(divisors(int(num))) 13 | print('Termino mi programa') 14 | 15 | 16 | if __name__ == '__main__': 17 | run() -------------------------------------------------------------------------------- /data_filter.py: -------------------------------------------------------------------------------- 1 | # escribir en MAYÚSCULAS significa una constante (es decir, que no se modificará) por lo que va por encima de la función run() 2 | DATA = [ 3 | { 4 | 'name': 'Facundo', 5 | 'age': 72, 6 | 'organization': 'Platzi', 7 | 'position': 'Technical Coach', 8 | 'language': 'python', 9 | }, 10 | { 11 | 'name': 'Luisana', 12 | 'age': 33, 13 | 'organization': 'Globant', 14 | 'position': 'UX Designer', 15 | 'language': 'javascript', 16 | }, 17 | { 18 | 'name': 'Héctor', 19 | 'age': 19, 20 | 'organization': 'Platzi', 21 | 'position': 'Associate', 22 | 'language': 'ruby', 23 | }, 24 | { 25 | 'name': 'Gabriel', 26 | 'age': 20, 27 | 'organization': 'Platzi', 28 | 'position': 'Associate', 29 | 'language': 'javascript', 30 | }, 31 | { 32 | 'name': 'Isabella', 33 | 'age': 30, 34 | 'organization': 'Platzi', 35 | 'position': 'QA Manager', 36 | 'language': 'java', 37 | }, 38 | { 39 | 'name': 'Karo', 40 | 'age': 23, 41 | 'organization': 'Everis', 42 | 'position': 'Backend Developer', 43 | 'language': 'python', 44 | }, 45 | { 46 | 'name': 'Ariel', 47 | 'age': 32, 48 | 'organization': 'Rappi', 49 | 'position': 'Support', 50 | 'language': '', 51 | }, 52 | { 53 | 'name': 'Juan', 54 | 'age': 17, 55 | 'organization': '', 56 | 'position': 'Student', 57 | 'language': 'go', 58 | }, 59 | { 60 | 'name': 'Pablo', 61 | 'age': 32, 62 | 'organization': 'Master', 63 | 'position': 'Human Resources Manager', 64 | 'language': 'python', 65 | }, 66 | { 67 | 'name': 'Lorena', 68 | 'age': 56, 69 | 'organization': 'Python Organization', 70 | 'position': 'Language Maker', 71 | 'language': 'python', 72 | }, 73 | ] 74 | 75 | 76 | def run(): 77 | # usando list comprehensions 78 | # all_python_devs = [worker['name'] for worker in DATA if worker['language'] == 'python'] 79 | # all_Platzi_workers = [worker['name'] for worker in DATA if worker['organization'] == 'Platzi'] 80 | 81 | # # usando filter() and map() 82 | # adults = list(filter(lambda worker: worker['age'] > 17, DATA)) 83 | # adults = list(map(lambda worker: worker['name'], adults)) 84 | # old_people = list(map(lambda worker: worker | {'old': worker['age'] > 70}, DATA)) # el | sirve para sumar diccionarios 85 | 86 | #RETO inverso 87 | #filter() and map() 88 | all_python_devs = list(filter(lambda worker: worker['language'] == 'python', DATA)) 89 | all_python_devs = list(map(lambda worker: worker['name'], all_python_devs)) 90 | 91 | all_Platzi_workers = list(filter(lambda worker: worker['organization'] == 'Platzi', DATA)) 92 | all_Platzi_workers = list(map(lambda worker: worker['name'], all_Platzi_workers)) 93 | 94 | #list comprehensions 95 | adults = [worker['name'] for worker in DATA if worker['age'] > 17] 96 | 97 | old_people = [worker | {'old': worker['age'] > 70} for worker in DATA] 98 | 99 | for worker in old_people: 100 | print(worker) 101 | 102 | 103 | if __name__ == '__main__': 104 | run() -------------------------------------------------------------------------------- /debugging.py: -------------------------------------------------------------------------------- 1 | def divisors(num): 2 | try: 3 | if num < 0: 4 | raise ValueError('Ingresa un número positivo') 5 | else: 6 | divisors = [i for i in range(1, num + 1) if num % i == 0] 7 | return divisors 8 | except ValueError as ve: 9 | return ve 10 | 11 | 12 | def run(): 13 | try: 14 | num = int(input('Ingresa un número: ')) 15 | print(divisors(num)) 16 | print('Termino mi programa') 17 | except ValueError: 18 | print('Debes ingresar un numero :c') 19 | 20 | 21 | if __name__ == '__main__': 22 | run() -------------------------------------------------------------------------------- /dictionary_comprehensions.py: -------------------------------------------------------------------------------- 1 | # Dictionary with the first 100 natural numbers as its key and numbers**3 as its values 2 | import math 3 | 4 | def run(): 5 | # dictionay_naturals = {i: i**3 for i in range(1, 101) if i % 3 != 0} 6 | # return dictionay_naturals 7 | 8 | # CHALLENGE 1000 first natural numbers as keys and sqrt(numbers) as values 9 | my_dic = {i: math.sqrt(i) for i in range (1, 1001)} 10 | return my_dic 11 | 12 | 13 | if __name__ == '__main__': 14 | print(run()) -------------------------------------------------------------------------------- /hangman_game.py: -------------------------------------------------------------------------------- 1 | import os 2 | import random 3 | 4 | 5 | def read_words(filepath='./words.txt'): 6 | 7 | with open(filepath, 'r', encoding='utf-8') as f: 8 | list_words = [word.strip().upper() for word in f] # el strip() quita ese espacio final 9 | choosen_word = list_words[random.randint(0, len(list_words))] 10 | 11 | return choosen_word 12 | 13 | 14 | def game(magic_word, letter, game_word): 15 | if letter in magic_word: 16 | for i in range(len(magic_word)): 17 | if letter == magic_word[i]: 18 | game_word[i] = letter 19 | 20 | return ' '.join(game_word) 21 | 22 | 23 | def run(): 24 | lives = 5 25 | letter = '' 26 | magic_word = read_words() 27 | game_word = ['_' for i in range(len(magic_word))] 28 | 29 | while lives > 0: 30 | os.system('cls') 31 | print(f'Vidas restantes: {"❤" * (lives)}') 32 | print('¡Adivina la palabra!') 33 | print(game(magic_word, letter, game_word)) 34 | try: 35 | if len(letter) > 1: 36 | raise ValueError('Solo puedes ingresar una letra') 37 | else: 38 | if game(magic_word, letter, game_word).count('_') > 0: 39 | if letter in game(magic_word, letter, game_word): 40 | letter = input('Escoge una letra: ').upper() 41 | else: 42 | lives -= 1 43 | if lives > 0: 44 | os.system('cls') 45 | print(f'Vidas restantes: {"❤" * (lives)}') 46 | print('¡Adivina la palabra!') 47 | print(game(magic_word, letter, game_word)) 48 | letter = input('Escoge una letra: ').upper() 49 | else: 50 | os.system('cls') 51 | print('¡Te quedaste sin vidas!\nJuego terminado...\n') 52 | print('La palabra era: ' + magic_word) 53 | else: 54 | print('¡Ganaste! √') 55 | break 56 | 57 | except ValueError as ve: 58 | return print(ve) 59 | 60 | 61 | if __name__ == '__main__': 62 | run() 63 | 64 | 65 | # Notas finales para mejorar o modificar el juego: 66 | # Investiga la función enumerate (documentacion) 67 | # El método get de los diccionarios te puede servir 68 | # Dibuja el ahorcado con el codigo ascii y dibujar en pantalla 69 | # Mejora la interfaz con emojis -------------------------------------------------------------------------------- /list_comprehensions.py: -------------------------------------------------------------------------------- 1 | # lista de los primeros 100 numeros naturales al cuadrado mientas no sean divisibles para 3 2 | 3 | def run(): 4 | # natural_numbers = [] 5 | # for i in range(1, 101): 6 | # if i % 3 != 0: 7 | # natural_numbers.append(i**2) 8 | # return natural_numbers 9 | 10 | # natural_numbers = [i**2 for i in range(1, 101) if i % 3 != 0] 11 | # return natural_numbers 12 | 13 | # RETO: lista de 1 hasta max 5 digitos que sean multiplos de 4, 6 y 9. 14 | reto = [i for i in range(1, 100_000) if i % 4 == 0 and i % 6 == 0 and i % 9 == 0] 15 | return reto 16 | 17 | 18 | if __name__ == '__main__': 19 | print(run()) -------------------------------------------------------------------------------- /lists_and_dicts.py: -------------------------------------------------------------------------------- 1 | def run(): 2 | my_list = [1, 'Hello', True, 4.5] 3 | my_dic = {'fistname': 'Tony', 'lastname': 'Manotoa'} 4 | 5 | super_list = [ 6 | {'fistname': 'Tony', 'lastname': 'Manotoa'}, 7 | {'fistname': 'Angie', 'lastname': 'Paladines'}, 8 | {'fistname': 'Max', 'lastname': 'Medina'}, 9 | {'fistname': 'Jandry', 'lastname': 'Camacho'}, 10 | {'fistname': 'David', 'lastname': 'Pardo'} 11 | ] 12 | 13 | super_dic = { 14 | 'natural_nums': [1, 2, 3, 4, 5], 15 | 'integer_num': [-1, -2, 0, 1, 2], 16 | 'float_nums': [1.1, 4.5, 6.43] 17 | } 18 | 19 | for key, value in super_dic.items(): # con .items() imprimes tanto el value como la key 20 | print(key, '-', value) 21 | 22 | print('\n' + '*' * 50 + '\n') 23 | 24 | for dicts in super_list: 25 | for key, value in dicts.items(): 26 | print(f'{key} - {value}') 27 | print('-' * 50) 28 | 29 | print(my_list, my_dic) 30 | 31 | if __name__ == '__main__': 32 | run() -------------------------------------------------------------------------------- /practice_filter.py: -------------------------------------------------------------------------------- 1 | # Usando list comprehension: 2 | my_list = [1, 4, 5, 6, 9, 13, 19, 21] 3 | odd_comprehension = [i for i in my_list if i % 2 != 0] 4 | print(odd_comprehension) 5 | 6 | # Usando filter: 7 | odd_filter = list(filter(lambda i: i % 2 != 0, my_list)) 8 | print(odd_filter) -------------------------------------------------------------------------------- /practice_map.py: -------------------------------------------------------------------------------- 1 | list1 = [1, 2, 3, 4, 5] 2 | 3 | # Con list comprehension: 4 | squares = [i**2 for i in list1] 5 | print(squares) 6 | 7 | # Con map: 8 | squares_map = list(map(lambda i: i**2, list1)) 9 | print(squares_map) -------------------------------------------------------------------------------- /practice_reduce.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | 3 | my_list = [2, 2, 2, 2, 2] 4 | all_multiplied = reduce(lambda a, b: a * b, my_list) 5 | print(all_multiplied) -------------------------------------------------------------------------------- /teacher_solution.py: -------------------------------------------------------------------------------- 1 | import random 2 | import os 3 | 4 | 5 | def read_data(filepath="./archivos/data.txt"): 6 | words = [] 7 | with open(filepath, "r", encoding="utf-8") as f: 8 | for line in f: 9 | words.append(line.strip().upper()) 10 | return words 11 | 12 | 13 | def run(): 14 | data = read_data(filepath="./words.txt") 15 | chosen_word = random.choice(data) 16 | chosen_word_list = [letter for letter in chosen_word] 17 | chosen_word_list_underscores = ["_"] * len(chosen_word_list) 18 | letter_index_dict = {} 19 | for idx, letter in enumerate(chosen_word): 20 | if not letter_index_dict.get(letter): 21 | letter_index_dict[letter] = [] 22 | letter_index_dict[letter].append(idx) 23 | 24 | while True: 25 | os.system("cls") # Si estás en Unix (Mac o Linux) cambia cls por clear 26 | print("¡Adivina la palabra!") 27 | for element in chosen_word_list_underscores: 28 | print(element + " ", end="") 29 | print("\n") 30 | 31 | letter = input("Ingresa una letra: ").strip().upper() 32 | assert letter.isalpha(), "Solo puedes ingresar letras" 33 | 34 | if letter in chosen_word_list: 35 | for idx in letter_index_dict[letter]: 36 | chosen_word_list_underscores[idx] = letter 37 | 38 | if "_" not in chosen_word_list_underscores: 39 | os.system("cls") # Si estás en Unix (Mac o Linux) cambia cls por clear 40 | print("¡Ganaste! La palabra era", chosen_word) 41 | break 42 | 43 | 44 | if __name__ == '__main__': 45 | run() -------------------------------------------------------------------------------- /words.txt: -------------------------------------------------------------------------------- 1 | crema 2 | café 3 | estrella 4 | explosión 5 | guitarra 6 | plástico 7 | navaja 8 | martillo 9 | libros 10 | lápiz 11 | lapicera 12 | aluminio 13 | embarcación 14 | letra 15 | agujeta 16 | ventana 17 | librería 18 | sonido 19 | universidad 20 | rueda 21 | perro 22 | llaves 23 | camisa 24 | pelo 25 | papá 26 | sillón 27 | felicidad 28 | catre 29 | teclado 30 | servilleta 31 | escuela 32 | pantalla 33 | sol 34 | codo 35 | tenedor 36 | estadística 37 | mapa 38 | agua 39 | mensaje 40 | lima 41 | cohete 42 | rey 43 | edificio 44 | césped 45 | presidencia 46 | hojas 47 | parlante 48 | colegio 49 | granizo 50 | pestaña 51 | lámpara 52 | mano 53 | monitor 54 | flor 55 | música 56 | hombre 57 | tornillo 58 | habitación 59 | velero 60 | abuela 61 | abuelo 62 | palo 63 | satélite 64 | templo 65 | lentes 66 | bolígrafo 67 | plato 68 | nube 69 | gobierno 70 | botella 71 | castillo 72 | enano 73 | casa 74 | libro 75 | persona 76 | planeta 77 | televisor 78 | guantes 79 | metal 80 | teléfono 81 | proyector 82 | mono 83 | remera 84 | muela 85 | petróleo 86 | percha 87 | remate 88 | debate 89 | anillo 90 | cuaderno 91 | ruido 92 | pared 93 | taladro 94 | herramienta 95 | cartas 96 | chocolate 97 | anteojos 98 | impresora 99 | caramelos 100 | living 101 | luces 102 | angustia 103 | zapato 104 | bomba 105 | lluvia 106 | ojo 107 | corbata 108 | periódico 109 | diente 110 | planta 111 | chupetín 112 | buzo 113 | oficina 114 | persiana 115 | puerta 116 | tío 117 | silla 118 | ensalada 119 | pradera 120 | zoológico 121 | candidato 122 | deporte 123 | recipiente 124 | diarios 125 | fotografía 126 | ave 127 | hierro 128 | refugio 129 | pantalón 130 | barco 131 | carne 132 | nieve 133 | tecla 134 | humedad 135 | pistola 136 | departamento 137 | celular 138 | tristeza 139 | hipopótamo 140 | sofá 141 | cama 142 | árbol 143 | mesada 144 | campera 145 | discurso 146 | auto 147 | cinturón 148 | rúcula 149 | famoso 150 | madera 151 | lentejas 152 | piso 153 | maletín 154 | reloj 155 | diputado 156 | cuchillo 157 | desodorante 158 | candado 159 | luz 160 | montañas 161 | computadora 162 | radio 163 | moño 164 | cuadro 165 | calor 166 | partido 167 | teatro 168 | bife 169 | fiesta 170 | bala 171 | auriculares 172 | civil --------------------------------------------------------------------------------