diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..2ea2eb049 Binary files /dev/null and b/.DS_Store differ diff --git a/M03_variablesydatos/Prep_Course_Homework_03-Resuelto.ipynb b/M03_variablesydatos/Prep_Course_Homework_03-Resuelto.ipynb index 43fd9bdb6..4b144764c 100644 --- a/M03_variablesydatos/Prep_Course_Homework_03-Resuelto.ipynb +++ b/M03_variablesydatos/Prep_Course_Homework_03-Resuelto.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -134,18 +134,19 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "complex" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" + "ename": "NameError", + "evalue": "name 'n_complejo' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mtype\u001b[39m(\u001b[43mn_complejo\u001b[49m)\n", + "\u001b[0;31mNameError\u001b[0m: name 'n_complejo' is not defined" + ] } ], "source": [ @@ -162,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -742,7 +743,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/M03_variablesydatos/Prep_Course_Homework_03.ipynb b/M03_variablesydatos/Prep_Course_Homework_03.ipynb index b4d68b08b..ad0b74473 100644 --- a/M03_variablesydatos/Prep_Course_Homework_03.ipynb +++ b/M03_variablesydatos/Prep_Course_Homework_03.ipynb @@ -18,10 +18,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], + "source": [ + "a= 7\n", + "print(a)" + ] }, { "attachments": {}, @@ -33,10 +44,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(8.5)" + ] }, { "attachments": {}, @@ -48,10 +72,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(a)" + ] }, { "attachments": {}, @@ -63,10 +100,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "nombre = 'Alejandro'" + ] }, { "attachments": {}, @@ -78,10 +117,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "numero_complejo = 5 + 3 " + ] }, { "attachments": {}, @@ -93,10 +134,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(numero_complejo)\n" + ] }, { "attachments": {}, @@ -108,10 +162,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.1416\n" + ] + } + ], + "source": [ + "pi = 3.1416\n", + "print(round(pi,4))" + ] }, { "attachments": {}, @@ -123,10 +188,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "variable = True\n", + "Variable2 = 'True'" + ] }, { "attachments": {}, @@ -138,10 +206,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "print(type(variable))\n", + "print(type(Variable2))" + ] }, { "attachments": {}, @@ -153,10 +233,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11.8\n" + ] + } + ], + "source": [ + "Numero = 5 + 6.8\n", + "print(Numero)" + ] }, { "attachments": {}, @@ -168,10 +259,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18\n" + ] + } + ], + "source": [ + "a = 2 + 4\n", + "b = 5 + 7\n", + "\n", + "print(a + b)" + ] }, { "attachments": {}, @@ -183,10 +287,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15.3\n" + ] + } + ], + "source": [ + "c = a + 9.3\n", + "print(c)" + ] }, { "attachments": {}, @@ -198,10 +313,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16\n" + ] + } + ], + "source": [ + "print(8*2)" + ] }, { "attachments": {}, @@ -213,10 +338,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "64\n" + ] + } + ], + "source": [ + "print(8**2)" + ] }, { "attachments": {}, @@ -228,10 +363,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.75\n" + ] + } + ], + "source": [ + "d = 7/4 \n", + "print(d)" + ] }, { "attachments": {}, @@ -243,10 +389,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "print(7//4)" + ] }, { "attachments": {}, @@ -258,10 +414,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "print(7%4)" + ] }, { "attachments": {}, @@ -273,10 +439,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], + "source": [ + "print (6*4+3)" + ] }, { "attachments": {}, @@ -288,10 +464,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Colombia Bogota\n" + ] + } + ], + "source": [ + "var1 = ' Colombia '\n", + "var2 = 'Bogota'\n", + "\n", + "print(var1 + var2)" + ] }, { "attachments": {}, @@ -303,10 +492,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print( 2 == '2')" + ] }, { "attachments": {}, @@ -318,10 +517,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print( 2 == int('2'))" + ] }, { "attachments": {}, @@ -333,10 +542,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#a = float('3,8')# no se puede convertir un string en float " + ] }, { "attachments": {}, @@ -348,10 +559,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "e = 3 \n", + "\n", + "e -= 1 \n", + "\n", + "print(e)" + ] }, { "attachments": {}, @@ -363,10 +588,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "print(1 << 2) # representa el desplazamiento de bits a la derecha " + ] }, { "attachments": {}, @@ -378,10 +613,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#print(2 + '2') No se puede sumar str con int " + ] }, { "attachments": {}, @@ -393,10 +630,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola Hola Hola Hola \n" + ] + } + ], + "source": [ + "print ( 4 * 'Hola ' )" + ] } ], "metadata": { @@ -418,7 +665,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/M03_variablesydatos/Prep_Course_Homework_03.py b/M03_variablesydatos/Prep_Course_Homework_03.py index eb7ddb9eb..eb2cdc921 100644 --- a/M03_variablesydatos/Prep_Course_Homework_03.py +++ b/M03_variablesydatos/Prep_Course_Homework_03.py @@ -6,38 +6,31 @@ # 1) Crear una variable que contenga un elemento del conjunto de números enteros y luego imprimir por pantalla # In[7]: - - - +a= 7 +print(a) # 2) Imprimir el tipo de dato de la constante 8.5 -# In[3]: - - - +type(8.5) # 3) Imprimir el tipo de dato de la variable creada en el punto 1 # In[8]: - - - - +type(a) # 4) Crear una variable que contenga tu nombre # In[2]: - +nombre = 'Alejandro' # 5) Crear una variable que contenga un número complejo # In[3]: - +numero_complejo = 5 + 3 @@ -45,134 +38,113 @@ # In[4]: +type(numero_complejo) # 7) Crear una variable que contenga el valor del número Pi redondeado a 4 decimales - -# In[1]: - - pi = 3.1416 - +print(round(pi,4)) +# In[1]: # 8) Crear una variable que contenga el valor 'True' y otra que contenga el valor True. ¿Se trata de lo mismo? -# In[3]: - - +variable = True +Variable2 = 'True' +# In[3]: # 9) Imprimir el tipo de dato correspondientes a las variables creadas en el punto 8 +print(type(variable)) +print(type(Variable2)) # In[5]: - - - # 10) Asignar a una variable, la suma de un número entero y otro decimal # In[1]: - - - +Numero = 5 + 6.8 +print(Numero) # 11) Realizar una operación de suma de números complejos +a = 2 + 4 +b = 5 + 7 +print(a + b) # In[2]: - - - - # 12) Realizar una operación de suma de un número real y otro complejo +c = a + 9.3 +print(c) # In[4]: - - - # 13) Realizar una operación de multiplicación +print(8*2) # In[5]: - - - # 14) Mostrar el resultado de elevar 2 a la octava potencia - +print(8**2) # In[6]: - - # 15) Obtener el cociente de la división de 27 entre 4 en una variable y luego mostrarla - +d = 7/4 +print(d) # In[8]: - - - # 16) De la división anterior solamente mostrar la parte entera - +print(7//4) # In[9]: - - # 17) De la división de 27 entre 4 mostrar solamente el resto - +print(7%4) # In[1]: - - - - # 18) Utilizando como operandos el número 4 y los resultados obtenidos en los puntos 16 y 17. Obtener 27 como resultado - +print (6*4+3) # In[2]: - - # 19) Utilizar el operador "+" en una operación donde intervengan solo variables alfanuméricas +var1 = ' Colombia ' +var2 = 'Bogota' +print(var1 + var2) # In[3]: - - # 20) Evaluar si "2" es igual a 2. ¿Por qué ocurre eso? # In[4]: - - +print( 2 == '2') # 21) Utilizar las funciones de cambio de tipo de dato, para que la validación del punto 20 resulte verdadera # In[11]: - - +print( 2 == int('2')) # 22) ¿Por qué arroja error el siguiente cambio de tipo de datos? a = float('3,8') # In[12]: - +#a = float('3,8')# no se puede convertir un string en float @@ -180,8 +152,11 @@ # In[15]: +e = 3 +e -= 1 +print(e) # 24) Realizar la operacion 1 << 2 ¿Por qué da ese resultado? ¿Qué es el sistema de numeración binario? @@ -189,14 +164,14 @@ # In[29]: - +print(1 << 2) # representa el desplazamiento de bits a la derecha # 25) Realizar la operación 2 + '2' ¿Por qué no está permitido? ¿Si los dos operandos serían del mismo tipo, siempre arrojaría el mismo resultado? # In[23]: - +#print(2 + '2') No se puede sumar str con int @@ -205,5 +180,5 @@ # In[30]: - +print ( 4 * 'Hola ' ) diff --git a/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb b/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb index 5ae3cbd63..1c7056c56 100644 --- a/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb +++ b/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb @@ -21,7 +21,16 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "Variable = 3\n", + "\n", + "if Variable > 0:\n", + " print(f'{Variable}Es mayor que 0')\n", + "elif Variable < 0:\n", + " print(f'{Variable}Es menor que 0')\n", + "else :\n", + " print(f'{Variable}Es igual que 0')" + ] }, { "attachments": {}, @@ -36,7 +45,15 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "a = 4\n", + "b = 2.3\n", + "\n", + "if type(a) == type(b):\n", + " print('son el mismo tipo de datos')\n", + "else:\n", + " print('no son el mismo tipo de datos')" + ] }, { "attachments": {}, @@ -51,7 +68,13 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for n in range(1,21):\n", + " if n % 2 == 0:\n", + " print(f'{n}','' ,'es par')\n", + " else:\n", + " print(f'{n}', '','es impar')" + ] }, { "attachments": {}, @@ -66,7 +89,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for n in range(0,6):\n", + " print('El valor', str(n), 'elevado a la 3 es ', str(n**3))" + ] }, { "attachments": {}, @@ -81,7 +107,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for n in range(0,numero):\n", + " print(n)" + ] }, { "attachments": {}, @@ -96,7 +125,19 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "if Variable == int(Variable):\n", + " if (Variable > 0):\n", + " factorial = Variable\n", + " while (Variable > 2):\n", + " Variable -= 1\n", + " factorial = factorial * Variable\n", + " print('el factorial es', factorial)\n", + " else:\n", + " print('el numero es menor que cero')\n", + "else:\n", + " print('el numero no es entero')" + ] }, { "attachments": {}, @@ -111,7 +152,16 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "i = 0 \n", + "\n", + "while (numero < 5):\n", + " numero += 1\n", + " for i in range(0,numero):\n", + " print('el', 'for', i)\n", + " print('el', 'while', numero)\n", + "print('fin')" + ] }, { "attachments": {}, @@ -126,7 +176,15 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "n = 5\n", + "\n", + "for i in range(1,n):\n", + " while (i < 5):\n", + " i += 1\n", + " print('for', i)\n", + " print('while', n)" + ] }, { "attachments": {}, @@ -141,7 +199,23 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "Rango = 30 \n", + "numero = 0\n", + "primo = True\n", + "\n", + "while numero < Rango:\n", + " for prim in range(2,numero):\n", + " if numero % prim == 0:\n", + " primo = False\n", + " \n", + " if (primo):\n", + " print(numero)\n", + " else:\n", + " primo = True\n", + " \n", + " numero += 1" + ] }, { "attachments": {}, @@ -156,7 +230,23 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "Rango = 30 \n", + "numero = 0\n", + "primo = True\n", + "\n", + "while numero < Rango:\n", + " for prim in range(2,numero):\n", + " if numero % prim == 0:\n", + " primo = False\n", + " break\n", + " if (primo):\n", + " print(numero)\n", + " else:\n", + " primo = True\n", + " \n", + " numero += 1" + ] }, { "attachments": {}, @@ -168,10 +258,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n", + "el numero de ciclos break son 134\n" + ] + } + ], + "source": [ + "Rango = 30 \n", + "numero = 0\n", + "primo = True\n", + "ciclos_break = 0\n", + "while numero < Rango:\n", + " for prim in range(2,numero):\n", + " ciclos_break += 1\n", + " if numero % prim == 0:\n", + " primo = False\n", + " break\n", + " if (primo):\n", + " print(numero)\n", + " else:\n", + " primo = True\n", + " \n", + " numero += 1\n", + "print(f'el numero de ciclos break son {ciclos_break}')" + ] }, { "attachments": {}, @@ -183,10 +311,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n", + "el numero de ciclos break son 134\n", + "optimizacion de: {1100} %\n" + ] + } + ], + "source": [ + "Rango = 30 \n", + "numero = 0\n", + "primo = True\n", + "ciclos_break = 0\n", + "ciclos_sin_break = 0\n", + "while numero < Rango:\n", + " for prim in range(2,numero):\n", + " ciclos_break += 1\n", + " if numero % prim == 0:\n", + " primo = False\n", + " break\n", + " if (primo):\n", + " ciclos_sin_break += 1\n", + " print(numero)\n", + " else:\n", + " primo = True\n", + " \n", + " numero += 1\n", + "print(f'el numero de ciclos break son {ciclos_break}')\n", + "print(f'optimizacion de: ', {(ciclos_break // ciclos_sin_break)* 100 }, '%')" + ] }, { "attachments": {}, @@ -201,7 +371,16 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "numero = 0\n", + "\n", + "while (numero <= 300):\n", + " numero += 1\n", + " if numero % 12 != 0:\n", + " continue\n", + " else:\n", + " print(numero)" + ] }, { "attachments": {}, @@ -216,7 +395,22 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while True:\n", + " numero_ingresado = int(input('ingresa numero, escribe 1 para terminar : '))\n", + " if numero_ingresado == 1:\n", + " break\n", + " try:\n", + " for i in range(2,numero_ingresado):\n", + " if numero_ingresado % i == 0:\n", + " primo = False\n", + " print(f'{numero_ingresado}','No es primo')\n", + " break\n", + " if (primo):\n", + " print(f'{numero_ingresado}', 'Es primo')\n", + " except ValueError:\n", + " print('Numero mayor que 2')" + ] }, { "attachments": {}, @@ -231,7 +425,14 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "n = 100\n", + "while(n<=300):\n", + " if n % 3 == 0 and n % 6 == 0:\n", + " print('El número es: ', str(n))\n", + " break\n", + " n += 1" + ] } ], "metadata": { @@ -253,7 +454,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/M04_flujosdecontrol/Prep_Course_Homework_04.py b/M04_flujosdecontrol/Prep_Course_Homework_04.py index bb082d691..ab8b65acf 100644 --- a/M04_flujosdecontrol/Prep_Course_Homework_04.py +++ b/M04_flujosdecontrol/Prep_Course_Homework_04.py @@ -7,15 +7,27 @@ # In[4]: +Variable = 3 - +if Variable > 0: + print(f'{Variable}Es mayor que 0') +elif Variable < 0: + print(f'{Variable}Es menor que 0') +else : + print(f'{Variable}Es igual que 0') # 2) Crear dos variables y un condicional que informe si son del mismo tipo de dato # In[5]: +a = 4 +b = 2.3 +if type(a) == type(b): + print('son el mismo tipo de datos') +else: + print('no son el mismo tipo de datos') @@ -23,7 +35,11 @@ # In[7]: - +for n in range(1,21): + if n % 2 == 0: + print(f'{n}','' ,'es par') + else: + print(f'{n}', '','es impar') @@ -31,15 +47,17 @@ # In[9]: - - - +for n in range(0,6): + print('El valor', str(n), 'elevado a la 3 es ', str(n**3)) # 5) Crear una variable que contenga un número entero y realizar un ciclo for la misma cantidad de ciclos # In[10]: +numero = 4 +for n in range(0,numero): + print(n) @@ -47,36 +65,88 @@ # In[33]: +Variable = 3 - +if Variable == int(Variable): + if (Variable > 0): + factorial = Variable + while (Variable > 2): + Variable -= 1 + factorial = factorial * Variable + print('el factorial es', factorial) + else: + print('el numero es menor que cero') +else: + print('el numero no es entero') # 7) Crear un ciclo for dentro de un ciclo while # In[38]: +i = 0 - +while (numero < 5): + numero += 1 + for i in range(0,numero): + print('el', 'for', i) + print('el', 'while', numero) +print('fin') + # 8) Crear un ciclo while dentro de un ciclo for # In[3]: +n = 5 - - +for i in range(1,n): + while (i < 5): + i += 1 + print('for', i) + print('while', n) # 9) Imprimir los números primos existentes entre 0 y 30 # In[54]: +Rango = 30 +numero = 0 +primo = True +while numero < Rango: + for prim in range(2,numero): + if numero % prim == 0: + primo = False + + if (primo): + print(numero) + else: + primo = True + + numero += 1 + # 10) ¿Se puede mejorar el proceso del punto 9? Utilizar las sentencias break y/ó continue para tal fin # In[55]: +Rango = 30 +numero = 0 +primo = True + +while numero < Rango: + for prim in range(2,numero): + if numero % prim == 0: + primo = False + break + if (primo): + print(numero) + else: + primo = True + + numero += 1 @@ -86,7 +156,7 @@ # In[56]: - +# En parar la iteraciòn al momento de encontrar el primer divisior y evitar iteraciones innesesarias # In[57]: @@ -98,7 +168,7 @@ # In[58]: - +#Si, ya que la al aumnentar el rango, incrementa las iteraciones @@ -111,15 +181,36 @@ # In[62]: +numero = 0 - - +while (numero <= 300): + numero += 1 + if numero % 12 != 0: + continue + else: + print(numero) # 14) Utilizar la función **input()** que permite hacer ingresos por teclado, para encontrar números primos y dar la opción al usario de buscar el siguiente # In[73]: - +# Primo = True + +while True: + numero_ingresado = int(input('ingresa numero, escribe 1 para terminar : ')) + if numero_ingresado == 1: + break + try: + for i in range(2,numero_ingresado): + if numero_ingresado % i == 0: + primo = False + print(f'{numero_ingresado}','No es primo') + break + if (primo): + print(f'{numero_ingresado}', 'Es primo') + except ValueError: + print('Numero mayor que 2') + # 15) Crear un ciclo while que encuentre dentro del rango de 100 a 300 el primer número divisible por 3 y además múltiplo de 6 @@ -127,4 +218,11 @@ # In[75]: +n = 100 +while(n<=300): + if n % 3 == 0 and n % 6 == 0: + print('El número es: ', str(n)) + break + n += 1 + diff --git a/M05_estructuradedatos/Prep_Course_Homework_05.ipynb b/M05_estructuradedatos/Prep_Course_Homework_05.ipynb index c71642967..d3e7bec36 100644 --- a/M05_estructuradedatos/Prep_Course_Homework_05.ipynb +++ b/M05_estructuradedatos/Prep_Course_Homework_05.ipynb @@ -21,7 +21,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos']\n", + "print(paises)" + ] }, { "attachments": {}, @@ -36,7 +39,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos']\n", + "print(paises[1])" + ] }, { "attachments": {}, @@ -51,7 +57,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos']\n", + "print(paises[1:5:1])" + ] }, { "attachments": {}, @@ -66,7 +75,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos']\n", + "print(type(paises))" + ] }, { "attachments": {}, @@ -81,7 +93,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos']\n", + "print(paises[2:])" + ] }, { "attachments": {}, @@ -96,7 +111,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos']\n", + "print(paises[:5])" + ] }, { "attachments": {}, @@ -111,7 +129,12 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos']\n", + "paises.append('Argentina')\n", + "paises.append('Alemania')\n", + "print(paises)" + ] }, { "attachments": {}, @@ -126,7 +149,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises.insert(3, 'Venezuela')\n", + "print(paises)" + ] }, { "attachments": {}, @@ -141,7 +167,11 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises_asia = ['japon', 'Korea', 'china', 'mongolia']\n", + "paises.extend(paises_asia)\n", + "print(paises)" + ] }, { "attachments": {}, @@ -156,7 +186,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(paises.index('Argentina'))" + ] }, { "attachments": {}, @@ -171,7 +203,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(paises.index('Uruguay'))" + ] }, { "attachments": {}, @@ -186,7 +220,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises.remove('japon')\n", + "print(paises)" + ] }, { "attachments": {}, @@ -201,7 +238,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "paises.remove('indonesia')\n", + "print(paises)" + ] }, { "attachments": {}, @@ -216,7 +256,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "last = paises.pop()\n", + "print(last)\n" + ] }, { "attachments": {}, @@ -231,7 +274,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(paises * 4)" + ] }, { "attachments": {}, @@ -246,7 +291,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "numeros = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)\n", + "print(numeros)" + ] }, { "attachments": {}, @@ -261,7 +309,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(numeros[9:15])" + ] }, { "attachments": {}, @@ -276,7 +326,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(20 in numeros)\n", + "print(30 in numeros)" + ] }, { "attachments": {}, @@ -291,7 +344,14 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "nuevo = 'francia'\n", + "if not nuevo in paises == True:\n", + " paises.append(nuevo)\n", + " print('Ingresa ', nuevo)\n", + "else:\n", + " print('ya esta en lista')" + ] }, { "attachments": {}, @@ -306,7 +366,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(numeros.count(9))\n", + "print(paises.count('Argentina'))" + ] }, { "attachments": {}, @@ -321,7 +384,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "numeros = list(numeros)\n", + "print(type(numeros))" + ] }, { "attachments": {}, @@ -336,7 +402,14 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "n1,n2,n3,n4,n5,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, = numeros\n", + "print(n1)\n", + "print(n2)\n", + "print(n3)\n", + "print(n4)\n", + "print(n5)" + ] }, { "attachments": {}, @@ -351,7 +424,12 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "diccio = { 'Pais': paises,\n", + " 'Ciudades': ['Bogota','Lima','Ciudad de mexico','Caracas','Berlin', 'Buenos Aires', 'Washinton','Bejgin'],\n", + " 'Continente': ['America', 'Europa', 'Asia']}\n", + "print(diccio)" + ] }, { "attachments": {}, @@ -366,7 +444,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(diccio.keys())" + ] }, { "attachments": {}, @@ -381,7 +461,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(diccio['Ciudades'])" + ] } ], "metadata": { @@ -403,7 +485,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/M05_estructuradedatos/Prep_Course_Homework_05.py b/M05_estructuradedatos/Prep_Course_Homework_05.py index 1d39ac89e..074ae7878 100644 --- a/M05_estructuradedatos/Prep_Course_Homework_05.py +++ b/M05_estructuradedatos/Prep_Course_Homework_05.py @@ -8,20 +8,23 @@ # In[3]: - +paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos'] +print(paises) # 2) Imprimir por pantalla el segundo elemento de la lista # In[4]: - +paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos'] +print(paises[1]) # 3) Imprimir por pantalla del segundo al cuarto elemento # In[8]: - +paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos'] +print(paises[1:5:1]) @@ -29,7 +32,8 @@ # In[12]: - +paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos'] +print(type(paises)) @@ -37,7 +41,8 @@ # In[14]: - +paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos'] +print(paises[2:]) @@ -45,7 +50,8 @@ # In[15]: - +paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos'] +print(paises[:5]) @@ -54,8 +60,10 @@ # In[16]: - - +paises = ['Colombia', 'Peru', 'Mexico', 'Alemania', 'Argentina', 'Estados unidos'] +paises.append('Argentina') +paises.append('Alemania') +print(paises) @@ -66,7 +74,8 @@ # In[20]: - +paises.insert(3, 'Venezuela') +print(paises) @@ -78,15 +87,16 @@ # 9) Concatenar otra lista a la ya creada # In[22]: - - +paises_asia = ['japon', 'Korea', 'china', 'mongolia'] +paises.extend(paises_asia) +print(paises) # 10) Encontrar el índice de la ciudad que en el punto 7 agregamos duplicada. ¿Se nota alguna particularidad? # In[23]: - +print(paises.index('Argentina')) @@ -94,6 +104,11 @@ # In[24]: +# print(paises.index('Uruguay')) +# +# print(paises.index('Uruguay')) +# ^^^^^^^^^^^^^^^^^^^^^^^ +# ValueError: 'Uruguay' is not in list @@ -102,7 +117,8 @@ # In[25]: - +paises.remove('japon') +print(paises) @@ -110,15 +126,19 @@ # In[27]: - - +# paises.remove('indonesia') +# print(paises) +# +# paises.remove('indonesia') +# ValueError: list.remove(x): x not in list # 14) Extraer el úlimo elemento de la lista, guardarlo en una variable e imprimirlo # In[28]: - +last = paises.pop() +print(last) @@ -126,28 +146,30 @@ # In[29]: - +print(paises * 4) # 16) Crear una tupla que contenga los números enteros del 1 al 20 # In[32]: - +numeros = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) +print(numeros) # 17) Imprimir desde el índice 10 al 15 de la tupla # In[35]: - +print(numeros[9:15]) # 18) Evaluar si los números 20 y 30 están dentro de la tupla # In[41]: - +print(20 in numeros) +print(30 in numeros) @@ -155,6 +177,12 @@ # In[48]: +nuevo = 'francia' +if not nuevo in paises == True: + paises.append(nuevo) + print('Ingresa ', nuevo) +else: + print('ya esta en lista') @@ -163,7 +191,8 @@ # In[51]: - +print(numeros.count(9)) +print(paises.count('Argentina')) @@ -171,7 +200,8 @@ # In[52]: - +numeros = list(numeros) +print(type(numeros)) @@ -180,7 +210,12 @@ # In[55]: - +n1,n2,n3,n4,n5,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, = numeros +print(n1) +print(n2) +print(n3) +print(n4) +print(n5) # 23) Crear un diccionario utilizando la lista crada en el punto 1, asignandole la clave "ciudad". Agregar tambien otras claves, como puede ser "Pais" y "Continente". @@ -188,7 +223,10 @@ # In[57]: - +diccio = { 'Pais': paises, + 'Ciudades': ['Bogota','Lima','Ciudad de mexico','Caracas','Berlin', 'Buenos Aires', 'Washinton','Bejgin'], + 'Continente': ['America', 'Europa', 'Asia']} +print(diccio) @@ -196,11 +234,11 @@ # In[59]: - +print(diccio.keys()) # 25) Imprimir las ciudades a través de su clave - +print(diccio['Ciudades']) # In[61]: diff --git a/M06_iterablesiteradores/Prep_Course_Homework_06.ipynb b/M06_iterablesiteradores/Prep_Course_Homework_06.ipynb index 75d6c18b1..3bce766d6 100644 --- a/M06_iterablesiteradores/Prep_Course_Homework_06.ipynb +++ b/M06_iterablesiteradores/Prep_Course_Homework_06.ipynb @@ -18,10 +18,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]\n" + ] + } + ], + "source": [ + "num_neg = []\n", + "numero = -15\n", + "while (numero < 0):\n", + " numero += 1 \n", + " num_neg.append(numero)\n", + "print(num_neg) " + ] }, { "attachments": {}, @@ -33,10 +48,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "19\n" + ] + } + ], + "source": [ + "lista_pares = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,19,20]\n", + "n = 0\n", + "while (n < len(lista_pares)):\n", + " if n % 2 != 0: \n", + " print(lista_pares[n])\n", + " n += 1" + ] }, { "attachments": {}, @@ -48,10 +85,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "19\n" + ] + } + ], + "source": [ + "lista_pares = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,19,20]\n", + "\n", + "for i in range(len(lista_pares)):\n", + " if i % 2 != 0:\n", + " print(lista_pares[i])" + ] }, { "attachments": {}, @@ -63,10 +121,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "for i in lista_pares[:3]:\n", + " print(i)" + ] }, { "attachments": {}, @@ -78,10 +149,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 1)\n", + "(1, 2)\n", + "(2, 3)\n", + "(3, 4)\n", + "(4, 5)\n", + "(5, 6)\n", + "(6, 7)\n", + "(7, 8)\n", + "(8, 9)\n", + "(9, 10)\n", + "(10, 11)\n", + "(11, 12)\n", + "(12, 13)\n", + "(13, 14)\n", + "(14, 15)\n", + "(15, 19)\n", + "(16, 20)\n" + ] + } + ], + "source": [ + "for i in enumerate(lista_pares):\n", + " print(i)" + ] }, { "attachments": {}, @@ -93,10 +191,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n" + ] + } + ], + "source": [ + "lista = [1,2,5,7,8,10,13,14,15,17,20]\n", + "\n", + "n = 1 \n", + "while (n <= 20):\n", + " if ( not (n in lista)):\n", + " lista.insert((n-1), n)\n", + " n += 1\n", + "print(lista)" + ] }, { "attachments": {}, @@ -112,10 +227,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229]\n" + ] + } + ], + "source": [ + "fibo = [0,1]\n", + "n = 2\n", + "while(n < 30):\n", + " fibo.append(fibo[n-1]+fibo[n-2])\n", + " n += 1\n", + "print(fibo)" + ] }, { "attachments": {}, @@ -127,10 +257,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "210\n" + ] + } + ], + "source": [ + "nuevalista = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n", + "sumaden = 0\n", + "for n in nuevalista:\n", + " sumaden += n\n", + "print(sumaden)" + ] }, { "attachments": {}, @@ -149,10 +293,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.1\n", + "1.0909090909090908\n", + "1.0833333333333333\n", + "1.0769230769230769\n", + "1.0714285714285714\n" + ] + } + ], + "source": [ + "nuevalista = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n", + "\n", + "primeros = 15\n", + "n = primeros - 5\n", + "while(n < primeros):\n", + " print(nuevalista[n]/nuevalista[n-1])\n", + " n += 1" + ] }, { "attachments": {}, @@ -165,10 +329,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "7\n" + ] + } + ], + "source": [ + "cadena = 'Hola Mundo'\n", + "\n", + "print(cadena.index('n'))\n", + "\n", + "for i, c in enumerate(cadena):\n", + " if c == 'n':\n", + " print(i)" + ] }, { "attachments": {}, @@ -180,10 +361,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys(['Continente', 'Paises', 'Ciudades'])\n", + "Continente\n", + "Paises\n", + "Ciudades\n" + ] + } + ], + "source": [ + "Dictiona = {'Continente': ['America', 'Oceania','Europa','Asia','Africa'],\n", + " 'Paises':['Colombia','Chile','Mexico','España','Egipto','Japon','Australia'],\n", + " 'Ciudades':['Bogota','Santiago','Ciudad de Mexico','Sidney','Tokio','Cairo','Madrid','Cancun','Barselona']\n", + "}\n", + "\n", + "print(Dictiona.keys())\n", + "for k in Dictiona:\n", + " print(k)" + ] }, { "attachments": {}, @@ -195,10 +396,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "o\n", + "l\n", + "a\n", + " \n", + "M\n", + "u\n", + "n\n", + "d\n", + "o\n" + ] + } + ], + "source": [ + "cadena = 'Hola Mundo'\n", + "lista_cadena = list(cadena)\n", + "\n", + "for l in lista_cadena:\n", + " print(l)" + ] }, { "attachments": {}, @@ -210,10 +434,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'i'), (10, 'j')]\n" + ] + } + ], + "source": [ + "la = [1,2,3,4,5,6,7,8,9,10]\n", + "lb = ['a','b','c','d','e','f','g','h','i','j','k','g']\n", + "t = zip(la, lb)\n", + "\n", + "print(list(t))" + ] }, { "attachments": {}, @@ -226,10 +464,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[21, 35, 42, 56, 63, 84, 91]\n" + ] + } + ], + "source": [ + "lis = [18,21,29,32,35,42,56,60,63,71,84,90,91,100]\n", + "numero_divi_7 = []\n", + "for s in lis:\n", + " if s % 7 == 0:\n", + " numero_divi_7.append(s)\n", + "print(numero_divi_7)" + ] }, { "attachments": {}, @@ -242,10 +495,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "lis = [[1,2,3,4],'rojo','verde',[True,False,False],['uno','dos','tres']]\n", + "\n", + "print(len(lis))" + ] }, { "attachments": {}, @@ -257,10 +522,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 2, 3, 4], 'rojo', 'verde', [True, False, False], ['uno', 'dos', 'tres'], ['rojo'], ['verde']]\n" + ] + } + ], + "source": [ + "lis = [[1,2,3,4],'rojo','verde',[True,False,False],['uno','dos','tres']]\n", + "lista_convertida = []\n", + "for e in lis:\n", + " if type(e) != list:\n", + " lis.append([e])\n", + "print(lis)" + ] } ], "metadata": { @@ -282,7 +562,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/M06_iterablesiteradores/Prep_Course_Homework_06.py b/M06_iterablesiteradores/Prep_Course_Homework_06.py index 85add52ab..eabdb3df3 100644 --- a/M06_iterablesiteradores/Prep_Course_Homework_06.py +++ b/M06_iterablesiteradores/Prep_Course_Homework_06.py @@ -7,15 +7,23 @@ # In[1]: - - +num_neg = [] +numero = -15 +while (numero < 0): + numero += 1 + num_neg.append(numero) +print(num_neg) # 2) ¿Con un ciclo while sería posible recorrer la lista para imprimir sólo los números pares? # In[3]: - - +lista_pares = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,19,20] +n = 0 +while (n < len(lista_pares)): + if n % 2 != 0: + print(lista_pares[n]) + n += 1 @@ -23,7 +31,11 @@ # In[4]: +lista_pares = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,19,20] +for i in range(len(lista_pares)): + if i % 2 != 0: + print(lista_pares[i]) @@ -31,6 +43,8 @@ # In[7]: +for i in lista_pares[:3]: + print(i) @@ -38,16 +52,22 @@ # In[9]: - +for i in enumerate(lista_pares): + print(i) # 6) Dada la siguiente lista de números enteros entre 1 y 20, crear un ciclo donde se completen los valores faltantes: lista = [1,2,5,7,8,10,13,14,15,17,20] # In[10]: +lista = [1,2,5,7,8,10,13,14,15,17,20] - - - +n = 1 +while (n <= 20): + if ( not (n in lista)): + lista.insert((n-1), n) + n += 1 +print(lista) + # In[11]: @@ -64,6 +84,12 @@ # In[23]: +lisnumero = [0,1] +indice = 2 +while indice < 30: + lisnumero.append(lisnumero[indice-1]+lisnumero[indice-2]) + n += 1 +print(lisnumero) @@ -72,7 +98,11 @@ # In[24]: - +nuevalista = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] +sumaden = 0 +for n in nuevalista: + sumaden += n +print(sumaden) # 9) La proporción aurea se expresa con una proporción matemática que nace el número irracional Phi= 1,618… que los griegos llamaron número áureo. El cuál se puede aproximar con la sucesión de Fibonacci. Con la lista del ejercicio anterior, imprimir el cociente de los últimos 5 pares de dos números contiguos:
@@ -85,16 +115,26 @@ # # In[38]: +nuevalista = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - - +primeros = 15 +n = primeros - 5 +while(n < primeros): + print(nuevalista[n]/nuevalista[n-1]) + n += 1 # 10) A partir de la variable cadena ya dada, mostrar en qué posiciones aparece la letra "n"
# cadena = 'Hola Mundo. Esto es una practica del lenguaje de programación Python' # In[39]: +cadena = 'Hola Mundo' +print(cadena.index('n')) + +for i, c in enumerate(cadena): + if c == 'n': + print(i) @@ -102,17 +142,23 @@ # In[40]: +Dictiona = {'Continente': ['America', 'Oceania','Europa','Asia','Africa'], + 'Paises':['Colombia','Chile','Mexico','España','Egipto','Japon','Australia'], + 'Ciudades':['Bogota','Santiago','Ciudad de Mexico','Sidney','Tokio','Cairo','Madrid','Cancun','Barselona'] +} - - - +print(Dictiona.keys()) +for k in Dictiona: + print(k) # 12) Convertir en una lista la variable "cadena" del punto 10 y luego recorrerla con un iterador # In[41]: +cadena = 'Hola Mundo' +lista_cadena = list(cadena) - - +for l in lista_cadena: + print(l) # In[45]: @@ -124,7 +170,11 @@ # In[48]: +la = [1,2,3,4,5,6,7,8,9,10] +lb = ['a','b','c','d','e','f','g','h','i','j','k','g'] +t = zip(la, lb) +print(list(t)) @@ -132,7 +182,13 @@ # lis = [18,21,29,32,35,42,56,60,63,71,84,90,91,100] # In[49]: - +lis = [18,21,29,32,35,42,56,60,63,71,84,90,91,100] +numero_divi_7 = [] +for s in lis: + if s % 7 == 0: + numero_divi_7.append(s) +print(numero_divi_7) + @@ -142,12 +198,16 @@ # In[56]: +lis = [[1,2,3,4],'rojo','verde',[True,False,False],['uno','dos','tres']] - +print(len(lis)) # In[51]: - +elemento = 0 +for e in lis: + elemento +=1 +print(elemento) @@ -161,5 +221,15 @@ # In[58]: +lis = [[1,2,3,4],'rojo','verde',[True,False,False],['uno','dos','tres']] +lista_convertida = [] +for e in lis: + if type(e) != list: + lis.append([e]) +print(lis) + + +# %% +# %% diff --git a/M07_funciones/Prep_Course_Homework_07-Resuelto.py b/M07_funciones/Prep_Course_Homework_07-Resuelto.py index 1a1239440..435ca9b4c 100644 --- a/M07_funciones/Prep_Course_Homework_07-Resuelto.py +++ b/M07_funciones/Prep_Course_Homework_07-Resuelto.py @@ -1,3 +1,4 @@ + #!/usr/bin/env python # coding: utf-8 @@ -208,3 +209,5 @@ def factorial(numero): print(factorial(1.23)) print(factorial('6')) + +# %% diff --git a/M07_funciones/Prep_Course_Homework_07.ipynb b/M07_funciones/Prep_Course_Homework_07.ipynb index d873f8157..833f543ab 100644 --- a/M07_funciones/Prep_Course_Homework_07.ipynb +++ b/M07_funciones/Prep_Course_Homework_07.ipynb @@ -21,7 +21,18 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def es_primo(numero):\n", + "\n", + " primo = True\n", + " for n in range(2,numero):\n", + " if numero % n == 0:\n", + " primo = False\n", + " break\n", + " return primo\n", + "\n", + "es_primo(6)" + ] }, { "attachments": {}, @@ -36,7 +47,18 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def lista_de_numero(listacomoleta):\n", + " lista_primos = []\n", + " for i in listacomoleta:\n", + " if es_primo(int(i)):\n", + " lista_primos.append(i)\n", + " return lista_primos\n", + "\n", + "listacomoleta = [1,2,3,4,5,6,7,8,9,10,11.12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]\n", + "\n", + "lista_de_numero(listacomoleta)" + ] }, { "attachments": {}, @@ -51,7 +73,27 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def cuenta_de_numeros_repetidos(lista):\n", + " conteo_numeros = {}\n", + " for num in lista:\n", + " if num in conteo_numeros:\n", + " conteo_numeros[num] +=1\n", + " else:\n", + " conteo_numeros[num] = 1\n", + " mas_reptido = ''\n", + " max_rep = 0\n", + "\n", + " for num, repeticiones in conteo_numeros.items():\n", + " if repeticiones > max_rep:\n", + " mas_reptido = num\n", + " max_rep = repeticiones\n", + " return mas_reptido, max_rep\n", + "\n", + "lista_num = [1,1,5,6,8,10,22,5,6,4,11,9,5]\n", + "conteo = cuenta_de_numeros_repetidos(lista_num)\n", + "print('el numero mas repetido es ', conteo[0], 'y se repite ', conteo[1], 'veces')" + ] }, { "attachments": {}, @@ -66,7 +108,37 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def cuenta_de_numeros_repetidos(lista, mayor = True):\n", + " conteo_numeros = {}\n", + " for num in lista:\n", + " if num in conteo_numeros:\n", + " conteo_numeros[num] +=1\n", + " else:\n", + " conteo_numeros[num] = 1\n", + " mas_reptido = ''\n", + " max_rep = 0\n", + "\n", + " for num, repeticiones in conteo_numeros.items():\n", + " if repeticiones > max_rep:\n", + " mas_reptido = num\n", + " max_rep = repeticiones\n", + " elif repeticiones == max_rep:\n", + " if mayor:\n", + " if num > mas_repetido:\n", + " mas_repetido = num\n", + " else:\n", + " if num < mas_repetido:\n", + " mas_repetido = num\n", + "\n", + " return mas_reptido, max_rep\n", + "\n", + "lista_num = [1,1,5,6,8,10,22,5,6,4,11,9,5,1,1,1,2]\n", + "conteo = cuenta_de_numeros_repetidos(lista_num, True)\n", + "print('el numero mas repetido es ', conteo[0], 'y se repite ', conteo[1], 'veces')\n", + "conteo = cuenta_de_numeros_repetidos(lista_num, False)\n", + "print('el numero menor mas repetido es ', conteo[0], 'y se repite ', conteo[1], 'veces')" + ] }, { "attachments": {}, @@ -84,7 +156,45 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def convert_grados (unidad, grados_origen , grados_final):\n", + " if grados_origen == 'celsius':\n", + " if grados_final == 'celsius':\n", + " grados_convert = unidad\n", + " elif grados_final == 'farenheit':\n", + " grados_convert = (unidad * 9/5) + 32\n", + " elif grados_final == 'kelvin':\n", + " grados_convert == (unidad + 273,15)\n", + " else:\n", + " print( 'parametro incorrecto')\n", + " elif grados_origen == 'farenheit':\n", + " if grados_final == 'farenheit':\n", + " grados_convert = unidad\n", + " elif grados_final == 'celsius':\n", + " grados_convert = (unidad - 32 ) * 5/9\n", + " elif grados_final == 'kelvin':\n", + " grados_convert == (unidad -32 )* 5/9 + 273,15\n", + " else:\n", + " print( 'parametro incorrecto')\n", + " elif grados_origen == 'kelvin':\n", + " if grados_final == 'kelvin':\n", + " grados_convert = unidad\n", + " elif grados_final == 'celsius':\n", + " grados_convert = (unidad - 273,15 ) \n", + " elif grados_final == 'farenheit':\n", + " grados_convert == (unidad - 273,15) * 9/5 +32\n", + " else:\n", + " print( 'parametro incorrecto')\n", + " else:\n", + " print('ingrese variable correcta ')\n", + "\n", + " return grados_convert\n", + "\n", + "\n", + "convertidor_caf = convert_grados(100, 'celsius', 'farenheit')\n", + "\n", + "print ('Celsius a Farenheit es : ', convertidor_caf)" + ] }, { "attachments": {}, @@ -99,7 +209,47 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def convert_grados (unidad, grados_origen , grados_final):\n", + " if grados_origen == 'celsius':\n", + " if grados_final == 'celsius':\n", + " grados_convert = unidad\n", + " elif grados_final == 'farenheit':\n", + " grados_convert = (unidad * 9/5) + 32\n", + " elif grados_final == 'kelvin':\n", + " grados_convert = unidad + 273.15\n", + " else:\n", + " print( 'parametro incorrecto')\n", + " elif grados_origen == 'farenheit':\n", + " if grados_final == 'farenheit':\n", + " grados_convert = unidad\n", + " elif grados_final == 'celsius':\n", + " grados_convert = (unidad - 32 ) * 5/9\n", + " elif grados_final == 'kelvin':\n", + " grados_convert = ((unidad -32 )* 5/9) + 273.15\n", + " else:\n", + " print( 'parametro incorrecto')\n", + " elif grados_origen == 'kelvin':\n", + " if grados_final == 'kelvin':\n", + " grados_convert = unidad\n", + " elif grados_final == 'celsius':\n", + " grados_convert = (unidad - 273.15 ) \n", + " elif grados_final == 'farenheit':\n", + " grados_convert = ((unidad - 273.15) * 9/5) +32\n", + " else:\n", + " print( 'parametro incorrecto')\n", + " else:\n", + " print('ingrese variable correcta ')\n", + "\n", + " return grados_convert\n", + "\n", + "unidades_tem = ['celsius', 'farenheit','kelvin']\n", + "\n", + "for i in range(0,3):\n", + " for j in range(0,3):\n", + " #print(i, j)\n", + " print('por cada grado de ', unidades_tem[i], 'equivale a ', unidades_tem[j], convert_grados(1,unidades_tem[i], unidades_tem[j]))" + ] }, { "attachments": {}, @@ -114,7 +264,23 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def num_fact(numero):\n", + " if (type(numero) != int):\n", + " return 'ingresa un numero entero'\n", + " if (numero < 0):\n", + " return 'ingresa un numero positivo'\n", + " if (numero > 1):\n", + " numero = numero * num_fact(numero - 1)\n", + " return numero\n", + "\n", + "\n", + "print(num_fact(3))\n", + "print(num_fact(-2))\n", + "print(num_fact(1.2))\n", + "print(num_fact(23))\n", + "print(num_fact('d'))" + ] } ], "metadata": { diff --git a/M07_funciones/Prep_Course_Homework_07.py b/M07_funciones/Prep_Course_Homework_07.py index af0f4d616..6d20488d3 100644 --- a/M07_funciones/Prep_Course_Homework_07.py +++ b/M07_funciones/Prep_Course_Homework_07.py @@ -8,28 +8,102 @@ # In[1]: +def es_primo(numero): + primo = True + for n in range(2,numero): + if numero % n == 0: + primo = False + break + return primo + +es_primo(6) + # 2) Utilizando la función del punto 1, realizar otra función que reciba de parámetro una lista de números y devuelva sólo aquellos que son primos en otra lista # In[25]: +# def es_primo(numero): + +# primo = True +# for n in range(2,numero): +# if numero % n == 0: +# primo = False +# print('No es primo') +# break +# return primo +def lista_de_numero(listacomoleta): + lista_primos = [] + for i in listacomoleta: + if es_primo(int(i)): + lista_primos.append(i) + return lista_primos +listacomoleta = [1,2,3,4,5,6,7,8,9,10,11.12,13,14,15,16,17,18,19,20,21,22,23,24,25,26] +lista_de_numero(listacomoleta) # 3) Crear una función que al recibir una lista de números, devuelva el que más se repite y cuántas veces lo hace. Si hay más de un "más repetido", que devuelva cualquiera + # In[33]: +def cuenta_de_numeros_repetidos(lista): + conteo_numeros = {} + for num in lista: + if num in conteo_numeros: + conteo_numeros[num] +=1 + else: + conteo_numeros[num] = 1 + mas_reptido = '' + max_rep = 0 + for num, repeticiones in conteo_numeros.items(): + if repeticiones > max_rep: + mas_reptido = num + max_rep = repeticiones + return mas_reptido, max_rep + +lista_num = [1,1,5,6,8,10,22,5,6,4,11,9,5] +conteo = cuenta_de_numeros_repetidos(lista_num) +print('el numero mas repetido es ', conteo[0], 'y se repite ', conteo[1], 'veces') # 4) A la función del punto 3, agregar un parámetro más, que permita elegir si se requiere el menor o el mayor de los mas repetidos. # In[45]: - +def cuenta_de_numeros_repetidos(lista, mayor = True): + conteo_numeros = {} + for num in lista: + if num in conteo_numeros: + conteo_numeros[num] +=1 + else: + conteo_numeros[num] = 1 + mas_reptido = '' + max_rep = 0 + + for num, repeticiones in conteo_numeros.items(): + if repeticiones > max_rep: + mas_reptido = num + max_rep = repeticiones + elif repeticiones == max_rep: + if mayor: + if num > mas_repetido: + mas_repetido = num + else: + if num < mas_repetido: + mas_repetido = num + + return mas_reptido, max_rep + +lista_num = [1,1,5,6,8,10,22,5,6,4,11,9,5,1,1,1,2] +conteo = cuenta_de_numeros_repetidos(lista_num, True) +print('el numero mas repetido es ', conteo[0], 'y se repite ', conteo[1], 'veces') +conteo = cuenta_de_numeros_repetidos(lista_num, False) +print('el numero menor mas repetido es ', conteo[0], 'y se repite ', conteo[1], 'veces') # 5) Crear una función que convierta entre grados Celsius, Farenheit y Kelvin
@@ -40,13 +114,88 @@ # In[56]: +def convert_grados (unidad, grados_origen , grados_final): + if grados_origen == 'celsius': + if grados_final == 'celsius': + grados_convert = unidad + elif grados_final == 'farenheit': + grados_convert = (unidad * 9/5) + 32 + elif grados_final == 'kelvin': + grados_convert == (unidad + 273,15) + else: + print( 'parametro incorrecto') + elif grados_origen == 'farenheit': + if grados_final == 'farenheit': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 32 ) * 5/9 + elif grados_final == 'kelvin': + grados_convert == (unidad -32 )* 5/9 + 273,15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'kelvin': + if grados_final == 'kelvin': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 273,15 ) + elif grados_final == 'farenheit': + grados_convert == (unidad - 273,15) * 9/5 +32 + else: + print( 'parametro incorrecto') + else: + print('ingrese variable correcta ') + + return grados_convert + + +convertidor_caf = convert_grados(100, 'celsius', 'farenheit') + +print ('Celsius a Farenheit es : ', convertidor_caf) + # 6) Iterando una lista con los tres valores posibles de temperatura que recibe la función del punto 5, hacer un print para cada combinación de los mismos: # In[62]: - - +def convert_grados (unidad, grados_origen , grados_final): + if grados_origen == 'celsius': + if grados_final == 'celsius': + grados_convert = unidad + elif grados_final == 'farenheit': + grados_convert = (unidad * 9/5) + 32 + elif grados_final == 'kelvin': + grados_convert = unidad + 273.15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'farenheit': + if grados_final == 'farenheit': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 32 ) * 5/9 + elif grados_final == 'kelvin': + grados_convert = ((unidad -32 )* 5/9) + 273.15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'kelvin': + if grados_final == 'kelvin': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 273.15 ) + elif grados_final == 'farenheit': + grados_convert = ((unidad - 273.15) * 9/5) +32 + else: + print( 'parametro incorrecto') + else: + print('ingrese variable correcta ') + + return grados_convert + +unidades_tem = ['celsius', 'farenheit','kelvin'] + +for i in range(0,3): + for j in range(0,3): + #print(i, j) + print('por cada grado de ', unidades_tem[i], 'equivale a ', unidades_tem[j], convert_grados(1,unidades_tem[i], unidades_tem[j])) # 7) Armar una función que devuelva el factorial de un número. Tener en cuenta que el usuario puede equivocarse y enviar de parámetro un número no entero o negativo @@ -54,5 +203,20 @@ # In[65]: +def num_fact(numero): + if (type(numero) != int): + return 'ingresa un numero entero' + if (numero < 0): + return 'ingresa un numero positivo' + if (numero > 1): + numero = numero * num_fact(numero - 1) + return numero + +print(num_fact(3)) +print(num_fact(-2)) +print(num_fact(1.2)) +print(num_fact(23)) +print(num_fact('d')) +# %% diff --git a/M08_clasesyOOP/Prep_Course_Homework_08.py b/M08_clasesyOOP/Prep_Course_Homework_08.py index 5a3b936ee..d7191077d 100644 --- a/M08_clasesyOOP/Prep_Course_Homework_08.py +++ b/M08_clasesyOOP/Prep_Course_Homework_08.py @@ -10,6 +10,11 @@ # In[1]: +class Vehiculo: + def __init__(self, color, tipo, cilindra): + self.color = color + self.tipo = tipo + self.cilindra = cilindra @@ -21,12 +26,54 @@ # In[5]: +class Vehiculo: + def __init__(self, color, tipo, cilindra): + self.color = color + self.tipo = tipo + self.cilindra = cilindra + self.velocidad = 0 + self.direccion = 0 + + def acelerar(self,vel): + self.velocidad += vel + + def frenar(self,vel): + self.velocidad -= vel + + def doblar(self,grados): + self.direccion += grados + # 3) Instanciar 3 objetos de la clase vehículo y ejecutar sus métodos, probar luego el resultado # In[6]: +class Vehiculo: + def __init__(self, color, tipo, cilindra): + self.color = color + self.tipo = tipo + self.cilindra = cilindra + self.velocidad = 0 + self.direccion = 0 + + def acelerar(self,vel): + self.velocidad += vel + + def frenar(self,vel): + self.velocidad -= vel + + def doblar(self,grados): + self.direccion += grados +car1 = Vehiculo('rojo','carro',1.6) +car2 = Vehiculo('negro','camion',3.5) +car3 = Vehiculo('blanco','moto',1) + + +print(car1.color, car1.tipo, car1.cilindra) + +print(car2.acelerar(40)) + @@ -35,7 +82,37 @@ # 4) Agregar a la clase Vehiculo, un método que muestre su estado, es decir, a que velocidad se encuentra y su dirección. Y otro método que muestre color, tipo y cilindrada # In[12]: +class Vehiculo: + def __init__(self, color, tipo, cilindra): + self.color = color + self.tipo = tipo + self.cilindra = cilindra + self.velocidad = 0 + self.direccion = 0 + + def acelerar(self,vel): + self.velocidad += vel + + def frenar(self,vel): + self.velocidad -= vel + + def doblar(self,grados): + self.direccion += grados + + def estado(self): + print('Velocidad', self.velocidad, 'Gira', self.direccion) + + def caracteristica(self): + print('el vehiculo es ', self.tipo, 'de color ', self.color, 'de cilindraje', self.cilindra) + + +car1 = Vehiculo('rojo','carro',1.6) +car2 = Vehiculo('negro','camion',3.5) +car3 = Vehiculo('blanco','moto',1) +car1.caracteristica() +car2.caracteristica() +car3.caracteristica() @@ -56,14 +133,181 @@ # In[33]: - - - - - +class Funciones_7: + + def __init__ (self): + pass + def es_primo(self,numero): + + primo = True + for n in range(2,numero): + if numero % n == 0: + primo = False + break + return primo + + def cuenta_de_numeros_repetidos(self,lista, mayor = True): + conteo_numeros = {} + for num in lista: + if num in conteo_numeros: + conteo_numeros[num] +=1 + else: + conteo_numeros[num] = 1 + mas_reptido = '' + max_rep = 0 + + for num, repeticiones in conteo_numeros.items(): + if repeticiones > max_rep: + mas_reptido = num + max_rep = repeticiones + elif repeticiones == max_rep: + if mayor: + if num > mas_repetido: + mas_repetido = num + else: + if num < mas_repetido: + mas_repetido = num + + return mas_reptido, max_rep + + def convert_grados (self, unidad, grados_origen , grados_final): + if grados_origen == 'celsius': + if grados_final == 'celsius': + grados_convert = unidad + elif grados_final == 'farenheit': + grados_convert = (unidad * 9/5) + 32 + elif grados_final == 'kelvin': + grados_convert == (unidad + 273,15) + else: + print( 'parametro incorrecto') + elif grados_origen == 'farenheit': + if grados_final == 'farenheit': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 32 ) * 5/9 + elif grados_final == 'kelvin': + grados_convert == (unidad -32 )* 5/9 + 273,15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'kelvin': + if grados_final == 'kelvin': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 273,15 ) + elif grados_final == 'farenheit': + grados_convert == (unidad - 273,15) * 9/5 +32 + else: + print( 'parametro incorrecto') + else: + print('ingrese variable correcta ') + + return grados_convert + + def num_fact(self,numero): + if (type(numero) != int): + return 'ingresa un numero entero' + if (numero < 0): + return 'ingresa un numero positivo' + if (numero > 1): + numero = numero * num_fact(numero - 1) + return numero + + +#print('el numero mas repetido es ', f.cuenta_de_numeros_repetidos[0], 'y se repite ', f.cuenta_de_numeros_repetidos[1], 'veces') # 6) Probar las funciones incorporadas en la clase del punto 5 + # In[28]: +class Funciones_7: + + def __init__ (self): + pass + def es_primo(self,numero): + + primo = True + for n in range(2,numero): + if numero % n == 0: + primo = False + break + return primo + + def cuenta_de_numeros_repetidos(self,lista, mayor = True): + conteo_numeros = {} + for num in lista: + if num in conteo_numeros: + conteo_numeros[num] +=1 + else: + conteo_numeros[num] = 1 + mas_reptido = '' + max_rep = 0 + + for num, repeticiones in conteo_numeros.items(): + if repeticiones > max_rep: + mas_reptido = num + max_rep = repeticiones + elif repeticiones == max_rep: + if mayor: + if num > mas_repetido: + mas_repetido = num + else: + if num < mas_repetido: + mas_repetido = num + + return mas_reptido, max_rep + + def convert_grados (self, unidad, grados_origen , grados_final): + if grados_origen == 'celsius': + if grados_final == 'celsius': + grados_convert = unidad + elif grados_final == 'farenheit': + grados_convert = (unidad * 9/5) + 32 + elif grados_final == 'kelvin': + grados_convert = (unidad + 273,15) + else: + print( 'parametro incorrecto') + elif grados_origen == 'farenheit': + if grados_final == 'farenheit': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 32 ) * 5/9 + elif grados_final == 'kelvin': + grados_convert = (unidad -32 )* 5/9 + 273,15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'kelvin': + if grados_final == 'kelvin': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 273,15 ) + elif grados_final == 'farenheit': + grados_convert =(unidad - 273,15) * 9/5 +32 + else: + print( 'parametro incorrecto') + else: + print('ingrese variable correcta ') + + return grados_convert + + def num_fact(self,numero): + if (type(numero) != int): + return 'ingresa un numero entero' + if (numero < 0): + return 'ingresa un numero positivo' + if (numero > 1): + numero = numero * self.num_fact(numero - 1) + return numero + +f = Funciones_7() + +f.es_primo(13) + +lista_num = [1,1,5,6,8,10,22,5,6,4,11,9,5,1,1,1,2] +moda, repe = f.cuenta_de_numeros_repetidos(lista_num, True) +print(' el valor de ', moda, ' y se repte ', repe ) + +f.convert_grados(19,'celsius','kelvin') + +f.num_fact(8) @@ -73,13 +317,117 @@ # In[55]: +class Funciones_7: + + def __init__ (self, lista_de_numeros): + self.lista = lista_de_numeros + pass + def es_primo(self): + primo = True + for i in self.lista: + for n in range(2,i): + if i % n == 0: + primo = False + print(i, 'no es numero primo') + else: + (i, 'es numero primo') + def convert_grados(self, grados_origen, grados_destino): + for i in self.lista: + print(i, 'grados',grados_origen, ' es igual a', self.__convert_grados(i,grados_origen,grados_destino),'grados',grados_destino) + + def num_fact (self): + for i in self.lista: + print('numero fact de ', i ,' es', self.__num_fact(i)) + + def __es_primo(self,numero): + primo = True + for n in range(2,numero): + if numero % n == 0: + primo = False + break + return primo + + def __cuenta_de_numeros_repetidos(self,lista, mayor = True): + conteo_numeros = {} + for num in lista: + if num in conteo_numeros: + conteo_numeros[num] +=1 + else: + conteo_numeros[num] = 1 + mas_reptido = '' + max_rep = 0 + + for num, repeticiones in conteo_numeros.items(): + if repeticiones > max_rep: + mas_reptido = num + max_rep = repeticiones + elif repeticiones == max_rep: + if mayor: + if num > mas_repetido: + mas_repetido = num + else: + if num < mas_repetido: + mas_repetido = num + + return mas_reptido, max_rep + + def __convert_grados (self, unidad, grados_origen , grados_final): + if grados_origen == 'celsius': + if grados_final == 'celsius': + grados_convert = unidad + elif grados_final == 'farenheit': + grados_convert = (unidad * 9/5) + 32 + elif grados_final == 'kelvin': + grados_convert = (unidad + 273,15) + else: + print( 'parametro incorrecto') + elif grados_origen == 'farenheit': + if grados_final == 'farenheit': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 32 ) * 5/9 + elif grados_final == 'kelvin': + grados_convert = (unidad -32 )* 5/9 + 273,15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'kelvin': + if grados_final == 'kelvin': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 273,15 ) + elif grados_final == 'farenheit': + grados_convert = (unidad - 273,15) * 9/5 +32 + else: + print( 'parametro incorrecto') + else: + print('ingrese variable correcta ') + + return grados_convert + + def __num_fact(self,numero): + if (type(numero) != int): + return 'ingresa un numero entero' + if (numero < 0): + return 'ingresa un numero positivo' + if (numero > 1): + numero = numero * self.__num_fact(numero - 1) + return numero +f = Funciones_7([1,4,5,6,8,10,22,5,6,4,11,9,5,1,1,1,2]) +f.es_primo() +f.convert_grados('celsius','farenheit') +f.num_fact() +#f.convert_grados(lista_num,'celsius','farenheit') +# 8) Crear un archivo .py aparte y ubicar allí la clase generada en el punto anterior. Luego realizar la importación del módulo y probar alguna de sus funciones +# In[1]: +from fun07 import * -# 8) Crear un archivo .py aparte y ubicar allí la clase generada en el punto anterior. Luego realizar la importación del módulo y probar alguna de sus funciones +f2 = Funciones_7([1,3,5,7,9,3,4,]) -# In[1]: +f2.es_primo() +# %% diff --git a/M08_clasesyOOP/fun07.py b/M08_clasesyOOP/fun07.py new file mode 100644 index 000000000..686007a6b --- /dev/null +++ b/M08_clasesyOOP/fun07.py @@ -0,0 +1,95 @@ +class Funciones_7: + + def __init__ (self, lista_de_numeros): + self.lista = lista_de_numeros + pass + def es_primo(self): + primo = True + for i in self.lista: + for n in range(2,i): + if i % n == 0: + primo = False + print(i, 'no es numero primo') + else: + (i, 'es numero primo') + def convert_grados(self, grados_origen, grados_destino): + for i in self.lista: + print(i, 'grados',grados_origen, ' es igual a', self.__convert_grados(i,grados_origen,grados_destino),'grados',grados_destino) + + def num_fact (self): + for i in self.lista: + print('numero fact de ', i ,' es', self.__num_fact(i)) + + def __es_primo(self,numero): + primo = True + for n in range(2,numero): + if numero % n == 0: + primo = False + break + return primo + + def __cuenta_de_numeros_repetidos(self,lista, mayor = True): + conteo_numeros = {} + for num in lista: + if num in conteo_numeros: + conteo_numeros[num] +=1 + else: + conteo_numeros[num] = 1 + mas_reptido = '' + max_rep = 0 + + for num, repeticiones in conteo_numeros.items(): + if repeticiones > max_rep: + mas_reptido = num + max_rep = repeticiones + elif repeticiones == max_rep: + if mayor: + if num > mas_repetido: + mas_repetido = num + else: + if num < mas_repetido: + mas_repetido = num + + return mas_reptido, max_rep + + def __convert_grados (self, unidad, grados_origen , grados_final): + if grados_origen == 'celsius': + if grados_final == 'celsius': + grados_convert = unidad + elif grados_final == 'farenheit': + grados_convert = (unidad * 9/5) + 32 + elif grados_final == 'kelvin': + grados_convert = (unidad + 273,15) + else: + print( 'parametro incorrecto') + elif grados_origen == 'farenheit': + if grados_final == 'farenheit': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 32 ) * 5/9 + elif grados_final == 'kelvin': + grados_convert = (unidad -32 )* 5/9 + 273,15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'kelvin': + if grados_final == 'kelvin': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 273,15 ) + elif grados_final == 'farenheit': + grados_convert = (unidad - 273,15) * 9/5 +32 + else: + print( 'parametro incorrecto') + else: + print('ingrese variable correcta ') + + return grados_convert + + def __num_fact(self,numero): + if (type(numero) != int): + return 'ingresa un numero entero' + if (numero < 0): + return 'ingresa un numero positivo' + if (numero > 1): + numero = numero * self.__num_fact(numero - 1) + return numero \ No newline at end of file diff --git a/M09_errorhandling/Prep_Course_Homework_09.ipynb b/M09_errorhandling/Prep_Course_Homework_09.ipynb index e3e771b0b..77a27aaf6 100644 --- a/M09_errorhandling/Prep_Course_Homework_09.ipynb +++ b/M09_errorhandling/Prep_Course_Homework_09.ipynb @@ -21,7 +21,15 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import sys \n", + "\n", + "sys.path.append(r'/Users/macbookpro/Documents/Programacion /Python /Repositorio hub /Python-Prep/M09_errorhandling/fun07.py')\n", + "\n", + "import fun07 as f \n", + "\n", + "f1 = f.Funciones_7(1,2,3,4,5)" + ] }, { "attachments": {}, @@ -36,7 +44,18 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import fun07 as f \n", + "import importlib\n", + "\n", + "importlib.reload(f)\n", + "\n", + "f1 = f.Funciones_7([1,2,3,4,5])\n", + "\n", + "#f1.convert_grados(1,2)\n", + "\n", + "f1.convert_grados('celsius','farenheit')" + ] }, { "attachments": {}, @@ -56,7 +75,32 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import fun07 as f\n", + "import unittest\n", + "\n", + "class Pruebas (unittest.TestCase):\n", + "\n", + " def test_prueba_crear_objeto(self):\n", + " objeto = 'producto'\n", + " self.assertRaises(ValueError,f.Funciones_7,objeto)\n", + " def test_prueba_crear_objeto_correcta(self):\n", + " objeto = [1,2,3,4,5,2]\n", + " f1 = f.Funciones_7(objeto)\n", + " self.assertAlmostEqual(f1.lista, objeto)\n", + " def test_valor_primos(self):\n", + " lis = [1,2,3,4,1,6,7,8]\n", + " f1 = f.Funciones_7(lis)\n", + " mas_repetido , max_rep = f1.__cuenta_de_numeros_repetidos(False)\n", + " mas_repetido = [mas_repetido]\n", + " mas_repetido.append(max_rep)\n", + " result = [1,2]\n", + " self.assertEqual(mas_repetido, result, 'no es igual')\n", + "\n", + "\n", + "\n", + "unittest.main(argv=[''], verbosity=2, exit=False)" + ] }, { "attachments": {}, @@ -71,7 +115,13 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import unittest\n", + "\n", + "f2 = f.Funciones_7('numero')\n", + "\n", + "unittest.main(argv=[''], verbosity=2, exit=False)" + ] }, { "attachments": {}, @@ -86,7 +136,18 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Test_probando_primos (unittest.TestCase):\n", + "\n", + " def test_verifica_primos(self):\n", + " lis = [1,2,3,4,5,6,7]\n", + " f1 = f.Funciones_7(lis)\n", + " primos = f1.es_primo()\n", + " primos_esperado = [True,False,True,False,False]\n", + " self.assertEqual(primos, primos_esperado)\n", + "\n", + "unittest.main(argv=[''], verbosity=2, exit=False)" + ] }, { "attachments": {}, @@ -101,7 +162,18 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Test_probando_grados(unittest.TestCase):\n", + "\n", + " def test_verifica_grados(self):\n", + " lis = [1,2,33,4,14]\n", + " f1 = f.Funciones_7(lis)\n", + " grados = f1.convert_grados('celsius','farenheit')\n", + " grados_esperado = [32,45,33,12,11]\n", + " self.assertEqual(grados, grados_esperado)\n", + "\n", + " unittest.main(argv=[''], verbosity=2 , exit=False)" + ] }, { "attachments": {}, @@ -113,10 +185,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'unittest' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mTest_probando_fact\u001b[39;00m(\u001b[43munittest\u001b[49m\u001b[38;5;241m.\u001b[39mTestCase):\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtest_verifica_fafct\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m 4\u001b[0m lis \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m33\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m14\u001b[39m]\n", + "\u001b[0;31mNameError\u001b[0m: name 'unittest' is not defined" + ] + } + ], + "source": [ + "import unittest\n", + "class Test_probando_fact(unittest.TestCase):\n", + "\n", + " def test_verifica_fafct(self):\n", + " lis = [1,2,33,4,14]\n", + " f1 = f.Funciones_7(lis)\n", + " num_fact = f1.num_fact()\n", + " fact_esperado = [32,45,33,12,11]\n", + " self.assertEqual(num_fact, fact_esperado)\n", + "\n", + " unittest.main(argv=[''], verbosity=2 , exit=False)" + ] } ], "metadata": { @@ -138,7 +234,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/M09_errorhandling/Prep_Course_Homework_09.py b/M09_errorhandling/Prep_Course_Homework_09.py index 837b0a4d9..18cd6964c 100644 --- a/M09_errorhandling/Prep_Course_Homework_09.py +++ b/M09_errorhandling/Prep_Course_Homework_09.py @@ -8,13 +8,28 @@ # In[1]: +import sys + +sys.path.append(r'/Users/macbookpro/Documents/Programacion /Python /Repositorio hub /Python-Prep/M09_errorhandling/fun07.py') + +import fun07 as f + +f1 = f.Funciones_7(1,2,3,4,5) # 2) En la función que hace la conversión de grados, validar que los parámetros enviados sean los esperados, de no serlo, informar cuáles son los valores esperados. # In[5]: +import fun07 as f +import importlib + +importlib.reload(f) +f1 = f.Funciones_7([1,2,3,4,5]) +#f1.convert_grados(1,2) + +f1.convert_grados('celsius','farenheit') @@ -27,27 +42,72 @@ # In[9]: +import fun07 as f +import unittest + +class Pruebas (unittest.TestCase): + + def test_prueba_crear_objeto(self): + objeto = 'producto' + self.assertRaises(ValueError,f.Funciones_7,objeto) + def test_prueba_crear_objeto_correcta(self): + objeto = [1,2,3,4,5,2] + f1 = f.Funciones_7(objeto) + self.assertAlmostEqual(f1.lista, objeto) + def test_valor_primos(self): + lis = [1,2,3,4,1,6,7,8] + f1 = f.Funciones_7(lis) + mas_repetido , max_rep = f1.__cuenta_de_numeros_repetidos(False) + mas_repetido = [mas_repetido] + mas_repetido.append(max_rep) + result = [1,2] + self.assertEqual(mas_repetido, result, 'no es igual') + +unittest.main(argv=[''], verbosity=2, exit=False) + +# if __name__ == '__main__': +# unittest.main() # 4) Probar una creación incorrecta y visualizar la salida del "raise" # In[13]: +import unittest +f2 = f.Funciones_7('numero') +unittest.main(argv=[''], verbosity=2, exit=False) # 6) Agregar casos de pruebas para el método verifica_primos() realizando el cambio en la clase, para que devuelva una lista de True o False en función de que el elemento en la posisicón sea o no primo # In[14]: +class Test_probando_primos (unittest.TestCase): + def test_verifica_primos(self): + lis = [1,2,3,4,5,6,7] + f1 = f.Funciones_7(lis) + primos = f1.es_primo() + primos_esperado = [True,False,True,False,False] + self.assertEqual(primos, primos_esperado) - +unittest.main(argv=[''], verbosity=2, exit=False) # 7) Agregar casos de pruebas para el método conversion_grados() # In[17]: +class Test_probando_grados(unittest.TestCase): + + def test_verifica_grados(self): + lis = [1,2,33,4,14] + f1 = f.Funciones_7(lis) + grados = f1.convert_grados('celsius','farenheit') + grados_esperado = [32,45,33,12,11] + self.assertEqual(grados, grados_esperado) + + unittest.main(argv=[''], verbosity=2 , exit=False) @@ -55,6 +115,18 @@ # In[20]: +class Test_probando_fact(unittest.TestCase): + + def test_verifica_fafct(self): + lis = [1,2,33,4,14] + f1 = f.Funciones_7(lis) + num_fact = f1.num_fact() + fact_esperado = [32,45,33,12,11] + self.assertEqual(num_fact, fact_esperado) + + unittest.main(argv=[''], verbosity=2 , exit=False) + +# %% diff --git a/M09_errorhandling/fun07.py b/M09_errorhandling/fun07.py new file mode 100644 index 000000000..686007a6b --- /dev/null +++ b/M09_errorhandling/fun07.py @@ -0,0 +1,95 @@ +class Funciones_7: + + def __init__ (self, lista_de_numeros): + self.lista = lista_de_numeros + pass + def es_primo(self): + primo = True + for i in self.lista: + for n in range(2,i): + if i % n == 0: + primo = False + print(i, 'no es numero primo') + else: + (i, 'es numero primo') + def convert_grados(self, grados_origen, grados_destino): + for i in self.lista: + print(i, 'grados',grados_origen, ' es igual a', self.__convert_grados(i,grados_origen,grados_destino),'grados',grados_destino) + + def num_fact (self): + for i in self.lista: + print('numero fact de ', i ,' es', self.__num_fact(i)) + + def __es_primo(self,numero): + primo = True + for n in range(2,numero): + if numero % n == 0: + primo = False + break + return primo + + def __cuenta_de_numeros_repetidos(self,lista, mayor = True): + conteo_numeros = {} + for num in lista: + if num in conteo_numeros: + conteo_numeros[num] +=1 + else: + conteo_numeros[num] = 1 + mas_reptido = '' + max_rep = 0 + + for num, repeticiones in conteo_numeros.items(): + if repeticiones > max_rep: + mas_reptido = num + max_rep = repeticiones + elif repeticiones == max_rep: + if mayor: + if num > mas_repetido: + mas_repetido = num + else: + if num < mas_repetido: + mas_repetido = num + + return mas_reptido, max_rep + + def __convert_grados (self, unidad, grados_origen , grados_final): + if grados_origen == 'celsius': + if grados_final == 'celsius': + grados_convert = unidad + elif grados_final == 'farenheit': + grados_convert = (unidad * 9/5) + 32 + elif grados_final == 'kelvin': + grados_convert = (unidad + 273,15) + else: + print( 'parametro incorrecto') + elif grados_origen == 'farenheit': + if grados_final == 'farenheit': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 32 ) * 5/9 + elif grados_final == 'kelvin': + grados_convert = (unidad -32 )* 5/9 + 273,15 + else: + print( 'parametro incorrecto') + elif grados_origen == 'kelvin': + if grados_final == 'kelvin': + grados_convert = unidad + elif grados_final == 'celsius': + grados_convert = (unidad - 273,15 ) + elif grados_final == 'farenheit': + grados_convert = (unidad - 273,15) * 9/5 +32 + else: + print( 'parametro incorrecto') + else: + print('ingrese variable correcta ') + + return grados_convert + + def __num_fact(self,numero): + if (type(numero) != int): + return 'ingresa un numero entero' + if (numero < 0): + return 'ingresa un numero positivo' + if (numero > 1): + numero = numero * self.__num_fact(numero - 1) + return numero \ No newline at end of file