diff --git a/Archivos_clases_en_vivo/Prep_Course_Homework_05.ipynb b/Archivos_clases_en_vivo/Prep_Course_Homework_05.ipynb new file mode 100644 index 000000000..078194645 --- /dev/null +++ b/Archivos_clases_en_vivo/Prep_Course_Homework_05.ipynb @@ -0,0 +1,769 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Estructuras de Datos" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1) Crear una lista que contenga nombres de ciudades del mundo que contenga más de 5 elementos e imprimir por pantalla" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['París', 'Londres', 'Barcelona', 'Roma', 'Amsterdam', 'Praga']\n" + ] + } + ], + "source": [ + "lis = [\"París\", \"Londres\", \"Barcelona\", \"Roma\", \"Amsterdam\", \"Praga\"]\n", + "print(lis)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2) Imprimir por pantalla el segundo elemento de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Londres\n" + ] + } + ], + "source": [ + "print(lis[1])" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3) Imprimir por pantalla del segundo al cuarto elemento" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Londres', 'Barcelona', 'Roma']\n" + ] + } + ], + "source": [ + "print(lis[1:4])" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4) Visualizar el tipo de dato de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(lis)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5) Visualizar todos los elementos de la lista a partir del tercero de manera genérica, es decir, sin explicitar la posición del último elemento" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Barcelona', 'Roma', 'Amsterdam', 'Praga']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lis[2:]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6) Visualizar los primeros 4 elementos de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['París', 'Londres', 'Barcelona', 'Roma']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lis[:4]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "7) Agregar una ciudad más a la lista que ya exista y otra que no ¿Arroja algún tipo de error?" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "lis.append(\"Barcelona\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "lis.append(\"Berlin\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['París',\n", + " 'Londres',\n", + " 'Barcelona',\n", + " 'Roma',\n", + " 'Amsterdam',\n", + " 'Praga',\n", + " 'Barcelona',\n", + " 'Berlin']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lis" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8) Agregar otra ciudad, pero en la cuarta posición" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "lis.insert(3, \"Helsinki\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin']\n" + ] + } + ], + "source": [ + "print(lis)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "9) Concatenar otra lista a la ya creada" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin', 'Bogotá', 'Buenos Aires', 'Ciudad México']\n" + ] + } + ], + "source": [ + "lis.extend([\"Bogotá\", \"Buenos Aires\", \"Ciudad México\"])\n", + "print(lis)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "10) Encontrar el índice de la ciudad que en el punto 7 agregamos duplicada. ¿Se nota alguna particularidad?" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lis.index(\"Barcelona\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin', 'Bogotá', 'Buenos Aires', 'Ciudad México']\n" + ] + } + ], + "source": [ + "print(lis)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "11) ¿Qué pasa si se busca un elemento que no existe?" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "'Lima' is not in list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[17], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m lis\u001b[39m.\u001b[39;49mindex(\u001b[39m\"\u001b[39;49m\u001b[39mLima\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n", + "\u001b[1;31mValueError\u001b[0m: 'Lima' is not in list" + ] + } + ], + "source": [ + "lis.index(\"Lima\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "12) Eliminar un elemento de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "lis.remove(\"Buenos Aires\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin', 'Bogotá', 'Ciudad México']\n" + ] + } + ], + "source": [ + "print(lis)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "13) ¿Qué pasa si el elemento a eliminar no existe?" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "list.remove(x): x not in list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[20], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m lis\u001b[39m.\u001b[39;49mremove(\u001b[39m\"\u001b[39;49m\u001b[39mBuenos Aires\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n", + "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list" + ] + } + ], + "source": [ + "lis.remove(\"Buenos Aires\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "14) Extraer el úlimo elemento de la lista, guardarlo en una variable e imprimirlo" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ciudad México\n" + ] + } + ], + "source": [ + "ultimo = lis.pop()\n", + "print(ultimo)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "15) Mostrar la lista multiplicada por 4" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin', 'Bogotá', 'París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin', 'Bogotá', 'París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin', 'Bogotá', 'París', 'Londres', 'Barcelona', 'Helsinki', 'Roma', 'Amsterdam', 'Praga', 'Barcelona', 'Berlin', 'Bogotá']\n" + ] + } + ], + "source": [ + "print(lis * 4)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "16) Crear una tupla que contenga los números enteros del 1 al 20" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)\n" + ] + } + ], + "source": [ + "tup = tuple(range(1, 21))\n", + "print(type(tup))\n", + "print(tup)\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "17) Imprimir desde el índice 10 al 15 de la tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(11, 12, 13, 14, 15, 16)" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup[10:16]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "18) Evaluar si los números 20 y 30 están dentro de la tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "print(20 in tup)\n", + "print(30 in tup)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "19) Con la lista creada en el punto 1, validar la existencia del elemento 'París' y si no existe, agregarlo. Utilizar una variable e informar lo sucedido." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El elemento París ya existía\n" + ] + } + ], + "source": [ + "elemento = \"París\"\n", + "if elemento not in lis:\n", + " lis.append(elemento)\n", + "else:\n", + " print(f\"El elemento {elemento} ya existía\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "20) Mostrar la cantidad de veces que se encuentra un elemento específico dentro de la tupla y de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n" + ] + } + ], + "source": [ + "print(tup.count(10))\n", + "print(lis.count(\"Barcelona\"))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "21) Convertir la tupla en una lista" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "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": [ + "lis2 = list(tup)\n", + "print(lis2)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "22) Desempaquetar solo los primeros 3 elementos de la tupla en 3 variables" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "a,b,c = tup[:3]\n", + "print(a)\n", + "print(b)\n", + "print(c)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3\n" + ] + } + ], + "source": [ + "a,b,c,*rest=tup\n", + "print(a,b,c)\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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\"." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "24) Imprimir las claves del diccionario" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "25) Imprimir las ciudades a través de su clave" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "c85384e4cb51c8b72350f3a8712cc8351fdc3955e32a27f9b60c6242ab125f01" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Archivos_clases_en_vivo/Prep_Course_Homework_06.ipynb b/Archivos_clases_en_vivo/Prep_Course_Homework_06.ipynb new file mode 100644 index 000000000..5ded9518f --- /dev/null +++ b/Archivos_clases_en_vivo/Prep_Course_Homework_06.ipynb @@ -0,0 +1,552 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Iteradores e iterables" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1) A partir de una lista vacía, utilizar un ciclo while para cargar allí números negativos del -15 al -1" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]\n" + ] + } + ], + "source": [ + "lista = []\n", + "n = -15\n", + "while n < 0:\n", + " lista.append(n)\n", + " n += 1\n", + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(lista)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2) ¿Con un ciclo while sería posible recorrer la lista para imprimir sólo los números pares?" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-14\n", + "-12\n", + "-10\n", + "-8\n", + "-6\n", + "-4\n", + "-2\n" + ] + } + ], + "source": [ + "n = 0\n", + "while n < len(lista):\n", + " if lista[n] % 2 == 0:\n", + " print(lista[n])\n", + " n += 1" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3) Resolver el punto anterior sin utilizar un ciclo while" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-14\n", + "-12\n", + "-10\n", + "-8\n", + "-6\n", + "-4\n", + "-2\n" + ] + } + ], + "source": [ + "for numero in lista:\n", + " if numero % 2 == 0:\n", + " print(numero)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4) Utilizar el iterable para recorrer sólo los primeros 3 elementos" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-15\n", + "-14\n", + "-13\n" + ] + } + ], + "source": [ + "for elemento in lista[:3]:\n", + " print(elemento)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5) Utilizar la función **enumerate** para obtener dentro del iterable, tambien el índice al que corresponde el elemento" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 -15\n", + "1 -14\n", + "2 -13\n", + "3 -12\n", + "4 -11\n", + "5 -10\n", + "6 -9\n", + "7 -8\n", + "8 -7\n", + "9 -6\n", + "10 -5\n", + "11 -4\n", + "12 -3\n", + "13 -2\n", + "14 -1\n" + ] + } + ], + "source": [ + "for posicion, numero in enumerate(lista):\n", + " print(posicion, numero)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "lista = [1,2,5,7,8,10,13,14,15,17,20]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "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": [ + "n = 1\n", + "while n <= max(lista):\n", + " if n not in lista:\n", + " lista.append(n)\n", + " n +=1\n", + "lista.sort()\n", + "print(lista)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "7) La sucesión de Fibonacci es un listado de números que sigue la fórmula:
\n", + "n0 = 0
\n", + "n1 = 1
\n", + "ni = ni-1 + ni-2
\n", + "Crear una lista con los primeros treinta números de la sucesión.
" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista4 = [1, 2, 3, 4]\n", + "lista4[-2]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "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", + "\n", + "while n < 30:\n", + " fibo.append(fibo[-1] + fibo[-2])\n", + " n +=1\n", + "print(fibo)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8) Realizar la suma de todos elementos de la lista del punto anterior" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1346268" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(fibo)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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:
\n", + "Donde i es la cantidad total de elementos
\n", + "ni-1 / ni
\n", + "ni-2 / ni-1
\n", + "ni-3 / ni-2
\n", + "ni-4 / ni-3
\n", + "ni-5 / ni-4
\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "514229 / 317811 = 1.6180339887543225\n", + "317811 / 196418 = 1.618033988738303\n", + "196418 / 121393 = 1.6180339887802426\n", + "121393 / 75025 = 1.6180339886704431\n", + "75025 / 46368 = 1.618033988957902\n" + ] + } + ], + "source": [ + "for i in range(1, 6):\n", + " numerador = fibo[-i]\n", + " denominador = fibo[-i - 1]\n", + " division = numerador / denominador\n", + " print(f\"{numerador} / {denominador} = {division}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "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": [ + "print(fibo)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "10) A partir de la variable cadena ya dada, mostrar en qué posiciones aparece la letra \"n\"
\n", + "cadena = 'Hola Mundo. Esto es una practica del lenguaje de programación Python'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "11) Crear un diccionario e imprimir sus claves utilizando un iterador" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "12) Convertir en una lista la variable \"cadena\" del punto 10 y luego recorrerla con un iterador " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "13) Crear dos listas y unirlas en una tupla utilizando la función zip" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "14) A partir de la siguiente lista de números, crear una nueva sólo si el número es divisible por 7
\n", + "lis = [18,21,29,32,35,42,56,60,63,71,84,90,91,100]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "15) A partir de la lista de a continuación, contar la cantidad total de elementos que contiene, teniendo en cuenta que un elemento de la lista podría ser otra lista:
\n", + "lis = [[1,2,3,4],'rojo','verde',[True,False,False],['uno','dos','tres']]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "16) Tomar la lista del punto anterior y convertir cada elemento en una lista si no lo es" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "c85384e4cb51c8b72350f3a8712cc8351fdc3955e32a27f9b60c6242ab125f01" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Archivos_clases_en_vivo/Prep_Course_Homework_07.ipynb b/Archivos_clases_en_vivo/Prep_Course_Homework_07.ipynb new file mode 100644 index 000000000..59939b9dc --- /dev/null +++ b/Archivos_clases_en_vivo/Prep_Course_Homework_07.ipynb @@ -0,0 +1,461 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Funciones" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1) Crear una función que reciba un número como parámetro y devuelva True si es primo y False si no lo es" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def verificar_primo(numero):\n", + " es_primo = True\n", + " for divisor in range(2, numero):\n", + " if numero % divisor == 0:\n", + " es_primo = False\n", + " break\n", + " return es_primo" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "verificar_primo(6)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def extraer_primos(lista):\n", + " lista_primos = []\n", + " for elemento in lista:\n", + " if verificar_primo(elemento):\n", + " lista_primos.append(elemento)\n", + " return lista_primos" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_numeros = list(range(3, 24))\n", + "lista_numeros" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 5, 7, 11, 13, 17, 19, 23]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "extraer_primos(lista_numeros)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting pycopy-collections.abcNote: you may need to restart the kernel to use updated packages.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ERROR: pycopy-collections.abc from https://files.pythonhosted.org/packages/9e/b4/c5cc0e52a1af6770004cac86c13d751ddef97e01195873fbeced0cbf46a0/pycopy-collections.abc-0.0.0.tar.gz does not appear to be a Python project: neither 'setup.py' nor 'pyproject.toml' found.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " Downloading pycopy-collections.abc-0.0.0.tar.gz (633 bytes)\n" + ] + } + ], + "source": [ + "#pip install collection" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({'a': 3, 'b': 1, 'c': 1})" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import Counter\n", + "lista_prueba = [\"a\", \"a\", \"a\", \"b\", \"c\"]\n", + "dicc_prueba = Counter(lista_prueba)\n", + "dicc_prueba" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dicc_prueba.get(\"a\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "def calcular_moda(lista):\n", + " conteo = Counter(lista)\n", + " llave = max(conteo, key = conteo.get)\n", + " valor = conteo[llave]\n", + "\n", + " return llave, valor" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número 1 es el que más se repite, 3 veces\n" + ] + } + ], + "source": [ + "lista_numeros = [1, 1, 1, 4, 7, 4, 9, 10]\n", + "numero, repeticiones =calcular_moda(lista_numeros)\n", + "print(f\"El número {numero} es el que más se repite, {repeticiones} veces\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4) Crear una función que convierta entre grados Celsius, Farenheit y Kelvin
\n", + "Fórmula 1\t: (°C × 9/5) + 32 = °F
\n", + "Fórmula 2\t: °C + 273.15 = °K
\n", + "Debe recibir 3 parámetros: el valor, la medida de orígen y la medida de destino\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Despejar c en términos de f\n", + "(c * 9/5) + 32 = f \n", + "(c * 9/5) = f - 32\n", + "c = (f - 32) * 5/9\n", + "\n", + "Despejar k en términos de f\n", + "c = k - 273.15\n", + "(f - 32) * 5/9 = k - 273.15\n", + "k = ((f - 32) * 5/9) + 273.15\n", + "\n", + "Despejar f en términos de k\n", + "(f - 32) * 5/9 = k - 273.15\n", + "(f - 32) = (k - 273.15) * 9/5\n", + "f = ((k - 273.15) * 9/5) + 32" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def convertir_grados(valor, origen, destino):\n", + " if origen == \"celcius\":\n", + " if destino == \"celcius\":\n", + " valor_destino = valor\n", + " elif destino == \"farenheit\":\n", + " valor_destino = (valor * 9/5) + 32\n", + " elif destino == \"kelvin\":\n", + " valor_destino = valor + 273.15\n", + " else:\n", + " print(\"debe pasar un destino correcto\")\n", + " \n", + " elif origen == \"farenheit\":\n", + " if destino == \"celcius\":\n", + " valor_destino = (valor - 32) * 5/9\n", + " elif destino == \"farenheit\":\n", + " valor_destino = valor\n", + " elif destino == \"kelvin\":\n", + " valor_destino = ((valor - 32) * 5/9) + 273.15\n", + " else:\n", + " print(\"debe pasar un destino correcto\")\n", + "\n", + " elif origen == \"kelvin\":\n", + " if destino == \"celcius\":\n", + " valor_destino = valor - 273.15\n", + " elif destino == \"farenheit\":\n", + " valor_destino = ((valor - 273.15) * 9/5) + 32\n", + " elif destino == \"kelvin\":\n", + " valor_destino = valor\n", + " else:\n", + " print(\"debe pasar un destino correcto\")\n", + "\n", + " else:\n", + " print(\"debe pasar un origen correcto\") \n", + "\n", + " return valor_destino" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "274.15" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "convertir_grados(1, \"celcius\", \"kelvin\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Solución de Lucas\n", + "def gradosC(c):\n", + " formula1 = (c * (9/5)) + 32 \n", + " formula2 = c + 273.15\n", + " return f\"los grados celcius son {c}, en farenheit son {formula1}, y en kelvin son {formula2}\"\n", + "\n", + "def gradosF(f):\n", + " formula1 = (f - 32) * (5/9)\n", + " formula2 = formula1 + 273.15\n", + " return f\"los grados farenheit son {f}, en celcius son {formula1}, y en kelvin son {formula2}\"\n", + "\n", + "def gradosK(k):\n", + " formula1 = k - 273.15 \n", + " formula2 = (formula1 * (9/5)) + 32\n", + " return f\"los grados kelvin son {k}, en celcius son {formula1}, y en farenheit son {formula2\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5) 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:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 grado de celcius a celcius es igual a 1\n", + "1 grado de celcius a kelvin es igual a 274.15\n", + "1 grado de celcius a farenheit es igual a 33.8\n", + "1 grado de kelvin a celcius es igual a -272.15\n", + "1 grado de kelvin a kelvin es igual a 1\n", + "1 grado de kelvin a farenheit es igual a -457.87\n", + "1 grado de farenheit a celcius es igual a -17.22222222222222\n", + "1 grado de farenheit a kelvin es igual a 255.92777777777775\n", + "1 grado de farenheit a farenheit es igual a 1\n" + ] + } + ], + "source": [ + "metricas = [\"celcius\", \"kelvin\", \"farenheit\"]\n", + "for indice_origen in range(0, 3):\n", + " for indice_destino in range(0, 3):\n", + " valor_funcion = convertir_grados(1, metricas[indice_origen], metricas[indice_destino])\n", + " print(f\"1 grado de {metricas[indice_origen]} a {metricas[indice_destino]} es igual a {valor_funcion}\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6) 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" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "def calcular_factorial(numero):\n", + " if type(numero) != int:\n", + " return \"La entrada debe ser un entero\"\n", + " \n", + " if numero <= 0:\n", + " return \"La entrada deber ser un positivo\"\n", + " \n", + " if numero == 1:\n", + " return 1\n", + " \n", + " numero = numero * calcular_factorial(numero - 1)\n", + "\n", + " return numero" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calcular_factorial(5)" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "c85384e4cb51c8b72350f3a8712cc8351fdc3955e32a27f9b60c6242ab125f01" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Archivos_clases_en_vivo/clase_en_vivo 16.11.U4.ipynb b/Archivos_clases_en_vivo/clase_en_vivo 16.11.U4.ipynb new file mode 100644 index 000000000..e69de29bb diff --git a/Archivos_clases_en_vivo/clase_en_vivo. estructura de datos.ipynb b/Archivos_clases_en_vivo/clase_en_vivo. estructura de datos.ipynb new file mode 100644 index 000000000..3a447e70a --- /dev/null +++ b/Archivos_clases_en_vivo/clase_en_vivo. estructura de datos.ipynb @@ -0,0 +1,1230 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Listas" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "lista = [1, 7.4, \"Henry\", True, \"Lationamérica\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 7.4, 'Henry', True, 'Lationamérica']\n" + ] + } + ], + "source": [ + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[7.4, 'Henry', True]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Slicers\n", + "lista[1:4]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[7.4, True]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# steps\n", + "lista[1:4:2]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Henry', True, 'Lationamérica']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "## Modificar\n", + "lista[2] = \"Perro\"" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', True, 'Lationamérica']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', True, 'Lationamérica', False]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## append\n", + "lista.append(False)\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', 'Indice 3', True, 'Lationamérica', False]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## insert\n", + "lista.insert(3, \"Indice 3\")\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## index\n", + "lista.index(\"Lationamérica\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', True, 'Lationamérica', False]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## remove\n", + "lista.remove(\"Indice 3\")\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "[1, 7.4, 'Perro', True, 'Lationamérica']\n" + ] + } + ], + "source": [ + "## pop\n", + "ultimo_elemento = lista.pop()\n", + "print(ultimo_elemento)\n", + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1,\n", + " 7.4,\n", + " 'Perro',\n", + " True,\n", + " 'Lationamérica',\n", + " 'Buenos Aires',\n", + " 'Ciudad de México',\n", + " ' Bogotá']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## extend (concatenar listas)\n", + "segunda_lista = [\"Buenos Aires\", \"Ciudad de México\", \" Bogotá\"]\n", + "lista.extend(segunda_lista)\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 7.4, 'Perro', True, 'Lationamérica', 'Buenos Aires', 'Ciudad de México', ' Bogotá', 1, 7.4, 'Perro', True, 'Lationamérica', 'Buenos Aires', 'Ciudad de México', ' Bogotá']\n" + ] + } + ], + "source": [ + "print(lista * 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1,\n", + " 7.4,\n", + " 'Perro',\n", + " True,\n", + " 'Lationamérica',\n", + " 'Buenos Aires',\n", + " 'Ciudad de México',\n", + " ' Bogotá']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1,\n", + " 7.4,\n", + " 'Perro',\n", + " 'Lationamérica',\n", + " 'Buenos Aires',\n", + " 'Ciudad de México',\n", + " ' Bogotá']" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista.pop(3) \n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 6, 4, 1]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_numero = [6, 4, 10, 1]\n", + "lista_numero.sort(reverse = True)\n", + "lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c']" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_letras = [\"b\", \"a\", \"c\"]\n", + "lista_letras.sort()\n", + "lista_letras" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Contenencia\n", + "10 in lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3 in lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "## Desempaquetar\n", + "letra_1, letra_2, letra_3 = \"a\", \"b\", \"c\"" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "b\n", + "c\n" + ] + } + ], + "source": [ + "letra_1, letra_2, letra_3 = lista_letras\n", + "print(letra_1)\n", + "print(letra_2)\n", + "print(letra_3)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c']\n" + ] + } + ], + "source": [ + "print(lista_letras)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tuplas" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('Python', 4, True, 5.8, 'Henry', 'Python')" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla = (\"Python\", 4, True, 5.8, \"Henry\", \"Python\")\n", + "tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(tupla)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('a', 'b', 'c')" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tuple(lista_letras)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## contenecia\n", + "4 in tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla.count(\"Python\")" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(33, 39, 40, 29, 27, 20)" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Empaquetar\n", + "edades = 33, 39, 40, 29, 27, 20\n", + "edades" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(edades)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "33\n", + "39\n", + "40\n", + "29\n", + "27\n", + "20\n" + ] + } + ], + "source": [ + "## Desempaquetar\n", + "gustavo, laura, jonny, escarlette, felipe, jimmy = edades\n", + "print(gustavo)\n", + "print(laura)\n", + "print(jonny)\n", + "print(escarlette)\n", + "print(felipe)\n", + "print(jimmy)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Diccionarios" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'gustavo': 33, 'laura': 39, 'jonny': 40}" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alumnos = {\"gustavo\": 33, \"laura\": 39, \"jonny\": 40}\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(alumnos)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "39" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Acceder a un valor\n", + "alumnos[\"laura\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'gustavo': 33, 'laura': 39, 'jonny': 25}" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Cambiar un valor\n", + "alumnos[\"jonny\"] = 25\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'gustavo': 33, 'laura': 39, 'jonny': 25, 'escarlette': 29}" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alumnos[\"escarlette\"] = 29\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'laura': 39, 'jonny': 25, 'escarlette': 29}" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## eliminar llave-valor\n", + "del alumnos[\"gustavo\"]\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['laura', 'jonny', 'escarlette'])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## acceder a todas las llaves\n", + "alumnos.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([39, 25, 29])" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Acceder a valor\n", + "alumnos.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('laura', 39), ('jonny', 25), ('escarlette', 29)])" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## tupla de llave-valor\n", + "alumnos.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Iteradores e iterables" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "6\n", + "10\n", + "14\n", + "18\n" + ] + } + ], + "source": [ + "## Iteracion listas o tuplas\n", + "iterable = [1, 3, 5, 7, 9]\n", + "for numero in iterable:\n", + " print(numero * 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PYTHON ES MUY USADO EN CIENCIA DE DATOS" + ] + } + ], + "source": [ + "## Iteracion en texto\n", + "texto = \"Python es muy usado en ciencia de datos\"\n", + "for letra in texto:\n", + " print(letra.upper(), end= \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "## Iteración sobre diccionarios\n", + "paises = {\"España\": \"Europa\", \"México\": \"América\", \"Egipto\": \"Africa\"}" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "España\n", + "México\n", + "Egipto\n" + ] + } + ], + "source": [ + "for pais in paises:\n", + " print(pais)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "España\n", + "México\n", + "Egipto\n" + ] + } + ], + "source": [ + "for pais in paises.keys():\n", + " print(pais)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Europa\n", + "América\n", + "Africa\n" + ] + } + ], + "source": [ + "for continente in paises.values():\n", + " print(continente)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('España', 'Europa')\n", + "('México', 'América')\n", + "('Egipto', 'Africa')\n" + ] + } + ], + "source": [ + "for tupla in paises.items():\n", + " print(tupla)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Iterador" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], + "source": [ + "colores = [\"rojo\", \"verde\", \"azul\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [], + "source": [ + "iterador_colores = iter(colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'rojo'" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'verde'" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'azul'" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[70], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[39mnext\u001b[39;49m(iterador_colores)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [], + "source": [ + "next(iterador_colores, None)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Función zip" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Julian', 33)\n", + "('Esperanza', 39)\n", + "('Gerardo', 42)\n" + ] + } + ], + "source": [ + "nombre = [\"Julian\", \"Esperanza\", \"Gerardo\"]\n", + "edades = [33, 39, 42]\n", + "combinacion = zip(nombre, edades)\n", + "for tupla in combinacion:\n", + " print(tupla)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Comprensión de listas" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['banano', 'mango']\n" + ] + } + ], + "source": [ + "frutas = [\"manzana\", \"banano\", \"mango\", \"fresa\"]\n", + "nueva_lista = []\n", + "\n", + "for fruta in frutas:\n", + " if \"o\" in fruta:\n", + " nueva_lista.append(fruta)\n", + "\n", + "print(nueva_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['banano', 'mango']" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## [valor for variable in iterable if condicion]\n", + "nueva_lista_2 = [fruta for fruta in frutas if \"o\" in fruta]\n", + "nueva_lista_2" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "maestria", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Archivos_clases_en_vivo/clase_en_vivo.ipynb b/Archivos_clases_en_vivo/clase_en_vivo.ipynb new file mode 100644 index 000000000..055a49382 --- /dev/null +++ b/Archivos_clases_en_vivo/clase_en_vivo.ipynb @@ -0,0 +1,439 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Clases" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Abstracción - Atributos - Métodos" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class Libro:\n", + " def __init__(self, titulo, autor):\n", + " self.titulo = titulo\n", + " self.autor = autor\n", + " self.pagina = 1\n", + " self.texto_subrayados = []\n", + "\n", + " def pasar_pagina(self):\n", + " self.pagina += 1\n", + " print(f\"Página {self.pagina}\")\n", + "\n", + " def subrayar_texto(self, texto):\n", + " self.texto_subrayados.append(texto)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "## Crear un objeto\n", + "libro1 = Libro(\"El lobo estepario\", \"Herman Hesse\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El lobo estepario Herman Hesse\n" + ] + } + ], + "source": [ + "## Atributos\n", + "print(libro1.titulo, libro1.autor)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Página 3\n" + ] + } + ], + "source": [ + "## Métodos\n", + "libro1.pasar_pagina()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "libro1.subrayar_texto(\"No tememos a la soledad, sino a lo que encontramos en ella.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['No tememos a la soledad, sino a lo que encontramos en ella.']\n" + ] + } + ], + "source": [ + "print(libro1.texto_subrayados)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "libro1.subrayar_texto(\"Cita 2\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['No tememos a la soledad, sino a lo que encontramos en ella.', 'Cita 2']\n" + ] + } + ], + "source": [ + "print(libro1.texto_subrayados)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Herencia" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "class LibroLiteratura(Libro):\n", + " def __init__(self, titulo, autor, año):\n", + " super().__init__(titulo, autor)\n", + " self.año = año\n", + " self.listado_palabras = []\n", + "\n", + " def agregar_palabra(self, palabra):\n", + " self.listado_palabras.append(palabra)\n", + " print(f\"Se agregó al listado la palabra {palabra}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "libro_literatura1 = LibroLiteratura(\"El Aleph\", \"Jorge Luis Borges\", 1949)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Página 2\n" + ] + } + ], + "source": [ + "libro_literatura1.pasar_pagina()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Se agregó al listado la palabra Zahir\n" + ] + } + ], + "source": [ + "libro_literatura1.agregar_palabra(\"Zahir\")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Zahir']" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "libro_literatura1.listado_palabras" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Poliformismo" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "class LibroProgramacion(Libro):\n", + " def __init__(self, titulo, autor, idioma):\n", + " super().__init__(titulo, autor)\n", + " self.idioma = idioma\n", + " self.codigos_guardados = []\n", + "\n", + " def subrayar_texto(self, codigo):\n", + " self.codigos_guardados.append(codigo)\n", + " print(f\"Se guardó el código {codigo} en la lista {self.codigos_guardados}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "libro_programacion1 = LibroProgramacion(\"Python Programming\", \"Jogn Zelle\", \"Inglés\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Se guardó el código print('Hello, world!') en la lista [\"print('Hello, world!')\"]\n" + ] + } + ], + "source": [ + "libro_programacion1.subrayar_texto(\"print('Hello, world!')\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "libro_literatura1.subrayar_texto(\"He cometido el peor pecado que uno puede cometer. No he sido feliz.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Encapsulamiento" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "class Paciente:\n", + " def __init__(self, nombre, edad):\n", + " self._nombre = nombre\n", + " self._edad = edad\n", + "\n", + " def mostrar_nombre_paciente(self):\n", + " print(self._nombre)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "Paciente1 = Paciente(\"Ana\", 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ana\n" + ] + } + ], + "source": [ + "Paciente1.mostrar_nombre_paciente()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Módulos" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "from operaciones_matematicas import sumar" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumar(5, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "from operaciones_matematicas import *" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "multiplicar(3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import operaciones_matematicas as om\n", + "om.sumar(7, 8)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "sh", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Archivos_clases_en_vivo/clase_en_vivo_funciones.ipynb b/Archivos_clases_en_vivo/clase_en_vivo_funciones.ipynb new file mode 100644 index 000000000..d3d5eefbf --- /dev/null +++ b/Archivos_clases_en_vivo/clase_en_vivo_funciones.ipynb @@ -0,0 +1,660 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Funciones" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def calcular_area_rectangulo(base, altura):\n", + " area = base * altura\n", + " return area" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calcular_area_rectangulo(3, 4)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "## Función sin paramétros\n", + "def saludar():\n", + " print(\"Hola\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola\n" + ] + } + ], + "source": [ + "saludar()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "## Paramétros con valores por defecto\n", + "def saludar(nombre, saludo= \"Hola\"):\n", + " print(f\"{saludo} {nombre}, ¿cómo estás?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Buenas Juan, ¿cómo estás?\n" + ] + } + ], + "source": [ + "saludar(\"Juan\", \"Buenas\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "non-default argument follows default argument (3753730189.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[12], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m def saludar(saludo= \"Hola\", nombre):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n" + ] + } + ], + "source": [ + "## Un valor default no puede estar antes de uno no default\n", + "def saludar(saludo= \"Hola\", nombre):\n", + " print(f\"{saludo} {nombre}, ¿cómo estás?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "## Todos los valores default\n", + "def saludar(saludo= \"Hola\", nombre= \"mundo\"):\n", + " print(f\"{saludo} {nombre}, ¿cómo estás?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola mundo, ¿cómo estás?\n" + ] + } + ], + "source": [ + "saludar()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hey Pedro, ¿cómo estás?\n" + ] + } + ], + "source": [ + "## Llamado por posición\n", + "saludar(\"Hey\", \"Pedro\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hey Pedro, ¿cómo estás?\n" + ] + } + ], + "source": [ + "## Llamado por valor\n", + "saludar(nombre =\"Pedro\", saludo=\"Hey\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "def preguntar_capital(nombre, pais, capital):\n", + " print(f\"La capital del pais {pais} donde vive el estudiante {nombre} es {capital}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La capital del pais México donde vive el estudiante Jorge es Ciudad de México\n" + ] + } + ], + "source": [ + "preguntar_capital(nombre=\"Jorge\", pais=\"México\", capital=\"Ciudad de México\")" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La capital del pais México donde vive el estudiante Jorge es Ciudad de México\n" + ] + } + ], + "source": [ + "preguntar_capital( pais=\"México\", capital=\"Ciudad de México\", nombre=\"Jorge\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "##Utilizar return\n", + "def convertir_mayusculas(texto):\n", + " return texto.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "SEGÚN PLATON, EL AMOR ES LA APRECIACION DE LA BELLEZA EN SI MISMA\n" + ] + } + ], + "source": [ + "resultado_return = convertir_mayusculas(\"según platon, el amor es la apreciacion de la belleza en si misma\")\n", + "print(resultado_return)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(resultado_return)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "## utilizar print\n", + "def convertir_mayusculas(texto):\n", + " print(texto.upper())" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "SEGÚN PLATON, EL AMOR ES LA APRECIACION DE LA BELLEZA EN SI MISMA\n", + "None\n" + ] + } + ], + "source": [ + "resultado_print = convertir_mayusculas(\"según platon, el amor es la apreciacion de la belleza en si misma\")\n", + "print(resultado_print)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "NoneType" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(resultado_print)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Variables locales y globales" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "## Variables locales\n", + "def dividir(numerador, denominador):\n", + " resultado_division = numerador / denominador \n", + " return resultado_division" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.0" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dividir(20, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'resultado_division' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[34], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m resultado_division\n", + "\u001b[1;31mNameError\u001b[0m: name 'resultado_division' is not defined" + ] + } + ], + "source": [ + "resultado_division" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "## Variable global\n", + "denominador = 5\n", + "def dividir(numerador, denominador):\n", + " resultado_division = numerador / denominador\n", + " return resultado_division" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.0" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dividir(20, denominador)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "denominador" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Pasajes por valor y por referencia" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "## Pasajes por valor\n", + "def duplicar_numero(numero):\n", + " numero *= 2\n", + " return numero" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "x = 5" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "duplicar_numero(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "# Pasaje por referencia\n", + "def agregar_elemento(lista, elemento):\n", + " lista.append(elemento)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "mi_lista = [1, 2, 3]\n", + "agregar_elemento(mi_lista, 4)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4]\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Recursividad" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "def factorial(n):\n", + " if n == 0:\n", + " return 1\n", + " else:\n", + " return n * factorial(n - 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5 * factorial(4) = ?\n", + "\n", + "4 * factorial(3) = ?\n", + "\n", + "factorial (3) = 3 * factorial(2) = 6\n", + "\n", + "factorial (2) = 2 * factorial(1) = 2\n", + "\n", + "factorial (1) = 1 * factorial(0) = 1 \n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Funciones Lambda" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "sumar = lambda x, y, z: x + y + z" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumar(3, 5, 7)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "sh", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git "a/M02_introprogramaci\303\263n/Practica.ipynb" "b/M02_introprogramaci\303\263n/Practica.ipynb" index b35d69666..3a84e6432 100644 --- "a/M02_introprogramaci\303\263n/Practica.ipynb" +++ "b/M02_introprogramaci\303\263n/Practica.ipynb" @@ -12,45 +12,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hola Mundo!\n", - "20\n", - "19\n", - "18\n", - "17\n", - "16\n", - "15\n", - "14\n", - "13\n", - "12\n", - "11\n", - "10\n", - "9\n", - "8\n", - "7\n", - "6\n", - "5\n", - "4\n", - "3\n", - "2\n", - "1\n" - ] - } - ], - "source": [ - "print('Hola Mundo!')\n", - "a = 20\n", - "while a > 0:\n", - " \n", - " print(a)\n", - " a -= 1" - ] + "outputs": [], + "source": [] } ], "metadata": { @@ -69,7 +34,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.12.0" }, "orig_nbformat": 4 }, diff --git a/M03_variablesydatos/Prep_Course_Homework_03.ipynb b/M03_variablesydatos/Prep_Course_Homework_03.ipynb index b4d68b08b..880c02c03 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": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "enteros= 5\n", + "print(enteros)" + ] }, { "attachments": {}, @@ -418,7 +429,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/M03_variablesydatos/practi_operaciones_entre_variables.ipynb b/M03_variablesydatos/practi_operaciones_entre_variables.ipynb new file mode 100644 index 000000000..7182b304e --- /dev/null +++ b/M03_variablesydatos/practi_operaciones_entre_variables.ipynb @@ -0,0 +1,1052 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "16" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "8+8 #SUMA" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11.5" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a= 8\n", + "b= 3.5 #para los decimales no usar comas, simo pintos. (3,5 no!!! )\n", + "a+b" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a=8\n", + "a+10" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "22-11 #RESTA " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m=50\n", + "n=30 #entre variables\n", + "m-n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m=50 #entre variables y números \n", + "60-m" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "20*4 # MULTIPLICACIÓN" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "num1=6 #entre variables\n", + "num2=3\n", + "num1*num2 " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "num=5\n", + "num*4 #entre números y variables " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "25" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5**2 #POTENCIACIÓN" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b=2 #entre variables \n", + "e=3\n", + "b**e" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numero=3 # entre variales y números - la variable numero es la base y 2 es el exponente\n", + "numero**2 " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.3333333333333335" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10/3 #DIVISIÓN una barra o slash divide el número de la izquierda con el de la derecha.\n", + " #nos devuelve un decimal - tipo float" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10//3 # Al usar dos barras redondea la respuesta y nos devuelve un número entero " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "division by zero", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M03_variablesydatos\\practi_operaciones_entre_variables.ipynb Cell 15\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m \u001b[39m20\u001b[39;49m\u001b[39m/\u001b[39;49m\u001b[39m0\u001b[39;49m\n", + "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], + "source": [ + "20/0 # La división por cero nos va a devolver un error." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "dividendo= 21\n", + "divisor=7" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.0" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dividendo/divisor # entre variables" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5.25" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dividendo/4 #entre una variable y un número" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# modulo o resto\n", + "\n", + "7%2 # 7/2 es igual a 3, el resto de la división es igual a 1." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "0\n", + "0\n" + ] + } + ], + "source": [ + "#numeros pares con resto cero\n", + "print(6%2)\n", + "print(12%2)\n", + "print(488%2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "1\n", + "1\n" + ] + } + ], + "source": [ + "#numeros impares de resto 1\n", + "print(9%2)\n", + "print(15%2)\n", + "print(327%2)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#OPERADORES RELACIONALES\n", + "'mano'=='mano'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "4==2+2" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "4=='4' # uno es un valor de tipo entero el otro es un string" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "4!='4' # operador 'distinto' devuelve true si los operandos son distintos" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "9!=4+5" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12>3 " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "num1=12\n", + "num2=16" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "num1\n", + "\n" + ] + } + ], + "source": [ + "print(type(x))\n", + "print(type(apellido)) #al usar print nos devuelve ambos tipos de datos" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(x) # ya que la variable tiene un valor almacenado es True, si estuviera vacía sería False" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "num=0" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(num) #al ser un elemento nulo devuelve False" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "num=3.5 # Casting o conversión de tipo de datos\n", + "print(type(num))" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(num) #convertimos a entero" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "num='8'\n", + "type(num) #obtenemos el tipo de variable " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(num) # lo convertimos a entero" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(num)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "num = int(num) #reasignamos un valor de tipo entero a la variable num, al guardar en cambio en una variable\n", + "type(num) #vemos el tipo de dato" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "num='8.5'\n", + "print(type(num))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "num3=float(num) #convertimos a flotante y guardamos en una nueva variable" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "print(type(num)) #tipo de variable string\n", + "print(type(num3)) #tipo de variable float" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "num=4 \n", + "print(type(num))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "num4=float(num) #convertimos a float y guardamos en una variable\n", + "print(type(num4))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "x=10 #declaramos un entero como variable\n", + "print(type(x)) #comprobamos el tipo de dato de x\n", + "y=str(x) #convertimos a strig a la variable X que antes era un entero\n", + "print(type(x)) #imprimimos el tipo de dato de y" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "w=2.2 # declaramos un una variable tipo float y la convertimos a float\n", + "print(type(w))\n", + "z=str(w)\n", + "print(type(z))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb b/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb index 8b5ecb46c..2b093b98c 100644 --- a/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb +++ b/M04_flujosdecontrol/Prep_Course_Homework_04.ipynb @@ -18,10 +18,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número 10, es mayor que cero\n" + ] + } + ], + "source": [ + "num = 10\n", + "if num > 0:\n", + " print(f\"El número {num}, es mayor que cero\")\n", + "elif num < 0:\n", + " print(f\"El número {num}, es menor que cero\")\n", + "else:\n", + " print(f\"El número {num}, es igual que cero\") " + ] }, { "attachments": {}, @@ -33,10 +49,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Las variables Henry e 4 son de distinto tipo de dato\n" + ] + } + ], + "source": [ + "x = \"Henry\"\n", + "y = 4\n", + "if type(x) == type(y):\n", + " print(f\"Las variables {x} e {y} son del mismo tipo de dato\")\n", + "else:\n", + " print(f\"Las variables {x} e {y} son de distinto tipo de dato\")" + ] }, { "attachments": {}, @@ -48,10 +79,86 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número 1 es impar\n", + "El número 2 es par\n", + "El número 3 es impar\n", + "El número 4 es par\n", + "El número 5 es impar\n", + "El número 6 es par\n", + "El número 7 es impar\n", + "El número 8 es par\n", + "El número 9 es impar\n", + "El número 10 es par\n", + "El número 11 es impar\n", + "El número 12 es par\n", + "El número 13 es impar\n", + "El número 14 es par\n", + "El número 15 es impar\n", + "El número 16 es par\n", + "El número 17 es impar\n", + "El número 18 es par\n", + "El número 19 es impar\n", + "El número 20 es par\n" + ] + } + ], + "source": [ + "for num in range (1, 21): #resuelto con un bucle for\n", + " if num %2 == 0:\n", + " print(f\"El número {num} es par\")\n", + " else:\n", + " print(f\"El número {num} es impar\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número 1 es impar\n", + "El número 2 es par\n", + "El número 3 es impar\n", + "El número 4 es par\n", + "El número 5 es impar\n", + "El número 6 es par\n", + "El número 7 es impar\n", + "El número 8 es par\n", + "El número 9 es impar\n", + "El número 10 es par\n", + "El número 11 es impar\n", + "El número 12 es par\n", + "El número 13 es impar\n", + "El número 14 es par\n", + "El número 15 es impar\n", + "El número 16 es par\n", + "El número 17 es impar\n", + "El número 18 es par\n", + "El número 19 es impar\n", + "El número 20 es par\n" + ] + } + ], + "source": [ + "num = 1 #resuelto con un bucle while\n", + "while num <21:\n", + " if num %2 == 0:\n", + " print(f\"El número {num} es par\")\n", + " num= num + 1 \n", + " else:\n", + " print(f\"El número {num} es impar\")\n", + " num= num + 1 \n" + ] }, { "attachments": {}, @@ -63,10 +170,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número 0 elevado a la 3ra potencia es 0 \n", + "El número 1 elevado a la 3ra potencia es 1 \n", + "El número 2 elevado a la 3ra potencia es 8 \n", + "El número 3 elevado a la 3ra potencia es 27 \n", + "El número 4 elevado a la 3ra potencia es 64 \n", + "El número 5 elevado a la 3ra potencia es 125 \n" + ] + } + ], + "source": [ + "for i in range(0,6): #resuelto con un boocle for\n", + " potenciación = i ** 3\n", + " print(f\"El número {i} elevado a la 3ra potencia es {potenciación} \")\n" + ] }, { "attachments": {}, @@ -78,10 +202,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ciclo for Nro 0\n", + "Ciclo for Nro 1\n", + "Ciclo for Nro 2\n", + "Ciclo for Nro 3\n", + "Ciclo for Nro 4\n" + ] + } + ], + "source": [ + "variable = 5\n", + "for i in range(0, variable):\n", + " print(f\"Ciclo for Nro {i}\")\n" + ] }, { "attachments": {}, @@ -93,10 +233,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " El factorial de 120\n" + ] + } + ], + "source": [ + "x = 5\n", + "if type(x) == int:\n", + " if x > 0 :\n", + " factorial = x\n", + " while x >= 2:\n", + " x = x - 1\n", + " factorial = factorial * x \n", + " print (f\" El factorial de {factorial}\")\n", + " else:\n", + " (\"La variable no es mayor que cero\")\n", + "else:\n", + " print(\" La variable no es un entero \")\n" + ] }, { "attachments": {}, @@ -108,10 +269,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ciclo for 0 en ciclo while 0\n", + "ciclo for 1 en ciclo while 1\n", + "ciclo for 2 en ciclo while 2\n", + "ciclo for 3 en ciclo while 3\n", + "ciclo for 4 en ciclo while 4\n" + ] + } + ], + "source": [ + "x = True\n", + "while x == True:\n", + " for i in range(0,5):\n", + " print(f\"ciclo for {i} en ciclo while {i}\")\n", + " x = False" + ] }, { "attachments": {}, @@ -123,10 +302,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ciclo for 0\n", + "Ciclo While 0\n", + "Ciclo for 1\n", + "Ciclo While 1\n", + "Ciclo for 2\n", + "Ciclo While 2\n", + "Ciclo for 3\n", + "Ciclo While 3\n", + "Ciclo for 4\n", + "Ciclo While 4\n" + ] + } + ], + "source": [ + "for i in range (0, 5):\n", + " print(f\"Ciclo for {i}\")\n", + " while True:\n", + " print(f\"Ciclo While {i}\")\n", + " break\n" + ] }, { "attachments": {}, @@ -138,10 +340,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n" + ] + } + ], + "source": [ + "n = 2\n", + "while n <= 30:\n", + " primo = True\n", + " for divisor in range(2, n):\n", + " if n % divisor == 0:\n", + " primo = False\n", + " if primo == True:\n", + " print(n) \n", + " n += 1 \n" + ] }, { "attachments": {}, @@ -153,10 +382,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n" + ] + } + ], + "source": [ + "n = 2\n", + "while n <= 30:\n", + " primo = True\n", + " for divisor in range(2, n):\n", + " if n % divisor == 0:\n", + " #print(f\"El numero es {n} y el divisor es {divisor}\")\n", + " primo = False\n", + " #print(f\"El valor de primeo es {primo}\")\n", + " break # Utiliando este brake detengo el for al encontrar el primer caso de exclucíon como primo. Menos iteraciones\n", + " if primo == True:\n", + " print(n) \n", + " n += 1 " + ] }, { "attachments": {}, @@ -168,10 +427,84 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n", + "Cantidad de ciclos sin brake 406\n" + ] + } + ], + "source": [ + "cantidad_de_ciclos = 0\n", + "n = 2\n", + "while n <= 30:\n", + " primo = True\n", + " for divisor in range(2, n):\n", + " cantidad_de_ciclos += 1\n", + " if n % divisor == 0:\n", + " primo = False\n", + " if primo == True:\n", + " print(n) \n", + " n += 1\n", + "print(f\"Cantidad de ciclos sin brake {cantidad_de_ciclos}\") \n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n", + "Cantida de ciclos con brake 19\n", + "En codigo de optimizo en un 95.32019704433498 % menos de iteraciones\n" + ] + } + ], + "source": [ + "cantidad_de_ciclos_con_brake = 0\n", + "n = 2\n", + "while n <= 30:\n", + " primo = True\n", + " for divisor in range(2, n):\n", + " if n % divisor == 0:\n", + " cantidad_de_ciclos_con_brake += 1\n", + " #print(f\"El numero es {n} y el divisor es {divisor}\")\n", + " primo = False\n", + " #print(f\"El valor de primeo es {primo}\")\n", + " break # Utiliando este brake detengo el for al encontrar el primer caso de exclucíon como primo. Menos iteraciones\n", + " if primo == True:\n", + " print(n) \n", + " n += 1 \n", + "print(f\"Cantida de ciclos con brake {cantidad_de_ciclos_con_brake}\")\n", + "print(f\"En codigo de optimizo en un {((cantidad_de_ciclos - cantidad_de_ciclos_con_brake) / cantidad_de_ciclos) *100} % menos de iteraciones\")" + ] }, { "attachments": {}, @@ -183,10 +516,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El 108 es divisible por 12\n", + "El 120 es divisible por 12\n", + "El 132 es divisible por 12\n", + "El 144 es divisible por 12\n", + "El 156 es divisible por 12\n", + "El 168 es divisible por 12\n", + "El 180 es divisible por 12\n", + "El 192 es divisible por 12\n", + "El 204 es divisible por 12\n", + "El 216 es divisible por 12\n", + "El 228 es divisible por 12\n", + "El 240 es divisible por 12\n", + "El 252 es divisible por 12\n", + "El 264 es divisible por 12\n", + "El 276 es divisible por 12\n", + "El 288 es divisible por 12\n", + "El 300 es divisible por 12\n" + ] + } + ], + "source": [ + "numero = 99\n", + "while (numero <= 300):\n", + " numero += 1\n", + " if numero % 12 != 0:\n", + " continue\n", + " print(f\"El {numero} es divisible por 12\")\n", + " \n" + ] }, { "attachments": {}, @@ -198,10 +563,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Si desea saber si un némero es primo ingréselo\n", + "El numero es primo\n", + "¿Desea encontrar el siguiente número primo?\n" + ] + } + ], + "source": [ + "n = 1\n", + "sigue = 1\n", + "primo = True\n", + "while (sigue == 1):\n", + " for div in range(2, n):\n", + " if (n % div == 0):\n", + " primo = False\n", + " break\n", + " print(\"Si desea saber si un némero es primo ingréselo\")\n", + " if primo:\n", + " if (input() != '1'):\n", + " print(f\"El numero {input()} es primo\")\n", + " print('¿Desea encontrar el siguiente número primo?')\n", + " break\n", + " else:\n", + " primo = True\n", + " n += 1" + ] }, { "attachments": {}, @@ -213,10 +606,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "102 es divisible por tres y ademas es multiplo de seis\n" + ] + } + ], + "source": [ + "numero = 99\n", + "while (numero <=300):\n", + " numero +=1\n", + " if numero % 3 == 0:\n", + " if numero % 6 == 0:\n", + " print(f\"{numero} es divisible por tres y ademas es multiplo de seis\")\n", + " break\n" + ] } ], "metadata": { @@ -238,7 +647,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/M04_flujosdecontrol/clase_en_vivo 16.11.U4.ipynb b/M04_flujosdecontrol/clase_en_vivo 16.11.U4.ipynb new file mode 100644 index 000000000..ae126e4ee --- /dev/null +++ b/M04_flujosdecontrol/clase_en_vivo 16.11.U4.ipynb @@ -0,0 +1,487 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# **Sentencia if - elif - else**" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número es positivo\n" + ] + } + ], + "source": [ + "# Esto es un comentario\n", + "numero = 5\n", + "if numero > 0:\n", + " print(\"El número es positivo\")\n", + "elif numero < 0:\n", + " print(\"El número es negativo\")\n", + "else:\n", + " print(\"El número es igual a cero\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número no es positivo\n", + "El tipo de dato es float\n" + ] + } + ], + "source": [ + "flotante = -4.6\n", + "if flotante > 0:\n", + " print(\"El número es positivo\")\n", + "else:\n", + " print(\"El número no es positivo\")\n", + "if type(flotante) == float:\n", + " print(\"El tipo de dato es float\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Su calificación es: F\n", + "Su calificación es: F\n" + ] + } + ], + "source": [ + "puntuacion = 35\n", + "if puntuacion >= 90:\n", + " calificacion = \"A\"\n", + "elif puntuacion >= 80:\n", + " calificacion = \"B\"\n", + "elif puntuacion >= 70:\n", + " calificacion = \"C\"\n", + "elif puntuacion >= 60:\n", + " calificacion = \"D\"\n", + "else:\n", + " calificacion = \"F\"\n", + "print(\"Su calificación es:\", calificacion)\n", + "print(f\"Su calificación es: {calificacion}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "20\n" + ] + } + ], + "source": [ + "print( 1 * 2)\n", + "print( 2 * 2)\n", + "print( 3 * 2)\n", + "print( 10 * 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Ciclo for" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "for i in range(0, 6):\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HOLA, MUNDO" + ] + } + ], + "source": [ + "texto = \"hola, mundo\"\n", + "for letra in texto:\n", + " print(letra.upper(), end = \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 H\n", + "1 O\n", + "2 L\n", + "3 A\n", + "4 ,\n", + "5 \n", + "6 M\n", + "7 U\n", + "8 N\n", + "9 D\n", + "10 O\n" + ] + } + ], + "source": [ + "## enumerate\n", + "texto = \"hola, mundo\"\n", + "for indice, letra in enumerate(texto):\n", + " print(indice, letra.upper())" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número en esta iteración es 1\n", + "La suma va 1\n", + "El número en esta iteración es 2\n", + "La suma va 3\n", + "El número en esta iteración es 3\n", + "La suma va 6\n", + "El número en esta iteración es 4\n", + "La suma va 10\n", + "El número en esta iteración es 5\n", + "La suma va 15\n", + "El número en esta iteración es 6\n", + "La suma va 21\n", + "El número en esta iteración es 7\n", + "La suma va 28\n", + "El número en esta iteración es 8\n", + "La suma va 36\n", + "El número en esta iteración es 9\n", + "La suma va 45\n", + "El número en esta iteración es 10\n", + "La suma va 55\n", + "La suma total es 55\n" + ] + } + ], + "source": [ + "suma = 0\n", + "for numero in range(1, 11):\n", + " print(f\"El número en esta iteración es {numero}\")\n", + " suma += numero\n", + " print(f\"La suma va {suma}\")\n", + "print(f\"La suma total es {suma}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Ciclo while" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "numero = 5\n", + "\n", + "while numero >= 1:\n", + " print(numero)\n", + " numero -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "contador = 1\n", + "while contador <= 5:\n", + " print(contador)\n", + " contador += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5 " + ] + } + ], + "source": [ + "contador = 1\n", + "while contador <= 5:\n", + " print(contador, end = \" \") # imprime de forma horizontal en lugar de vertical\n", + " contador += 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Continue y break" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "13 % 2" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n", + "10\n" + ] + } + ], + "source": [ + "for numero in range(1, 11):\n", + " if numero % 2 == 0:\n", + " print(numero)\n", + " else:\n", + " continue\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "for numero in range(1, 11):\n", + " if numero % 2 == 0:\n", + " print(numero)\n", + " else:\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pythn s n lngj d prgrmcn" + ] + } + ], + "source": [ + "frase = \"Python es un lenguaje de programación\"\n", + "vocales = \"aeiouó\"\n", + "\n", + "for letra in frase:\n", + " if letra in vocales:\n", + " continue\n", + " print(letra, end = \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Es l prmr vz q Clmb l gn Brsl" + ] + } + ], + "source": [ + "frase = input(\"Escriba la frase que quiere eliminar las vocales\") #pedimos ingreso al uduario\n", + "vocales = \"aeiouó\"\n", + "\n", + "for letra in frase:\n", + " if letra in vocales:\n", + " continue\n", + " print(letra, end = \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Iteración 1\n", + "Iteración 2\n", + "Iteración 3\n", + "Iteración 4\n", + "Iteración 5\n", + "Fin del ciclo\n" + ] + } + ], + "source": [ + "contador = 1\n", + "while True: # Seria como decirle que entre siempre\n", + " if contador > 5:\n", + " break\n", + " print(f\"Iteración {contador}\")\n", + " contador += 1\n", + "print(\"Fin del ciclo\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "sh", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M04_flujosdecontrol/practica clase en vivo y repaso homework.ipynb b/M04_flujosdecontrol/practica clase en vivo y repaso homework.ipynb new file mode 100644 index 000000000..166bfadab --- /dev/null +++ b/M04_flujosdecontrol/practica clase en vivo y repaso homework.ipynb @@ -0,0 +1,417 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sentecias if, elif y else" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El numero es positivo\n" + ] + } + ], + "source": [ + "numero=6\n", + "if numero > 0:\n", + " print( 'El numero es positivo')\n", + "elif numero < 0:\n", + " print( 'El número es negativo')\n", + "else:\n", + " print('El numeor es igual a cero')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El numeor es positivo\n", + "El tipo de dato es float\n" + ] + } + ], + "source": [ + "flotante = 4.6 #al usar dos if puedo evaluar mas de una condición\n", + "if flotante > 0:\n", + " print('El numeor es positivo')\n", + "if type(flotante)== float:\n", + " print('El tipo de dato es float')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El numeor es positivo\n" + ] + } + ], + "source": [ + "flotante = 4.6 #si uso un solo if no evalua la segunda condición\n", + "if flotante > 0:\n", + " print('El numeor es positivo')\n", + "elif type(flotante)== float:\n", + " print('El tipo de dato es float')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Su calificación es D\n" + ] + } + ], + "source": [ + "puntuacion = 65\n", + "if puntuacion >= 90:\n", + " calificacion = 'A'\n", + "elif puntuacion >= 80:\n", + " calificacion = 'B'\n", + "elif puntuacion >= 70:\n", + " calificacion = 'C'\n", + "elif puntuacion >= 60:\n", + " calificacion = 'D'\n", + "else:\n", + " calificacion = 'F'\n", + "print('Su calificación es', calificacion)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ciclo for" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "for i in range(0, 6):\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "print(i) # muestra el valor de la ultima iteración" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "O\n", + "L\n", + "A\n", + ",\n", + " \n", + "M\n", + "U\n", + "N\n", + "D\n", + "O\n" + ] + } + ], + "source": [ + "texto = \"Hola, mundo\"\n", + "for letra in texto:\n", + " print(letra.upper(), end = \"\\n\") #back slash es un salto de linea" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El nuero en esta iteración es 1\n", + "La suma va 1\n", + "El nuero en esta iteración es 2\n", + "La suma va 3\n", + "El nuero en esta iteración es 3\n", + "La suma va 6\n", + "El nuero en esta iteración es 4\n", + "La suma va 10\n", + "El nuero en esta iteración es 5\n", + "La suma va 15\n", + "El nuero en esta iteración es 6\n", + "La suma va 21\n", + "El nuero en esta iteración es 7\n", + "La suma va 28\n", + "El nuero en esta iteración es 8\n", + "La suma va 36\n", + "El nuero en esta iteración es 9\n", + "La suma va 45\n", + "El nuero en esta iteración es 10\n", + "La suma va 55\n", + "La suma total es 55\n" + ] + } + ], + "source": [ + "suma = 0\n", + "for numero in range(1, 11):\n", + " print(f\"El nuero en esta iteración es {numero}\")\n", + " suma += numero\n", + " print(f\"La suma va {suma}\")\n", + "print(f\"La suma total es {suma}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ciclo While" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "numero = 5\n", + "while numero >=1:\n", + " print(numero)\n", + " numero -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Continue y break" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "13 % 2" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n", + "10\n" + ] + } + ], + "source": [ + "for numero in range (1, 11):\n", + " if numero % 2 == 0:\n", + " print(numero)\n", + " else:\n", + " continue #continue termina una iteración en particular, brake detiene todo" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pytn s n lngj d prgrmcón" + ] + } + ], + "source": [ + "frase = \"Pyton es un lenguaje de programación\"\n", + "vocales = \"aeiou\"\n", + "\n", + "for letra in frase:\n", + " if letra in vocales:\n", + " continue\n", + " print(letra, end = \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pyt" + ] + } + ], + "source": [ + "frase = \"Pyton es un lenguaje de programación\"\n", + "vocales = \"aeiou\"\n", + "\n", + "for letra in frase:\n", + " if letra in vocales:\n", + " break\n", + " print(letra, end = \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "oeueuaeeoaaió" + ] + } + ], + "source": [ + "frase = \"Python es un lenguaje de programación\"\n", + "vocales = \"aeiouó\"\n", + "\n", + "for letra in frase:\n", + " if letra not in vocales: #imprime las volales\n", + " continue\n", + " print(letra, end = \"\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Repaso de homework" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M05_estructuradedatos/Practica_estructura_de_datos_listas.ipynb b/M05_estructuradedatos/Practica_estructura_de_datos_listas.ipynb new file mode 100644 index 000000000..15df9013a --- /dev/null +++ b/M05_estructuradedatos/Practica_estructura_de_datos_listas.ipynb @@ -0,0 +1,662 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Listas" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[22, 23, 24, 25]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[22, 23, 24, 25] # elentos enteros " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['rojo', ' azul', 'amarillo']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[\"rojo\", \" azul\", \"amarillo\"] #Strings" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['python', 8, 2.2, [10, 12, 14]]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[\"python\", 8, 2.2, [10,12,14] ] #Multiples elementos de difente tipo" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "colores = [\"rojo\", \"azul\", \"amarillo\"] # asignación de una lista a una variable\n", + "numeros = [22, 23, 24, 25]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "lista_vacia=[] #lista vacia" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[22, 23, 24, 25]\n", + "['rojo', 'azul', 'amarillo']\n", + "[]\n" + ] + } + ], + "source": [ + "print(numeros)\n", + "print(colores)\n", + "print(lista_vacia)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "print(type(numeros))\n", + "print(type(colores))\n", + "print(type(lista_vacia))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Indice de las listas" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "mi_lista = [\"rojo\", \"azul\", \"amarillo\", \"naranja\", \"violeta\", \"verde\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "amarillo\n" + ] + } + ], + "source": [ + "print(mi_lista[2]) # recuperacion del tercer elemento, se inicializa en Cero" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul']\n" + ] + } + ], + "source": [ + "print(mi_lista[0:2]) # obtenemos dos elementos de la lista n:n-1" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul']\n" + ] + } + ], + "source": [ + "print(mi_lista[:2]) #python asume que el primer valor es cero" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul', 'amarillo', 'naranja', 'violeta', 'verde']\n" + ] + } + ], + "source": [ + "print(mi_lista[0:]) # Python asume que se trata de toddos los elementos a partir del primero" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Metodos en las listas" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "mi_lista.append(\"blanco\") #agregar un elemento al final de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul', 'amarillo', 'naranja', 'violeta', 'verde', 'blanco']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + " mi_lista.insert(3,\"negro\") #insertamos un elemento en un índice en específico" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul', 'amarillo', 'negro', 'naranja', 'violeta', 'verde', 'blanco']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "mi_lista.extend([\"rosa\", \"gris\"]) #concatenamos una lista a la lista ya creada" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul', 'amarillo', 'negro', 'naranja', 'violeta', 'verde', 'blanco', 'rosa', 'gris']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mi_lista.index(\"azul\") # Podemos saber el indice del elemento \"azul\"" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "mi_lista.remove(\"blanco\") #eliminar un elemento de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul', 'amarillo', 'negro', 'naranja', 'violeta', 'verde', 'rosa', 'gris']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "'celeste' is not in list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\Practica_estructura_de_datos.ipynb Cell 25\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m \u001b[39mprint\u001b[39m(mi_lista\u001b[39m.\u001b[39;49mindex(\u001b[39m\"\u001b[39;49m\u001b[39mceleste\u001b[39;49m\u001b[39m\"\u001b[39;49m))\n", + "\u001b[1;31mValueError\u001b[0m: 'celeste' is not in list" + ] + } + ], + "source": [ + "print(mi_lista.index(\"celeste\")) #Error al buscar un elemento que no se encuentra en la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "ultimo = mi_lista.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'azul', 'amarillo', 'negro', 'naranja', 'violeta', 'verde', 'gris', 'gris']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "gris\n" + ] + } + ], + "source": [ + "print(ultimo)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "segundo = mi_lista.pop(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "azul\n" + ] + } + ], + "source": [ + "print(segundo)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['rojo', 'amarillo', 'negro', 'naranja', 'violeta', 'verde', 'gris', 'gris']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[22, 23, 24, 22, 23, 24, 22, 23, 24]\n" + ] + } + ], + "source": [ + "lista = [22, 23, 24]\n", + "print(lista * 3) #podemos obtener una lista multiplicada sin alterar la original" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[22, 23, 24]\n" + ] + } + ], + "source": [ + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "lista_por_tres = lista * 3 # Creamos una nueva variable y multiplicamos la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[22, 23, 24, 22, 23, 24, 22, 23, 24]" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_por_tres" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "lista = [1, 4, 3, 6, 8 ,2 ]" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "lista.sort() #ordenamos la lista de menor a mayor" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 6, 8]\n" + ] + } + ], + "source": [ + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [], + "source": [ + "mi_lista.sort() #ordena alfabéticamente la lista de strings" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['amarillo', 'gris', 'gris', 'naranja', 'negro', 'rojo', 'verde', 'violeta']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "lista = [ 1, 4, 3, 6, 8, 2]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "lista.sort(reverse = True) #ordenamos la lista de mayor a menor" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8, 6, 4, 3, 2, 1]\n" + ] + } + ], + "source": [ + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "mi_lista = [\"rojo\", \"azul\", \"amarillo\", \"naranja\", \"violeta\", \"verde\"]\n", + "mi_lista.sort(reverse=True) #ordenamos alfabeticamente pero en sentido inverso" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['violeta', 'verde', 'rojo', 'naranja', 'azul', 'amarillo']\n" + ] + } + ], + "source": [ + "print(mi_lista)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M05_estructuradedatos/Practica_estructuras_tuplas.ipynb b/M05_estructuradedatos/Practica_estructuras_tuplas.ipynb new file mode 100644 index 000000000..472cb61f9 --- /dev/null +++ b/M05_estructuradedatos/Practica_estructuras_tuplas.ipynb @@ -0,0 +1,673 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Tuplas" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "mi_tupla =(2, 4, 6, 8, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 4, 6, 8, 10)\n" + ] + } + ], + "source": [ + "print(mi_tupla)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(mi_tupla)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "tupla_dias = (\"lunes\", \"martes\", \"miercoles\", \"jueves\", \"viernes\", \"sabado\", \"domingo\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "martes\n" + ] + } + ], + "source": [ + "print(tupla_dias[1]) # Obtenemos segundo elemento de la lista" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"jueves\" in tupla_dias" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Jueves\" in tupla_dias #Devuelve flase xq al tener una mayuscula lo toma como un elemento distinto" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla_dias.count(\"sabado\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "mi_tupla_unitaria = (\"blanco\",) # Sintaxis de una tupla de un solo elemento" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(mi_tupla_unitaria)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "mi_tupla = ('amarillo') #sin la coma (,) se lo toma como una variable de tipo string" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(mi_tupla)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'sabado', 'domingo')" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla_dias" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'miercoles'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla_dias[2] #obtemos elemento del indice 2, tercer lugar" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('martes', 'miercoles')" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla_dias[1:3] #Obtenemos [n:n-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\Practica_estructuras_tuplas.ipynb Cell 17\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m tupla_dias[\u001b[39m0\u001b[39;49m] \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mlunes\u001b[39m\u001b[39m\"\u001b[39m\n", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "tupla_dias[0] = \"lunes\" #Da error xq las tuplas no pueden modificarse" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Empaquetado de tuplas" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "x = 11\n", + "y = 22\n", + "z = \"Henry\"" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "mi_tupla = x, y, z #Creamos una tupla a partir de variables" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(11, 22, 'Henry')\n" + ] + } + ], + "source": [ + "print(mi_tupla)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(mi_tupla)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Desempaquetado de tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "date = (22, \"agosto\", 1999)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(22, 'agosto', 1999)\n" + ] + } + ], + "source": [ + "print(date)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(date)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "uno, dos, tres = date #al igualar los elementos a la tupla asignamos los elementos de la misma a las variables." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "22\n", + "agosto\n", + "1999\n" + ] + } + ], + "source": [ + "print(uno)\n", + "print(dos)\n", + "print(tres)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "too many values to unpack (expected 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\Practica_estructuras_tuplas.ipynb Cell 29\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m uno, dos \u001b[39m=\u001b[39mdate\n", + "\u001b[1;31mValueError\u001b[0m: too many values to unpack (expected 2)" + ] + } + ], + "source": [ + "uno, dos =date #da error xq debe tener la misma cantidad de elementos" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "not enough values to unpack (expected 4, got 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\Practica_estructuras_tuplas.ipynb Cell 30\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m uno, dos, tres, cuatro \u001b[39m=\u001b[39m date\n", + "\u001b[1;31mValueError\u001b[0m: not enough values to unpack (expected 4, got 3)" + ] + } + ], + "source": [ + "uno, dos, tres, cuatro = date # da error xq hay mas variables que elementos para desempaquetar" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "tupla_impares = (1, 3, 5 , 7, 9)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "primero = tupla_impares[0] #desempaquetamos usando el indice\n", + "cuarto = tupla_impares[3]\n", + "quinto = tupla_impares[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "7\n", + "9\n" + ] + } + ], + "source": [ + "print(primero)\n", + "print(cuarto)\n", + "print(quinto)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 3, 5, 7, 9)" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla_impares" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "_,_,_,cuarto,quinto = tupla_impares" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "9\n" + ] + } + ], + "source": [ + "print(cuarto)\n", + "print(quinto)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "lista_impares = list(tupla_impares) #convetimos una tupla en una lita" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 3, 5, 7, 9]\n" + ] + } + ], + "source": [ + "print(lista_impares)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(lista_impares)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "nueva_tupla = tuple(lista_impares) #convertimos una lista en una tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 3, 5, 7, 9)\n" + ] + } + ], + "source": [ + "print(nueva_tupla)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(nueva_tupla)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M05_estructuradedatos/Prep_Course_Homework_05.ipynb b/M05_estructuradedatos/Prep_Course_Homework_05.ipynb index c71642967..aac94a6c2 100644 --- a/M05_estructuradedatos/Prep_Course_Homework_05.ipynb +++ b/M05_estructuradedatos/Prep_Course_Homework_05.ipynb @@ -5,7 +5,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Estructuras de Datos" + "1) Crear una lista que contenga nombres de ciudades del mundo que contenga más de 5 elementos e imprimir por pantalla" ] }, { @@ -13,15 +13,26 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "1) Crear una lista que contenga nombres de ciudades del mundo que contenga más de 5 elementos e imprimir por pantalla" + "## Estructuras de Datos" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Montevideo', 'Brasilia']\n" + ] + } + ], + "source": [ + "cuidades = [\"Buenos Aires\",\"Santiago\",\"Asuncion\",\"Montevideo\",\"Brasilia\"]\n", + "print(cuidades)" + ] }, { "attachments": {}, @@ -33,10 +44,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Santiago\n" + ] + } + ], + "source": [ + "print(cuidades[1])" + ] }, { "attachments": {}, @@ -48,10 +69,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Santiago', 'Asuncion', 'Montevideo']\n" + ] + } + ], + "source": [ + "print(cuidades[1:4])" + ] }, { "attachments": {}, @@ -63,10 +94,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(cuidades)" + ] }, { "attachments": {}, @@ -78,10 +122,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Asuncion', 'Montevideo', 'Brasilia']\n" + ] + } + ], + "source": [ + "print(cuidades[2:])" + ] }, { "attachments": {}, @@ -93,10 +147,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Montevideo']\n" + ] + } + ], + "source": [ + "print(cuidades[:4])" + ] }, { "attachments": {}, @@ -108,10 +172,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Tokyo']\n" + ] + } + ], + "source": [ + "cuidades.append(\"Buenos Aires\")\n", + "cuidades.append(\"Tokyo\")\n", + "print(cuidades)" + ] }, { "attachments": {}, @@ -123,10 +199,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Tokyo']\n" + ] + } + ], + "source": [ + "cuidades.insert(3, \"Pekin\")\n", + "print(cuidades)" + ] }, { "attachments": {}, @@ -138,10 +225,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Tokyo', 'Berlin', 'Paris']\n" + ] + } + ], + "source": [ + "cuidades.extend([\"Berlin\", \"Paris\"])\n", + "print(cuidades)" + ] }, { "attachments": {}, @@ -153,10 +251,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "print(cuidades.index(\"Buenos Aires\")) #Da solo un indice aunque esté duplicada" + ] }, { "attachments": {}, @@ -168,10 +276,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "'Moscu' is not in list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[43], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mcuidades\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mMoscu\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[1;31mValueError\u001b[0m: 'Moscu' is not in list" + ] + } + ], + "source": [ + "cuidades.index(\"Moscu\")" + ] }, { "attachments": {}, @@ -183,10 +305,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Berlin', 'Paris']\n" + ] + } + ], + "source": [ + "cuidades.remove(\"Tokyo\")\n", + "print(cuidades)" + ] }, { "attachments": {}, @@ -198,10 +331,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "list.remove(x): x not in list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[45], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mcuidades\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mTokyo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(cuidades)\n", + "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list" + ] + } + ], + "source": [ + "cuidades.remove(\"Tokyo\")\n", + "print(cuidades)" + ] }, { "attachments": {}, @@ -213,10 +361,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Berlin']\n", + "Paris\n" + ] + } + ], + "source": [ + "ultimo_elemento = cuidades.pop()\n", + "print(cuidades)\n", + "print(ultimo_elemento)" + ] }, { "attachments": {}, @@ -228,10 +389,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Berlin', 'Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Berlin', 'Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Berlin', 'Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Berlin']\n" + ] + } + ], + "source": [ + "print(cuidades *4)" + ] }, { "attachments": {}, @@ -243,10 +414,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 64, + "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": [ + "primera_tupla = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)\n", + "print(primera_tupla)\n" + ] }, { "attachments": {}, @@ -258,10 +440,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(11, 12, 13, 14, 15, 16)\n" + ] + } + ], + "source": [ + "print(primera_tupla[10:16])" + ] }, { "attachments": {}, @@ -273,10 +465,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "print(20 in primera_tupla)\n", + "print(30 in primera_tupla)\n" + ] }, { "attachments": {}, @@ -288,10 +492,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Se agrego el Paris en la lista\n", + "['Buenos Aires', 'Santiago', 'Asuncion', 'Pekin', 'Montevideo', 'Brasilia', 'Buenos Aires', 'Berlin', 'Paris']\n" + ] + } + ], + "source": [ + "elemento = \"Paris\"\n", + "if not elemento in cuidades:\n", + " cuidades.append(\"Paris\")\n", + " print(f\"Se agrego el {elemento} en la lista\")\n", + "else:\n", + " print(f\"El elemento {elemento} se encuantra en la lista\") \n", + "print(cuidades)\n" + ] }, { "attachments": {}, @@ -303,10 +524,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "1\n" + ] + } + ], + "source": [ + "print(cuidades.count(\"Buenos Aires\"))\n", + "print(primera_tupla.count(2))" + ] }, { "attachments": {}, @@ -318,10 +551,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 85, + "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_numeros = list(primera_tupla)\n", + "print(lista_numeros)" + ] }, { "attachments": {}, @@ -333,10 +577,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "primero = primera_tupla[0]\n", + "segundo = primera_tupla[1]\n", + "tercero = primera_tupla[2]\n", + "print(primero)\n", + "print(segundo)\n", + "print(tercero)" + ] }, { "attachments": {}, @@ -348,10 +609,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Ciudad': ['Buenos Aires', 'Santiago', 'Asuncion', 'Montevideo', 'Brasilia'], 'Contienente': ['America', 'Africa'], 'Pais': ['Argentina', 'Brasil']}\n" + ] + } + ], + "source": [ + "diccionario = {\"Ciudad\" : cuidades,\n", + " \"Contienente\":[\"America\", \"Africa\"],\n", + " \"Pais\": [\"Argentina\", \"Brasil\"] }\n", + "\n", + "print(diccionario)\n" + ] }, { "attachments": {}, @@ -363,10 +638,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['Ciudad', 'Contienente', 'Pais'])" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diccionario.keys()" + ] }, { "attachments": {}, @@ -378,10 +666,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 109, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Buenos Aires', 'Santiago', 'Asuncion', 'Montevideo', 'Brasilia']\n" + ] + } + ], + "source": [ + "print(diccionario[\"Ciudad\"])" + ] } ], "metadata": { @@ -403,7 +701,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/M05_estructuradedatos/clase_en_vivo. estructura de datos.ipynb b/M05_estructuradedatos/clase_en_vivo. estructura de datos.ipynb new file mode 100644 index 000000000..3a447e70a --- /dev/null +++ b/M05_estructuradedatos/clase_en_vivo. estructura de datos.ipynb @@ -0,0 +1,1230 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Listas" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "lista = [1, 7.4, \"Henry\", True, \"Lationamérica\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 7.4, 'Henry', True, 'Lationamérica']\n" + ] + } + ], + "source": [ + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[7.4, 'Henry', True]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Slicers\n", + "lista[1:4]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[7.4, True]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# steps\n", + "lista[1:4:2]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Henry', True, 'Lationamérica']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "## Modificar\n", + "lista[2] = \"Perro\"" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', True, 'Lationamérica']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', True, 'Lationamérica', False]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## append\n", + "lista.append(False)\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', 'Indice 3', True, 'Lationamérica', False]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## insert\n", + "lista.insert(3, \"Indice 3\")\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## index\n", + "lista.index(\"Lationamérica\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Perro', True, 'Lationamérica', False]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## remove\n", + "lista.remove(\"Indice 3\")\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "[1, 7.4, 'Perro', True, 'Lationamérica']\n" + ] + } + ], + "source": [ + "## pop\n", + "ultimo_elemento = lista.pop()\n", + "print(ultimo_elemento)\n", + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1,\n", + " 7.4,\n", + " 'Perro',\n", + " True,\n", + " 'Lationamérica',\n", + " 'Buenos Aires',\n", + " 'Ciudad de México',\n", + " ' Bogotá']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## extend (concatenar listas)\n", + "segunda_lista = [\"Buenos Aires\", \"Ciudad de México\", \" Bogotá\"]\n", + "lista.extend(segunda_lista)\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 7.4, 'Perro', True, 'Lationamérica', 'Buenos Aires', 'Ciudad de México', ' Bogotá', 1, 7.4, 'Perro', True, 'Lationamérica', 'Buenos Aires', 'Ciudad de México', ' Bogotá']\n" + ] + } + ], + "source": [ + "print(lista * 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1,\n", + " 7.4,\n", + " 'Perro',\n", + " True,\n", + " 'Lationamérica',\n", + " 'Buenos Aires',\n", + " 'Ciudad de México',\n", + " ' Bogotá']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1,\n", + " 7.4,\n", + " 'Perro',\n", + " 'Lationamérica',\n", + " 'Buenos Aires',\n", + " 'Ciudad de México',\n", + " ' Bogotá']" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista.pop(3) \n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 6, 4, 1]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_numero = [6, 4, 10, 1]\n", + "lista_numero.sort(reverse = True)\n", + "lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c']" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_letras = [\"b\", \"a\", \"c\"]\n", + "lista_letras.sort()\n", + "lista_letras" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Contenencia\n", + "10 in lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3 in lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "## Desempaquetar\n", + "letra_1, letra_2, letra_3 = \"a\", \"b\", \"c\"" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "b\n", + "c\n" + ] + } + ], + "source": [ + "letra_1, letra_2, letra_3 = lista_letras\n", + "print(letra_1)\n", + "print(letra_2)\n", + "print(letra_3)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c']\n" + ] + } + ], + "source": [ + "print(lista_letras)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tuplas" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('Python', 4, True, 5.8, 'Henry', 'Python')" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla = (\"Python\", 4, True, 5.8, \"Henry\", \"Python\")\n", + "tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(tupla)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('a', 'b', 'c')" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tuple(lista_letras)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## contenecia\n", + "4 in tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla.count(\"Python\")" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(33, 39, 40, 29, 27, 20)" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Empaquetar\n", + "edades = 33, 39, 40, 29, 27, 20\n", + "edades" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(edades)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "33\n", + "39\n", + "40\n", + "29\n", + "27\n", + "20\n" + ] + } + ], + "source": [ + "## Desempaquetar\n", + "gustavo, laura, jonny, escarlette, felipe, jimmy = edades\n", + "print(gustavo)\n", + "print(laura)\n", + "print(jonny)\n", + "print(escarlette)\n", + "print(felipe)\n", + "print(jimmy)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Diccionarios" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'gustavo': 33, 'laura': 39, 'jonny': 40}" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alumnos = {\"gustavo\": 33, \"laura\": 39, \"jonny\": 40}\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(alumnos)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "39" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Acceder a un valor\n", + "alumnos[\"laura\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'gustavo': 33, 'laura': 39, 'jonny': 25}" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Cambiar un valor\n", + "alumnos[\"jonny\"] = 25\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'gustavo': 33, 'laura': 39, 'jonny': 25, 'escarlette': 29}" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alumnos[\"escarlette\"] = 29\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'laura': 39, 'jonny': 25, 'escarlette': 29}" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## eliminar llave-valor\n", + "del alumnos[\"gustavo\"]\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['laura', 'jonny', 'escarlette'])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## acceder a todas las llaves\n", + "alumnos.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([39, 25, 29])" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Acceder a valor\n", + "alumnos.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('laura', 39), ('jonny', 25), ('escarlette', 29)])" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## tupla de llave-valor\n", + "alumnos.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Iteradores e iterables" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "6\n", + "10\n", + "14\n", + "18\n" + ] + } + ], + "source": [ + "## Iteracion listas o tuplas\n", + "iterable = [1, 3, 5, 7, 9]\n", + "for numero in iterable:\n", + " print(numero * 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PYTHON ES MUY USADO EN CIENCIA DE DATOS" + ] + } + ], + "source": [ + "## Iteracion en texto\n", + "texto = \"Python es muy usado en ciencia de datos\"\n", + "for letra in texto:\n", + " print(letra.upper(), end= \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "## Iteración sobre diccionarios\n", + "paises = {\"España\": \"Europa\", \"México\": \"América\", \"Egipto\": \"Africa\"}" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "España\n", + "México\n", + "Egipto\n" + ] + } + ], + "source": [ + "for pais in paises:\n", + " print(pais)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "España\n", + "México\n", + "Egipto\n" + ] + } + ], + "source": [ + "for pais in paises.keys():\n", + " print(pais)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Europa\n", + "América\n", + "Africa\n" + ] + } + ], + "source": [ + "for continente in paises.values():\n", + " print(continente)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('España', 'Europa')\n", + "('México', 'América')\n", + "('Egipto', 'Africa')\n" + ] + } + ], + "source": [ + "for tupla in paises.items():\n", + " print(tupla)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Iterador" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], + "source": [ + "colores = [\"rojo\", \"verde\", \"azul\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [], + "source": [ + "iterador_colores = iter(colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'rojo'" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'verde'" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'azul'" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[70], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[39mnext\u001b[39;49m(iterador_colores)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [], + "source": [ + "next(iterador_colores, None)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Función zip" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Julian', 33)\n", + "('Esperanza', 39)\n", + "('Gerardo', 42)\n" + ] + } + ], + "source": [ + "nombre = [\"Julian\", \"Esperanza\", \"Gerardo\"]\n", + "edades = [33, 39, 42]\n", + "combinacion = zip(nombre, edades)\n", + "for tupla in combinacion:\n", + " print(tupla)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Comprensión de listas" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['banano', 'mango']\n" + ] + } + ], + "source": [ + "frutas = [\"manzana\", \"banano\", \"mango\", \"fresa\"]\n", + "nueva_lista = []\n", + "\n", + "for fruta in frutas:\n", + " if \"o\" in fruta:\n", + " nueva_lista.append(fruta)\n", + "\n", + "print(nueva_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['banano', 'mango']" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## [valor for variable in iterable if condicion]\n", + "nueva_lista_2 = [fruta for fruta in frutas if \"o\" in fruta]\n", + "nueva_lista_2" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "maestria", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M05_estructuradedatos/practica _clase_en_.ipynb b/M05_estructuradedatos/practica _clase_en_.ipynb new file mode 100644 index 000000000..b752f2c67 --- /dev/null +++ b/M05_estructuradedatos/practica _clase_en_.ipynb @@ -0,0 +1,1018 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lista" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "lista = [1, 7.4, \"Henry\", True, \"Latinoamerica\" ]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 7.4, 'Henry', True, 'Latinoamerica']\n" + ] + } + ], + "source": [ + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Henry'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista[2] # accedo a el dato del indice 2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[7.4, 'Henry', True]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#slicers\n", + "lista[1:4] #devuleve has [n; n-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[7.4, True]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#steps\n", + "lista[1:4:2]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'Henry', True, 'Latinoamerica']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Modificar\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "lista[2] = \"perro\"" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'perro', True, 'Latinoamerica']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'perro', True, 'Latinoamerica', False]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# agregar append\n", + "lista.append(False)\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 7.4, 'perro', 'indice 3', True, 'Latinoamerica', False]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#insert\n", + "lista.insert(3, \"indice 3\")\n", + "lista" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## index busca un elento\n", + "lista.index(\"Latinoamerica\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "list.remove(x): x not in list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\practica _clase_en_.ipynb Cell 14\u001b[0m line \u001b[0;36m2\n\u001b[0;32m 1\u001b[0m \u001b[39m## remove\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m lista\u001b[39m.\u001b[39;49mremove(\u001b[39m\"\u001b[39;49m\u001b[39mindice 3\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n", + "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list" + ] + } + ], + "source": [ + "## remove\n", + "lista.remove(\"indice 3\")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "perro\n", + "[1, 7.4]\n" + ] + } + ], + "source": [ + "##pop\n", + "ultimo_elemento = lista.pop()\n", + "print(ultimo_elemento)\n", + "print(lista)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'list' object has no attribute 'exted'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\practica _clase_en_.ipynb Cell 16\u001b[0m line \u001b[0;36m3\n\u001b[0;32m 1\u001b[0m \u001b[39m##exted (concatenar listas)\u001b[39;00m\n\u001b[0;32m 2\u001b[0m segunda_lista \u001b[39m=\u001b[39m [\u001b[39m\"\u001b[39m\u001b[39mBuenos Aires\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mCuidad de mexico\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m Bogota\u001b[39m\u001b[39m\"\u001b[39m]\n\u001b[1;32m----> 3\u001b[0m lista\u001b[39m.\u001b[39;49mexted(segunda_lista)\n\u001b[0;32m 4\u001b[0m \u001b[39mprint\u001b[39m(lista)\n", + "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'exted'" + ] + } + ], + "source": [ + "##exted (concatenar listas)\n", + "segunda_lista = [\"Buenos Aires\", \"Cuidad de mexico\", \" Bogota\"]\n", + "lista.exted(segunda_lista)\n", + "print(lista)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 7.4, 1, 7.4]\n" + ] + } + ], + "source": [ + "print(lista * 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 6, 4, 1]" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_numero = [6, 4, 10, 1] #ordena\n", + "lista_numero.sort(reverse = True)\n", + "lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_letras = [\"c\", \"a\", \"b\"] #ordena\n", + "lista_letras.sort()\n", + "lista_letras" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## contenecia\n", + "10 in lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3 in lista_numero" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "##desempaquetar\n", + "letra_1, letra_2, letra_3= \"a\", \"b\", \"c\"" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "too many values to unpack (expected 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\practica _clase_en_.ipynb Cell 23\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m letra_1; letra_2, letra_3 \u001b[39m=\u001b[39m lista_letras\n", + "\u001b[1;31mValueError\u001b[0m: too many values to unpack (expected 2)" + ] + } + ], + "source": [ + "letra_1; letra_2, letra_3 = lista_letras" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Tuplas" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('Python', 4, True, 5.8, 'Henry', 'Python')" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla = (\"Python\", 4, True, 5.8, \"Henry\", \"Python\")\n", + "tupla" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "type" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('a', 'b', 'c')" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tuple(lista_letras)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tupla.count(\"Python\")" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(33, 39, 40, 20, 27, 20)" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Empaquetar\n", + "edades = 33, 39, 40, 20, 27, 20\n", + "edades" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(edades)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "33\n", + "33\n", + "33\n", + "33\n", + "33\n", + "33\n" + ] + } + ], + "source": [ + "##desempaquetar\n", + "gustavo, laura, jonny, escarlette, felipe, jimmy = edades\n", + "print(gustavo)\n", + "print(gustavo)\n", + "print(gustavo)\n", + "print(gustavo)\n", + "print(gustavo)\n", + "print(gustavo)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Diccionarios" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Gustavo': 33, 'Laura': 39, 'jonny': 40}" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alumnos = {\"Gustavo\": 33, \"Laura\": 39, \"jonny\": 40}\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(alumnos)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "39" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "##Accder al valor\n", + "alumnos[\"Laura\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Gustavo': 33, 'Laura': 39, 'jonny': 25}" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## cambiar valor.\n", + "alumnos[\"jonny\"] = 25\n", + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'Gustavo'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\practica _clase_en_.ipynb Cell 37\u001b[0m line \u001b[0;36m2\n\u001b[0;32m 1\u001b[0m \u001b[39m## eliminar un par de clave valor\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[39mdel\u001b[39;00m alumnos[\u001b[39m\"\u001b[39;49m\u001b[39mGustavo\u001b[39;49m\u001b[39m\"\u001b[39;49m]\n", + "\u001b[1;31mKeyError\u001b[0m: 'Gustavo'" + ] + } + ], + "source": [ + "## eliminar un par de clave valor\n", + "del alumnos[\"Gustavo\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Laura': 39, 'jonny': 25}" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alumnos" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['Laura', 'jonny'])" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "##acceder a todas las llaves\n", + "alumnos.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([39, 25])" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## aceder a los valores\n", + "alumnos.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('Laura', 39), ('jonny', 25)])" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## tupla de llave valor\n", + "alumnos.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Iteradores e iterables" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "##iterador - iterar sin usar un bucle for" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "colores = [\"rojo\", \"verde\", \"azul\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [], + "source": [ + "iterador_colores = iter(colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'rojo'" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'verde'" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'azul'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(iterador_colores)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Julian\\Desktop\\Python-Prep\\M05_estructuradedatos\\practica _clase_en_.ipynb Cell 49\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m \u001b[39mnext\u001b[39;49m(iterador_colores)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "next(iterador_colores) ##da error xq no hay mas valores" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#Funcion zip" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [], + "source": [ + "nombre = [ \"julian\", \"Esperanza\", \"Gerardo\"]\n", + "edades = [33, 39, 42]" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [], + "source": [ + "combinacion = zip(nombre, edades)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('julian', 33)\n", + "('Esperanza', 39)\n", + "('Gerardo', 42)\n" + ] + } + ], + "source": [ + "for tupla in combinacion:\n", + " print(tupla)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#Compresion de listas" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['banano', 'mango']\n" + ] + } + ], + "source": [ + "frutas = [\"manzana\", \"banano\", \"mango\", \"fresa\"]\n", + "nueva_lista = []\n", + "\n", + "for fruta in frutas:\n", + " if \"o\" in fruta:\n", + " nueva_lista.append(fruta)\n", + " \n", + "print(nueva_lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax. Perhaps you forgot a comma? (1228321974.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[74], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m nuevas_lista_2 = [fruta fruta in frutas if \"o\" in fruta]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax. Perhaps you forgot a comma?\n" + ] + } + ], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M05_estructuradedatos/practica_diccionarios.ipynb b/M05_estructuradedatos/practica_diccionarios.ipynb new file mode 100644 index 000000000..6a2af98c6 --- /dev/null +++ b/M05_estructuradedatos/practica_diccionarios.ipynb @@ -0,0 +1,400 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Diccionarios\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "primer_diccionario= dict()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "print(primer_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(primer_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "primer_diccionario['primero'] = 'uno'" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'primero': 'uno'}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "primer_diccionario" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "primer_diccionario['segundo'] = 'dos'\n", + "primer_diccionario['tercero'] = 'tres'" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'primero': 'uno', 'segundo': 'dos', 'tercero': 'tres'}\n" + ] + } + ], + "source": [ + "print(primer_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "segundo_diccionario = {'cuatro':4, 'cinco': 5, 'seis': 6} " + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'cuatro': 4, 'cinco': 5, 'seis': 6}\n" + ] + } + ], + "source": [ + "print(segundo_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(segundo_diccionario[4]) #Accedemos al valor a travez de la clave" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "mi_diccionario = {'Colores primarios': ['Rojo', 'Amarillo', 'Azul'],\n", + " 'Colores secundarios': ['Naranja', 'Violeta', 'Verde'],\n", + " 'Clave 3': 10,\n", + " 'Clave 4': False }" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Colores primarios': ['Rojo', 'Amarillo', 'Azul'], 'Colores secundarios': ['Naranja', 'Violeta', 'Verde'], 'Clave 3': 10, 'Clave 4': False}\n" + ] + } + ], + "source": [ + "print(mi_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "del mi_diccionario['Clave 4'] #Eliminamos un elemento del diccionario atravez de su clave" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Colores primarios': ['Rojo', 'Amarillo', 'Azul'], 'Colores secundarios': ['Naranja', 'Violeta', 'Verde'], 'Clave 3': 10}\n" + ] + } + ], + "source": [ + "print( mi_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "mi_tupla = ('Argentina', 'Italia', 'Inglaterra') #Utilizamos una tupla como clave de un diccionario" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "mi_diccionario ={mi_tupla[0]: 'Buenos Aires', #Asignamos valores a las claves provenientes de la tupla\n", + " mi_tupla[1]: 'Roma',\n", + " mi_tupla[2]: 'Londres'}" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Argentina': 'Buenos Aires', 'Italia': 'Roma', 'Inglaterra': 'Londres'}\n" + ] + } + ], + "source": [ + "print(mi_diccionario) #se crearon los pares de clave valor" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "mi_diccionario = {'Clave1': 'Valor1', 'Clave2': (1, 2, 3, 4, 5)} #Diccionario con una tupla como valor" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Clave1': 'Valor1', 'Clave2': (1, 2, 3, 4, 5)}\n" + ] + } + ], + "source": [ + "print(mi_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "mi_diccionario = {'Clave1': 'Valor1', 'Clave2': [1, 2, 3, 4, 5]} #Creamos un diccionario con una lista como valor" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Clave1': 'Valor1', 'Clave2': [1, 2, 3, 4, 5]}\n" + ] + } + ], + "source": [ + "print(mi_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "mi_diccionario = {'Clave1': 'Valor1', 'Clave2': {\"numeros\" : [1, 2, 3, 4, 5]}} #Un diccionario dentro de un diccionario" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Clave1': 'Valor1', 'Clave2': {'numeros': [1, 2, 3, 4, 5]}}\n" + ] + } + ], + "source": [ + "print(mi_diccionario)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys(['Clave1', 'Clave2'])\n" + ] + } + ], + "source": [ + "print(mi_diccionario.keys()) #Obtemos las claves del diccionario con la funcion kyes" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_values(['Valor1', {'numeros': [1, 2, 3, 4, 5]}])\n" + ] + } + ], + "source": [ + "print(mi_diccionario.values()) #Obtenemos los valores del diccionario con la funcion values" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(mi_diccionario) #Obtenemos la longitud del diccionario, en este caso dos pares de clave valor" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M06_iterablesiteradores/Clase_iterables_iteradores.ipynb b/M06_iterablesiteradores/Clase_iterables_iteradores.ipynb new file mode 100644 index 000000000..2007470a7 --- /dev/null +++ b/M06_iterablesiteradores/Clase_iterables_iteradores.ipynb @@ -0,0 +1,376 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "4\n", + "9\n", + "2\n" + ] + } + ], + "source": [ + "lista = [5,4,9,2] #para mostrar los valores de la lista por pantalla\n", + "i = 0 # i se utiliza como contador\n", + "while i < len(lista): # mientras i sea menor que la longitud de la listas\n", + " elemento = lista[i] \n", + " print(elemento)\n", + " i +=1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "4\n", + "9\n", + "2\n" + ] + } + ], + "source": [ + "lista = [5,4,9,2] # ambos codigos haven lo mismo aunque el for es mas legible y no requiere una variable de control\n", + "for elemento in lista:\n", + " print(elemento)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "n\n", + "r\n", + "y\n" + ] + } + ], + "source": [ + "cadena = \"Henry\"\n", + "for c in cadena: # se podria leer como: \"poner en C cada elemento presente en la cadena\"\n", + " print(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "incomplete input (3362375983.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[1], line 3\u001b[1;36m\u001b[0m\n\u001b[1;33m #imprime una tupla con el indice y el elemento\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m incomplete input\n" + ] + } + ], + "source": [ + "cadena = \"Henry\" \n", + "for c in enumerate(cadena): #con eneumerate agregamos el indice de cada elemento al imprimir\n", + " #imprime una tupla con el indice y el elemento" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "cadena = \"Henry\"\n", + "numero = 9\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "isinstance(cadena,Iterable)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "isinstance(numero,Iterable)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['H', 'e', 'n', 'r', 'y']\n" + ] + } + ], + "source": [ + "#Metodo list \n", + "\n", + "print(list(\"Henry\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "#Función sum - suma todos los elementos de un iterable\n", + "print(sum([1,2,3]))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H-e-n-r-y\n" + ] + } + ], + "source": [ + "#Metodo join\n", + "\n", + "print(\"-\".join(\"Henry\")) #Obtenemos la cadena con el separador espeficicado en este caso un guión" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "b\n", + "c\n" + ] + } + ], + "source": [ + "# Iteramos en un diccionario\n", + "mi_dict = {\"a\":1, \"b\":2, \"c\":3}\n", + "for i in mi_dict:\n", + " print(i) #nos retorna las tres claves del diccionnario" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "ITERADORES" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "libro = [\"pagina1\", \"pagina2\", \"pagina3\", \"pagina4\"]\n", + "marcapaginas = iter(libro) # Creamos u iterador con la función iter y le pasamos como argumento la lista libro" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list_iterator" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(marcapaginas)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pagina1\n" + ] + } + ], + "source": [ + "print(next(marcapaginas))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pagina2\n" + ] + } + ], + "source": [ + "print(next(marcapaginas))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pagina3\n" + ] + } + ], + "source": [ + "print(next(marcapaginas))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pagina4\n" + ] + } + ], + "source": [ + "print(next(marcapaginas))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmarcapaginas\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "print(next(marcapaginas)) #da error xq llegó al final de la iteración" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M06_iterablesiteradores/Prep_Course_Homework_06-Resuelto.ipynb b/M06_iterablesiteradores/Prep_Course_Homework_06-Resuelto.ipynb index 955bb1fa2..0eedcf226 100644 --- a/M06_iterablesiteradores/Prep_Course_Homework_06-Resuelto.ipynb +++ b/M06_iterablesiteradores/Prep_Course_Homework_06-Resuelto.ipynb @@ -234,7 +234,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -684,7 +684,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/M06_iterablesiteradores/Prep_Course_Homework_06.ipynb b/M06_iterablesiteradores/Prep_Course_Homework_06.ipynb index 75d6c18b1..765bce129 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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]\n" + ] + } + ], + "source": [ + "lista_vacía = []\n", + "numero = -15\n", + "while numero < 0:\n", + " lista_vacía.append(numero)\n", + " numero += 1\n", + "print(lista_vacía) \n" + ] }, { "attachments": {}, @@ -33,10 +48,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-14\n", + "-12\n", + "-10\n", + "-8\n", + "-6\n", + "-4\n", + "-2\n" + ] + } + ], + "source": [ + "n = 0\n", + "while n < len(lista_vacía):\n", + " if lista_vacía[n] %2 ==0:\n", + " print(lista_vacía[n])\n", + " n +=1\n", + "\n" + ] }, { "attachments": {}, @@ -48,10 +84,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-14\n", + "-12\n", + "-10\n", + "-8\n", + "-6\n", + "-4\n", + "-2\n" + ] + } + ], + "source": [ + "for n in lista_vacía:\n", + " if n %2 == 0:\n", + " print(n)" + ] }, { "attachments": {}, @@ -63,10 +117,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-15\n", + "-14\n", + "-13\n" + ] + } + ], + "source": [ + "for i in lista_vacía [:3]:\n", + " print(i)" + ] }, { "attachments": {}, @@ -78,10 +145,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 -15\n", + "1 -14\n", + "2 -13\n", + "3 -12\n", + "4 -11\n", + "5 -10\n", + "6 -9\n", + "7 -8\n", + "8 -7\n", + "9 -6\n", + "10 -5\n", + "11 -4\n", + "12 -3\n", + "13 -2\n", + "14 -1\n" + ] + } + ], + "source": [ + "for indice, elemento in enumerate(lista_vacía):\n", + " print(indice, elemento)\n" + ] }, { "attachments": {}, @@ -93,10 +185,51 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 12, + "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) \n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "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": [ + "n = 1\n", + "while n <= max(lista):\n", + " if n not in lista:\n", + " lista.append()\n", + " n += 1\n", + "lista.sort()\n", + "print(lista) " + ] }, { "attachments": {}, @@ -112,10 +245,72 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "3\n" + ] + } + ], + "source": [ + "lista4= [1, 2, 3, 4]\n", + "print(lista4 [-1])\n", + "print(lista4[-2]) # al poner [-1] hace un slice de el untimos elemento / [-2] del ante ultimo" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "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", + "n = 2\n", + "while n < 30:\n", + " fibo.append(fibo[-1] + fibo[-2])\n", + " n += 1\n", + "print(fibo) " + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "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": [ + "fibonacci= [0, 1]\n", + "\n", + "n = 2\n", + "\n", + "while n < 30:\n", + " fibonacci.append(fibonacci[n - 1] +fibonacci[n - 2])\n", + " n += 1\n", + "\n", + "print(fibonacci) \n", + " " + ] }, { "attachments": {}, @@ -127,10 +322,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1346268\n", + "1346268\n" + ] + } + ], + "source": [ + "print(sum(fibo))\n", + "print(sum(fibonacci))\n" + ] }, { "attachments": {}, @@ -149,10 +356,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "514229 / 317811 = 1.6180339887543225\n", + "317811 / 196418 = 1.618033988738303\n", + "196418 / 121393 = 1.6180339887802426\n", + "121393 / 75025 = 1.6180339886704431\n", + "75025 / 46368 = 1.618033988957902\n" + ] + } + ], + "source": [ + "for i in range (1, 6):\n", + " numerador = fibo[-i]\n", + " denominador = fibo [-i - 1]\n", + " división = numerador / denominador\n", + " print(f\"{numerador} / {denominador} = {división}\") " + ] }, { "attachments": {}, @@ -165,10 +390,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " La letra N aparece en el indice 7 de la cadena Hola Mundo. Esto es una práctica del lenguaje de programación Python\n", + " La letra N aparece en el indice 21 de la cadena Hola Mundo. Esto es una práctica del lenguaje de programación Python\n", + " La letra N aparece en el indice 39 de la cadena Hola Mundo. Esto es una práctica del lenguaje de programación Python\n", + " La letra N aparece en el indice 60 de la cadena Hola Mundo. Esto es una práctica del lenguaje de programación Python\n", + " La letra N aparece en el indice 67 de la cadena Hola Mundo. Esto es una práctica del lenguaje de programación Python\n" + ] + } + ], + "source": [ + "cadena = \"Hola Mundo. Esto es una práctica del lenguaje de programación Python\"\n", + "\n", + "for indice, caracter in enumerate(cadena):\n", + " if caracter == \"n\":\n", + " print(f\" La letra N aparece en el indice {indice} de la cadena \",\"Hola Mundo. Esto es una práctica del lenguaje de programación Python\")\n", + " " + ] }, { "attachments": {}, @@ -180,10 +424,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "clave1\n", + "clave2\n", + "clave3\n" + ] + } + ], + "source": [ + "primer_diccionario ={\"clave1\": \"valor1\",\n", + " \"clave2\": \"valor2\",\n", + " \"clave3\": \"valor2\"}\n", + "\n", + "for i in primer_diccionario:\n", + " print(i)\n" + ] }, { "attachments": {}, @@ -195,10 +456,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Hola Mundo. Esto es una práctica del lenguaje de programación Python" + ] + } + ], + "source": [ + "lista_cadena = list(cadena)\n", + "print(type(lista_cadena))\n", + "\n", + "for i in lista_cadena:\n", + " print(i, end= \"\")" + ] }, { "attachments": {}, @@ -210,10 +486,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "((1, 6), (2, 7), (3, 8), (4, 9), (5, 10))\n" + ] + } + ], + "source": [ + "lista1 = [1,2,3,4,5]\n", + "lista2 = [6,7,8,9,10]\n", + "\n", + "combinacion= zip(lista1, lista2)\n", + "print(type(combinacion))\n", + "tupla_combinacion = tuple(combinacion)\n", + "print(type(tupla_combinacion))\n", + "print(tupla_combinacion)" + ] }, { "attachments": {}, @@ -226,10 +521,47 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 63, + "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", + "divisibles = []\n", + "for i in lis:\n", + " if i %7 == 0:\n", + " divisibles.append(i)\n", + "print(divisibles) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "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] # Definición de la lista original\n", + "\n", + "lis2 = [i for i in lis if i % 7 == 0] # Comprensión de lista para filtrar elementos divisibles por 7\n", + "\n", + "print(lis2) # Impresión de la lista resultante\n" + ] }, { "attachments": {}, @@ -242,10 +574,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n" + ] + } + ], + "source": [ + "lis = [[1,2,3,4],'rojo','verde',[True,False,False],['uno','dos','tres']]\n", + " \n", + "elementos_totales = 0\n", + "\n", + "for elemento in lis:\n", + " if type(elemento) == list:\n", + " elementos_totales += len(elemento)\n", + " else:\n", + " elementos_totales += 1\n", + "\n", + "print(elementos_totales) \n" + ] }, { "attachments": {}, @@ -257,10 +609,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 2, 3, 4], ['rojo'], ['verde'], [True, False, False], ['uno', 'dos', 'tres']]\n" + ] + } + ], + "source": [ + "for i ,e in enumerate(lis): # Iteración sobre cada elemento de la lista 'lis' junto con su índice\n", + " if type(e) != list: # Verificación si el elemento no es una lista\n", + " lis[i] = [e] # Si no es una lista, se reemplaza por una lista que contiene ese elemento\n", + " \n", + "print(lis) \n", + "\n" + ] } ], "metadata": { @@ -282,7 +649,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/M06_iterablesiteradores/Sentecia_zip_y_condicionales.ipynb b/M06_iterablesiteradores/Sentecia_zip_y_condicionales.ipynb new file mode 100644 index 000000000..9935171ca --- /dev/null +++ b/M06_iterablesiteradores/Sentecia_zip_y_condicionales.ipynb @@ -0,0 +1,158 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "lista1= [1,2,3]\n", + "lista2= ['a','b','c']\n", + "combnación = zip(lista1, lista2)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "print(type(combnación))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "print(combnación)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 'a')\n", + "(2, 'b')\n", + "(3, 'c')\n" + ] + } + ], + "source": [ + "for elemento in combnación:\n", + " print(elemento) #Devuelve tuplas con los elementos combinados" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "CONDICIONALES" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 8, 12]\n" + ] + } + ], + "source": [ + "#Utilizando la comprensión de lista optenemos una nueva lista con los numeros pares multiplicados por dos\n", + "\n", + "numeros= [1,2,3,4,5,6]\n", + "pares_por_dos = [x*2 for x in numeros if x%2 == 0]\n", + "print(pares_por_dos)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['r', 'r', 'r', 'r']\n", + "4\n" + ] + } + ], + "source": [ + "#Utilizamos una frase de la cual queremos saber la cantidade letras r que va acontener\n", + "\n", + "frase = \"El perro de san roque no tiene rabo\"\n", + "erres = [i for i in frase if i==\"r\"] #itera cada letra de la frase. Si es una R la añade la lista\n", + "print(erres)\n", + "print(len(erres))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "print(len(erres)) #contamos la cantidad de elementos de la lista " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M07_funciones/Funciones.ipynb b/M07_funciones/Funciones.ipynb new file mode 100644 index 000000000..3c32cbec5 --- /dev/null +++ b/M07_funciones/Funciones.ipynb @@ -0,0 +1,442 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "FUNCIONES" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def calcular_area_rectángulo (base,altura):\n", + " area = base * altura\n", + " return area #con return" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calcular_area_rectángulo(4,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def saludar (nombre, saludo = \"Hola\"):\n", + " print(saludo + \", \" + nombre) # sin return\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola, Julián\n" + ] + } + ], + "source": [ + "saludar(\"Julián\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Buenos Días, María\n" + ] + } + ], + "source": [ + "saludar(\"María\", \"Buenos Días\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def imprimir_mensaje ():\n", + " print(\"Hola Mundo\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola Mundo\n" + ] + } + ], + "source": [ + "imprimir_mensaje()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def sumar (sumando1, sumando2):\n", + " resultado = sumando1 + sumando2\n", + " return resultado" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumar(50, 50 )" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "def dividir (dividendo, divisor):\n", + " cociente = dividendo // divisor\n", + " resto = dividendo % divisor\n", + " return cociente, resto" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 2)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dividir(14,3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "ASIGNAR A UNA VARIABLE EL RESULTADO DE RETORNO DE UNA FUNCION" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "def obtener_edad():\n", + " return 30" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La edad es: 30\n" + ] + } + ], + "source": [ + "edad = obtener_edad()\n", + "print(\"La edad es: \", edad)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "def imprimir_valor_variable(var):\n", + " print(\"El valor de la variable es \"+ str(var))" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El valor de la variable es 22\n" + ] + } + ], + "source": [ + "imprimir_valor_variable(22)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "def ordenar_dos_numeros(num1 = 0, num2 = 0):\n", + " if(num1 > num2):\n", + " return num2, num1\n", + " else:\n", + " return num1, num2" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 4)" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ordenar_dos_numeros(4,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "def dividir(dividendo, divisor = 1):\n", + " if (divisor == 0 ):\n", + " return \"No se puede dividir por cero\"\n", + " else: \n", + " return dividendo // divisor\n" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'No se puede dividir por cero'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dividir(10,0)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'divisor' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[42], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mdivisor\u001b[49m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'divisor' is not defined" + ] + } + ], + "source": [ + "print(divisor) #da error xq es una variable local" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "divisor = 5\n", + "def dividir (dividendo):\n", + " if (dividendo == 0):\n", + " return \"No se puede dividir por cero\"\n", + " else :\n", + " return dividendo / divisor" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ], + "source": [ + "print(dividir(10))" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "print(divisor) # Se puede imprimir xq ahora se trata de una variable global." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "divisor = 5\n", + "def dividir (dividendo, divisor = 1):\n", + " if (dividendo == 0):\n", + " return \"No se puede dividir por cero\"\n", + " else :\n", + " return dividendo / divisor" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10.0\n" + ] + } + ], + "source": [ + "print(dividir(10)) # Al no proporcionar el segundo argumento se utiliza el valor predeterminado de 1 / prioriza la variable local" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "print(divisor)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git "a/M07_funciones/Pasaje_de_par\303\241metros.ipynb" "b/M07_funciones/Pasaje_de_par\303\241metros.ipynb" new file mode 100644 index 000000000..2d6eb307f --- /dev/null +++ "b/M07_funciones/Pasaje_de_par\303\241metros.ipynb" @@ -0,0 +1,398 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Paso por valor\n", + "\n", + "x = 10\n", + "def funcion(entrada):\n", + " entrada = 0 #Python trata a los enteros pasados por valor por lo que no altera la variable original" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "funcion(x) #Dentro de la fucnión se crea una copia local de x por lo que la variable original no es modificada" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 20, 30, 40]\n" + ] + } + ], + "source": [ + "# Paso por referencia\n", + "\n", + "x = [10,20,30]\n", + "\n", + "def funcion(entrada):\n", + " entrada.append(40)\n", + "\n", + "funcion(x)\n", + "print(x) # La variable original es modificada" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 20, 30]\n" + ] + } + ], + "source": [ + "x =[10,20,30]\n", + "def funcion(entrada):\n", + " entrada=[]\n", + "funcion(x)\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140713945451224\n" + ] + } + ], + "source": [ + "x = 10 \n", + "print (id(x)) # Nos de vuelve un identificador único para cada objeto. Nos muestra el espacio de memoria en el que está almacenado el objeto." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140713945450904\n" + ] + } + ], + "source": [ + "def funcion(entrada):\n", + " entrada = 0 \n", + " print(id(entrada)) # Los espacios de momeria son diferentes ya que la variable x de la función es otro elemento\n", + "funcion(x) " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1363688821376\n" + ] + } + ], + "source": [ + "x = [10, 20, 30]\n", + "print(id(x)) # id() de x" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1363688821376\n" + ] + } + ], + "source": [ + "def funcion(entrada):\n", + " entrada.append(40)\n", + " print(id(entrada)) # Los id() son iguales por lo que la lista original se modifica\n", + "funcion(x)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 20, 30, 40]\n" + ] + } + ], + "source": [ + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "x = 1\n", + "y = x\n", + "y = 5" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "5\n" + ] + } + ], + "source": [ + "print(x) # La variable x no se ha modificado\n", + "print(y) " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "x = [1,2]\n", + "y = x\n", + "y.append(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "[1, 2, 3]\n" + ] + } + ], + "source": [ + "print(x)\n", + "print(y) " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1363694901184\n", + "1363694901184\n" + ] + } + ], + "source": [ + "print (id(x))\n", + "print (id(y)) # Referencian al mismo espacio de memoria el cual contiene la lista" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "METODO COPY" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "x = [1,2]\n", + "y = x.copy() # guardamos una copia de x para que solo esta se modifique\n", + "y.append(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2]\n", + "[1, 2, 3]\n" + ] + } + ], + "source": [ + "print(x) \n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1363694861312\n", + "1363694865536\n" + ] + } + ], + "source": [ + "print(id(x)) # por esa razón los espacios de memoria son diferentes\n", + "print(id(y))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "FUNCIONES LAMBDA" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "lambda_producto = lambda x,y : x *y #ejecuta una multiplicación" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lambda_producto(3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M07_funciones/Prep_Course_Homework_07-Resuelto.ipynb b/M07_funciones/Prep_Course_Homework_07-Resuelto.ipynb index 9e73b92ea..287a029ec 100644 --- a/M07_funciones/Prep_Course_Homework_07-Resuelto.ipynb +++ b/M07_funciones/Prep_Course_Homework_07-Resuelto.ipynb @@ -85,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -378,7 +378,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/M07_funciones/Prep_Course_Homework_07.ipynb b/M07_funciones/Prep_Course_Homework_07.ipynb index dc6c4e40d..8165168d9 100644 --- a/M07_funciones/Prep_Course_Homework_07.ipynb +++ b/M07_funciones/Prep_Course_Homework_07.ipynb @@ -18,10 +18,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def verificar_primo(numero):\n", + " es_primo = True\n", + " for divisor in range (2, numero):\n", + " if numero % divisor == 0:\n", + " es_primo = False\n", + " break \n", + " return es_primo " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "verificar_primo(11)" + ] }, { "attachments": {}, @@ -33,10 +61,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def extraer_primos(lista):\n", + " lista_primos = []\n", + " for elemento in lista:\n", + " if verificar_primo(elemento) == True:\n", + " lista_primos.append(elemento)\n", + " return lista_primos \n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "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, 21, 22, 23, 24, 25]\n" + ] + } + ], + "source": [ + "lista_numeros = list (range (1, 26))\n", + "print(lista_numeros)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 5, 7, 11, 13, 17, 19, 23]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "extraer_primos(lista_numeros)" + ] }, { "attachments": {}, @@ -48,10 +121,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({'a': 3, 'b': 1, 'c': 1})" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import Counter #Utilizando el modulo Counter \n", + "lista_prueba = [\"a\", \"a\", \"a\", \"b\", \"c\"]\n", + "diccionario_prueba = Counter(lista_prueba) #Devuelve un diccionario con la cantidad de veces que se repiten los elementos\n", + "diccionario_prueba" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diccionario_prueba.get(\"a\") #la función get nos devuelve el valor asociado a una clave / llave" + ] + }, + { + "cell_type": "code", + "execution_count": 51, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def calcular_moda(lista): ## La función devuelve una tupla con la llave y la cantida de veces que se repite\n", + " conteo = Counter(lista) # Genera un diccionario con los elementos y su numero de repeticiones\n", + " llave = max(conteo, key = conteo.get) # Itera entre los elementos de conteo, devulve la llave de asociada a la mayor cantidad de repeticiones(.get) \n", + " valor = conteo[llave] # utilizamos la llave obtenida para acceder al valor\n", + "\n", + " return llave, valor" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El número 1 es el que más se repite, 3 veces\n" + ] + } + ], + "source": [ + "lista_numeros = [1, 1, 1, 4, 7, 4, 9, 10]\n", + "numero, repeticiones = calcular_moda(lista_numeros) # Desempaquetado de la tupla\n", + "print(f\"El número {numero} es el que más se repite, {repeticiones} veces\")" + ] }, { "attachments": {}, @@ -66,10 +208,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def convertir_grados( valor, origen, destino):\n", + " \n", + " if origen == \"Celsius\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = valor\n", + " elif destino == \"Farenheit\":\n", + " valor_destino = (valor * 9/5) + 32\n", + " elif destino == \"Kelvin\":\n", + " valor_destino = valor + 273.15\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\")\n", + " \n", + " if origen == \"Farenheit\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = (valor -32) * 5/9\n", + " elif destino == \"Farenheit\":\n", + " valor_destino = valor_destino \n", + " elif destino == \"Kelvin\":\n", + " valor_destino = ((valor - 32 )* 5/9) + 273.15\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\") \n", + " \n", + " if origen == \"Kelvin\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = valor - 273.15 \n", + " elif destino == \"Farenheit\":\n", + " valor_destino = ((valor - 273.15) * 9/5) + 32\n", + " elif destino == \"Kelvin\":\n", + " valor_destino = valor\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\")\n", + "\n", + " return valor_destino" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "50.0" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "convertir_grados(10, \"Celsius\", \"Farenheit\")" + ] }, { "attachments": {}, @@ -81,10 +277,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 1 grado de Celsius a Celsius es igul a 2\n", + " 1 grado de Celsius a Kelvin es igul a 275.15\n", + " 1 grado de Celsius a Farenheit es igul a 35.6\n", + " 1 grado de Kelvin a Celsius es igul a -271.15\n", + " 1 grado de Kelvin a Kelvin es igul a 2\n", + " 1 grado de Kelvin a Farenheit es igul a -456.07\n", + " 1 grado de Farenheit a Celsius es igul a -16.666666666666668\n", + " 1 grado de Farenheit a Kelvin es igul a 256.4833333333333\n" + ] + }, + { + "ename": "UnboundLocalError", + "evalue": "cannot access local variable 'valor_destino' where it is not associated with a value", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[21], line 4\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m indice_origen \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m3\u001b[39m):\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m indice_destino \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m (\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m3\u001b[39m):\n\u001b[1;32m----> 4\u001b[0m valor_funcion \u001b[38;5;241m=\u001b[39m \u001b[43mconvertir_grados\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmagnitudes\u001b[49m\u001b[43m[\u001b[49m\u001b[43mindice_origen\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmagnitudes\u001b[49m\u001b[43m[\u001b[49m\u001b[43mindice_destino\u001b[49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m 1 grado de \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmagnitudes[indice_origen]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m a \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmagnitudes[indice_destino]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m es igul a \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mvalor_funcion\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", + "Cell \u001b[1;32mIn[18], line 17\u001b[0m, in \u001b[0;36mconvertir_grados\u001b[1;34m(valor, origen, destino)\u001b[0m\n\u001b[0;32m 15\u001b[0m valor_destino \u001b[38;5;241m=\u001b[39m (valor \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m32\u001b[39m) \u001b[38;5;241m*\u001b[39m \u001b[38;5;241m5\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m9\u001b[39m\n\u001b[0;32m 16\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m destino \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFarenheit\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m---> 17\u001b[0m valor_destino \u001b[38;5;241m=\u001b[39m \u001b[43mvalor_destino\u001b[49m \n\u001b[0;32m 18\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m destino \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mKelvin\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m 19\u001b[0m valor_destino \u001b[38;5;241m=\u001b[39m ((valor \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m32\u001b[39m )\u001b[38;5;241m*\u001b[39m \u001b[38;5;241m5\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m9\u001b[39m) \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m273.15\u001b[39m\n", + "\u001b[1;31mUnboundLocalError\u001b[0m: cannot access local variable 'valor_destino' where it is not associated with a value" + ] + } + ], + "source": [ + "magnitudes = [\"Celsius\", \"Kelvin\", \"Farenheit\"]\n", + "for indice_origen in range(0, 3):\n", + " for indice_destino in range (0, 3):\n", + " valor_funcion = convertir_grados(2, magnitudes[indice_origen], magnitudes[indice_destino] )\n", + " print(f\" 1 grado de {magnitudes[indice_origen]} a {magnitudes[indice_destino]} es igul a {valor_funcion}\")" + ] }, { "attachments": {}, @@ -96,10 +325,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def calcular_factorial (numero):\n", + " if type(numero) != int:\n", + " return \"La entrada debe ser in entero\"\n", + " if numero <= 0:\n", + " return \"La entrada debe ser un positivo\"\n", + " if numero == 1:\n", + " return 1\n", + " numero = numero * calcular_factorial(numero - 1)\n", + "\n", + " return numero" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calcular_factorial(5)" + ] } ], "metadata": { @@ -121,7 +381,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/M07_funciones/Recursividad.ipynb b/M07_funciones/Recursividad.ipynb new file mode 100644 index 000000000..68d032231 --- /dev/null +++ b/M07_funciones/Recursividad.ipynb @@ -0,0 +1,135 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "RECURSIVIDAD" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def factorial(numero):\n", + " if (numero > 1):\n", + " numero = numero * factorial (numero - 1)\n", + " return numero" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "FUNCION HELP" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function print in module builtins:\n", + "\n", + "print(*args, sep=' ', end='\\n', file=None, flush=False)\n", + " Prints the values to a stream, or to sys.stdout by default.\n", + "\n", + " sep\n", + " string inserted between values, default a space.\n", + " end\n", + " string appended after the last value, default a newline.\n", + " file\n", + " a file-like object (stream); defaults to the current sys.stdout.\n", + " flush\n", + " whether to forcibly flush the stream.\n", + "\n" + ] + } + ], + "source": [ + "help(print) #nos devuelve información detallada acerca del elemento" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def factorial(numero):\n", + " 'Calcula el factorial de un número' #Cadena de documentación / incluimos la funcionalidad de la función\n", + " if (numero > 1):\n", + " numero = numero * factorial (numero - 1)\n", + " return numero" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on function factorial in module __main__:\n", + "\n", + "factorial(numero)\n", + " Calcula el factorial de un número\n", + "\n" + ] + } + ], + "source": [ + "help(factorial)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M08_clasesyOOP/Poo_ejercicios.ipynb b/M08_clasesyOOP/Poo_ejercicios.ipynb new file mode 100644 index 000000000..478c2d312 --- /dev/null +++ b/M08_clasesyOOP/Poo_ejercicios.ipynb @@ -0,0 +1,845 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class Gato: \n", + "\n", + " def __init__(self, nombre, edad, raza, sexo):\n", + " self.nombre = nombre\n", + " self.edad = edad\n", + " self.raza = raza\n", + " self.sexo = sexo \n", + "\n", + "#Metodo presentar\n", + "\n", + " def presentar(self):\n", + " return print(\"Nombre:\", self.nombre, \"Edad:\", self.edad, \"Raza:\", self.raza, \"Sexo:\", self.sexo)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "gato1 = Gato(\"Pompón\", 2, \"Siames\", \"Macho\") " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nombre: Pompón Edad: 2 Raza: Siames Sexo: Macho\n" + ] + } + ], + "source": [ + "gato1.presentar()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Pilares POO\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "#Encapsulamiento\n", + "\n", + "class CuentaBancaria:\n", + " def __init__(self, saldo):\n", + " self.__saldo # El atributo está encapsulado con el __\n", + " \n", + " def depositar(self, monto):\n", + " self.__saldo += monto\n", + "\n", + " def retirar (self, monto):\n", + " if self.__saldo >= monto:\n", + " self.__saldo -= monto\n", + " else:\n", + " print(\"Saldo insuficiente\")\n", + "\n", + " def obtener_saldo (self):\n", + " return self.__saldo\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "#Herencia\n", + "\n", + "gato2 = Gato (\"Nina\", 3, \"Mezcla\",\"Hembra\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nombre: Nina Edad: 3 Raza: Mezcla Sexo: Hembra\n" + ] + } + ], + "source": [ + "gato2.presentar()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "#Polimorfismo\n", + "\n", + "class Animal:\n", + " def __init__(self, especie, edad, color): \n", + " self.especie = especie\n", + " self.edad = edad\n", + " self.color = color\n", + "\n", + " def mePresento (self):\n", + " print(\"Hola, soy\", self.especie, \"de color\", self.color, \"y tengo\", self.edad, \"años\")\n", + "\n", + " def cumplirAños (self):\n", + " self.edad = self.edad + 1" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "a1 = Animal (\"Raton\", 2, \"Marrón\")\n", + "a2 = Animal (\"Liebre\", 3, \"Gris\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Raton\n", + "2\n", + "Liebre\n", + "3\n" + ] + } + ], + "source": [ + "print(a1.especie)\n", + "print(a1.edad)\n", + "print(a2.especie)\n", + "print(a2.edad)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola, soy Raton de color Marrón y tengo 2 años\n" + ] + } + ], + "source": [ + "a1.mePresento()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola, soy Liebre de color Gris y tengo 3 años\n" + ] + } + ], + "source": [ + "a2.mePresento()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "a1.cumplirAños()\n", + "a2.cumplirAños()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola, soy Raton de color Marrón y tengo 3 años\n" + ] + } + ], + "source": [ + "a1.mePresento()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola, soy Liebre de color Gris y tengo 4 años\n" + ] + } + ], + "source": [ + "a2.mePresento()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Herencia POO" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "class Animal: #Clase base\n", + " def __init__(self, especie, edad): \n", + " self.especie = especie\n", + " self.edad = edad\n", + " # Metodo generico con implementación particular\n", + "\n", + " def hablar (self):\n", + " #Metodo vacio\n", + " pass\n", + " # Metodo generico con implementación particular\n", + " \n", + " def moverse(self):\n", + " #Metodo vacio\n", + " pass\n", + " # Metodo generico con la misma implementación\n", + " def describeme(self):\n", + " print(\"Soy un animal del tipo\", type(self).__name__)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "# Perro hereda de animal\n", + "\n", + "class Perro(Animal):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Soy un animal del tipo Perro\n" + ] + } + ], + "source": [ + "mi_perro = Perro (\"Mamifero\", 10)\n", + "mi_perro.describeme()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "class Perro(Animal):\n", + " def hablar(self):\n", + " print(\"Guau!\")\n", + " def moverse (self):\n", + " print(\"Caminar con cuatro patas\") " + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "class Vaca(Animal):\n", + " def hablar(self):\n", + " print(\"Muuuu!\")\n", + " def moverse (self):\n", + " print(\"Caminar con cuatro patas\") " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "class Abeja(Animal):\n", + " def hablar(self):\n", + " print(\"Bzzzz!\")\n", + " def moverse (self):\n", + " print(\"Volando\")\n", + " # Nuevo método\n", + "\n", + " def picar(self):\n", + " print(\"Picar\") " + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "mi_perro = Perro(\"Mamifero\", 10) #Instanciamos tres objetos\n", + "mi_vaca = Vaca (\"Mamifero\", 23)\n", + "mi_abeja = Abeja(\"Insecto\", 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Muuuu!\n" + ] + } + ], + "source": [ + "mi_vaca.hablar()" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Soy un animal del tipo Abeja\n" + ] + } + ], + "source": [ + "mi_abeja.describeme()" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Picar\n" + ] + } + ], + "source": [ + "mi_abeja.picar()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Función super()" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "class Animal: #Clase base\n", + " def __init__(self, especie, edad): \n", + " self.especie = especie\n", + " self.edad = edad\n", + " # Metodo generico con implementación particular\n", + "\n", + " def hablar (self):\n", + " #Metodo vacio\n", + " pass\n", + " # Metodo generico con implementación particular\n", + " \n", + " def moverse(self):\n", + " #Metodo vacio\n", + " pass\n", + " # Metodo generico con la misma implementación\n", + " def describeme(self):\n", + " print(\"Soy un animal del tipo\", type(self).__name__)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "class Perro:\n", + " def __init__(self, especie, edad, dueño): #Añadimos atributo en el constructor \n", + " self.especie = especie\n", + " self.edad = edad\n", + " self.dueño = dueño" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "#Añadimos atributo con la funsión super\n", + "\n", + "class Perro(Animal):\n", + " def __init__(self, especie, edad, dueño):\n", + " super().__init__(especie,edad) #Llamamos al contructor de la clase base y luego agragar el atributo dueño\n", + " self.dueño = dueño #Agrega el atributo nuevo" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "nuevo_perro = Perro (\"Mamifero\", 7 , \"Luis\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mamifero\n", + "7\n", + "Luis\n" + ] + } + ], + "source": [ + "print(nuevo_perro.especie) #Accedemos a los atributos de la instacia de la clase perro recién creada\n", + "print(nuevo_perro.edad)\n", + "print(nuevo_perro.dueño)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "LIBRERIAS Y MODULOS" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4.0\n" + ] + } + ], + "source": [ + "# Libreria math\n", + "\n", + "import math\n", + "\n", + "x = 16\n", + "raiz_cuarada = math.sqrt(x)\n", + "\n", + "print(raiz_cuarada)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "1\n" + ] + } + ], + "source": [ + "#utilizo mi modulo.py importando todo su contenido \n", + "\n", + "import mi_modulo\n", + "\n", + "print(mi_modulo.suma(4,3))\n", + "print(mi_modulo.resta(10,9))" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], + "source": [ + "#importarmos solo los elementos que nos interesan\n", + "\n", + "from mi_modulo import suma\n", + "\n", + "print(suma(8,6))" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n" + ] + } + ], + "source": [ + "#Podemos importar todo el modulo haciendo uso del asterisco\n", + "\n", + "from mi_modulo import *\n", + "\n", + "print(resta(22, 10))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "RUTAS Y USO DE SYS.PATH" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola\n", + "None\n" + ] + } + ], + "source": [ + "from mi_modulo import *\n", + "print (hola())" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['c:\\\\Users\\\\Julian\\\\Desktop\\\\Python-Prep\\\\M08_clasesyOOP', 'c:\\\\Users\\\\Julian\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python312\\\\python312.zip', 'c:\\\\Users\\\\Julian\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python312\\\\DLLs', 'c:\\\\Users\\\\Julian\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python312\\\\Lib', 'c:\\\\Users\\\\Julian\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python312', '', 'C:\\\\Users\\\\Julian\\\\AppData\\\\Roaming\\\\Python\\\\Python312\\\\site-packages', 'C:\\\\Users\\\\Julian\\\\AppData\\\\Roaming\\\\Python\\\\Python312\\\\site-packages\\\\win32', 'C:\\\\Users\\\\Julian\\\\AppData\\\\Roaming\\\\Python\\\\Python312\\\\site-packages\\\\win32\\\\lib', 'C:\\\\Users\\\\Julian\\\\AppData\\\\Roaming\\\\Python\\\\Python312\\\\site-packages\\\\Pythonwin', 'c:\\\\Users\\\\Julian\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python312\\\\Lib\\\\site-packages']\n" + ] + } + ], + "source": [ + "import sys\n", + "print(sys.path) " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import mi_modulo as m\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola\n", + "None\n" + ] + } + ], + "source": [ + "print(m.hola())" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '__vsc_ipynb_file__', '_dh', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i2', '_i3', '_i4', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'hola', 'm', 'open', 'quit', 'resta', 'suma', 'sys']\n" + ] + } + ], + "source": [ + "print(dir())" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c:\\Users\\Julian\\Desktop\\Python-Prep\\M08_clasesyOOP\\mi_modulo.py\n" + ] + } + ], + "source": [ + "print(m.__file__)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'hola', 'resta', 'suma']\n" + ] + } + ], + "source": [ + "import mi_modulo\n", + "print(dir(mi_modulo))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mi_modulo\n" + ] + } + ], + "source": [ + "print(mi_modulo.__name__)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c:\\Users\\Julian\\Desktop\\Python-Prep\\M08_clasesyOOP\\mi_modulo.py\n" + ] + } + ], + "source": [ + "print(mi_modulo.__file__)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'moduloquenoexiste'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[17], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmoduloquenoexiste\u001b[39;00m\n", + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'moduloquenoexiste'" + ] + } + ], + "source": [ + "import moduloquenoexiste" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La suma es : 3\n" + ] + } + ], + "source": [ + "import modulo" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "import modulo" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import mi_modulo\n", + "import importlib\n", + "\n", + "importlib.reload(mi_modulo)\n", + "importlib.reload(mi_modulo)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M08_clasesyOOP/Prep_Course_Homework_08-Resuelto.ipynb b/M08_clasesyOOP/Prep_Course_Homework_08-Resuelto.ipynb index cdaa0f8e5..4f4a0170f 100644 --- a/M08_clasesyOOP/Prep_Course_Homework_08-Resuelto.ipynb +++ b/M08_clasesyOOP/Prep_Course_Homework_08-Resuelto.ipynb @@ -200,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -281,7 +281,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -670,7 +670,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/M08_clasesyOOP/Prep_Course_Homework_08.ipynb b/M08_clasesyOOP/Prep_Course_Homework_08.ipynb index 3d21ca077..84e7ccfcf 100644 --- a/M08_clasesyOOP/Prep_Course_Homework_08.ipynb +++ b/M08_clasesyOOP/Prep_Course_Homework_08.ipynb @@ -21,10 +21,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Vehículo:\n", + "\n", + " def __init__(self, color, tipo, cilindrada):\n", + " self.color = color\n", + " self.tipo = tipo\n", + " self.cilindrada = cilindrada" + ] }, { "attachments": {}, @@ -39,10 +46,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Vehículo:\n", + "\n", + " def __init__(self, color, tipo, cilindrada):\n", + " self.color = color\n", + " self.tipo = tipo\n", + " self.cilindrada = cilindrada\n", + " \n", + " def acelerar(self, velocidad):\n", + " print(f\"Acelerando a {velocidad}\")\n", + " def frenar(self):\n", + " print(\"Frenando\")\n", + " def doblar(self,direccción):\n", + " print(f\"Doblando hacia la {direccción}\")\n", + " " + ] }, { "attachments": {}, @@ -54,10 +76,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "Auto = Vehículo(\"Rojo\", \"Automovil\", \"1000 cm3\")\n", + "Moto = Vehículo(\"Azul\", \"Motocicleta\", \"125 cm3\")\n", + "Camioneta = Vehículo(\"Negro\", \"Camioneta\", \"3000 cm3\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Acelerando a 50 km/h\n", + "Frenando\n", + "Doblando hacia la Izquierda\n" + ] + } + ], + "source": [ + "Auto.acelerar(\"50 km/h\")\n", + "Moto.frenar()\n", + "Camioneta.doblar(\"Izquierda\")" + ] }, { "attachments": {}, @@ -69,10 +116,96 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Vehículo:\n", + "\n", + " def __init__(self, color, tipo, cilindrada):\n", + " self.color = color\n", + " self.tipo = tipo\n", + " self.cilindrada = cilindrada\n", + " \n", + " def acelerar(self, velocidad):\n", + " print(f\"Acelerando a {velocidad}\")\n", + " def frenar(self):\n", + " print(\"Frenando\")\n", + " def doblar(self,direccción):\n", + " print(f\"Doblando hacia la {direccción}\")\n", + "\n", + " def estado(self):\n", + " self.velocidad = 0\n", + " self.direccción = 0\n", + " \n", + " print(f\"El vehículo se desplaza a {self.velocidad}, el la dirección {self.direccción}\")\n", + " \n", + " def descripción(self):\n", + " print(f\"Vehiculo de color {self.color}, del tipo {self.tipo} y de {self.cilindrada} de cilindrada\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "Auto = Vehículo(\"Rojo\", \"Automovil\", \"1000 cm3\")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Vehiculo de color Rojo, del tipo Automovil y de 1000 cm3 de cilindrada\n" + ] + } + ], + "source": [ + "Auto.descripción()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Acelerando a 50 km/h\n", + "Doblando hacia la Izquierda\n" + ] + } + ], + "source": [ + "Auto.acelerar(\"50 km/h\")\n", + "Auto.doblar(\"Izquierda\")" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El vehículo se desplaza a 0, el la dirección 0\n" + ] + } + ], + "source": [ + "Auto.estado()" + ] }, { "attachments": {}, @@ -88,10 +221,92 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "class Funciones:\n", + "\n", + " def __init__(self) -> None:\n", + " pass\n", + " \n", + " def verificar_primo(self, numero):\n", + " es_primo = True\n", + " for divisor in range (2, numero):\n", + " if numero % divisor == 0:\n", + " es_primo = False\n", + " break \n", + " return es_primo\n", + " \n", + "\n", + " def calcular_moda(self, lista):\n", + "\n", + " from collections import Counter \n", + "\n", + " conteo = Counter(lista)\n", + " llave = max(conteo, key = conteo.get)\n", + " valor = conteo[llave] \n", + "\n", + " return llave, valor\n", + "\n", + "\n", + " def convertir_grados(self, valor, origen, destino):\n", + " \n", + " if origen == \"Celsius\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = valor\n", + " elif destino == \"Farenheit\":\n", + " valor_destino = (valor * 9/5) + 32\n", + " elif destino == \"Kelvin\":\n", + " valor_destino = valor + 273.15\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\")\n", + " \n", + " if origen == \"Farenheit\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = (valor -32) * 5/9\n", + " elif destino == \"Farenheit\":\n", + " valor_destino = valor_destino \n", + " elif destino == \"Kelvin\":\n", + " valor_destino = ((valor - 32 )* 5/9) + 273.15\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\") \n", + " \n", + " if origen == \"Kelvin\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = valor - 273.15 \n", + " elif destino == \"Farenheit\":\n", + " valor_destino = ((valor - 273.15) * 9/5) + 32\n", + " elif destino == \"Kelvin\":\n", + " valor_destino = valor\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\")\n", + "\n", + " return valor_destino\n", + "\n", + " def calcular_factorial (self,numero):\n", + "\n", + " if type(numero) != int:\n", + " return \"La entrada debe ser in entero\"\n", + " if numero <= 0:\n", + " return \"La entrada debe ser un positivo\"\n", + " if numero == 1:\n", + " return 1\n", + " numero = numero * self.calcular_factorial(numero - 1)\n", + "\n", + " return numero \n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "f = Funciones()" + ] }, { "attachments": {}, @@ -103,10 +318,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.calcular_factorial(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "323.15" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.convertir_grados(50, \"Celsius\", \"Kelvin\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 4)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.calcular_moda([1,2,3,4,1,1,1])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.verificar_primo(11)" + ] }, { "attachments": {}, @@ -118,10 +406,81 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Funciones:\n", + "\n", + " def __init__(self, lista_numeros):\n", + " self.lista = lista_numeros\n", + " \n", + " def verificar_primo(self, numero):\n", + " es_primo = True\n", + " for divisor in range (2, numero):\n", + " if numero % divisor == 0:\n", + " es_primo = False\n", + " break \n", + " return es_primo\n", + " \n", + "\n", + " def calcular_moda(self, lista):\n", + "\n", + " from collections import Counter \n", + "\n", + " conteo = Counter(lista)\n", + " llave = max(conteo, key = conteo.get)\n", + " valor = conteo[llave] \n", + "\n", + " return llave, valor\n", + "\n", + "\n", + " def convertir_grados(self, valor, origen, destino):\n", + " \n", + " if origen == \"Celsius\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = valor\n", + " elif destino == \"Farenheit\":\n", + " valor_destino = (valor * 9/5) + 32\n", + " elif destino == \"Kelvin\":\n", + " valor_destino = valor + 273.15\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\")\n", + " \n", + " if origen == \"Farenheit\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = (valor -32) * 5/9\n", + " elif destino == \"Farenheit\":\n", + " valor_destino = valor_destino \n", + " elif destino == \"Kelvin\":\n", + " valor_destino = ((valor - 32 )* 5/9) + 273.15\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\") \n", + " \n", + " if origen == \"Kelvin\":\n", + " if destino == \"Celsius\":\n", + " valor_destino = valor - 273.15 \n", + " elif destino == \"Farenheit\":\n", + " valor_destino = ((valor - 273.15) * 9/5) + 32\n", + " elif destino == \"Kelvin\":\n", + " valor_destino = valor\n", + " else:\n", + " print(\"Debe ingresar un destino correcto\")\n", + "\n", + " return valor_destino\n", + "\n", + " def calcular_factorial (self,numero):\n", + "\n", + " if type(numero) != int:\n", + " return \"La entrada debe ser in entero\"\n", + " if numero <= 0:\n", + " return \"La entrada debe ser un positivo\"\n", + " if numero == 1:\n", + " return 1\n", + " numero = numero * self.calcular_factorial(numero - 1)\n", + "\n", + " return numero " + ] }, { "attachments": {}, @@ -133,10 +492,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from modulos_punto7 import*" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "f2 = Funciones([1,2,2,3,4,5,6,7,8,9,1,1,1,1,])" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f2.calcular_factorial(5)" + ] } ], "metadata": { @@ -158,7 +548,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.11.8" } }, "nbformat": 4, diff --git a/M08_clasesyOOP/__pycache__/ejemplo.cpython-312.pyc b/M08_clasesyOOP/__pycache__/ejemplo.cpython-312.pyc new file mode 100644 index 000000000..0573e5977 Binary files /dev/null and b/M08_clasesyOOP/__pycache__/ejemplo.cpython-312.pyc differ diff --git a/M08_clasesyOOP/__pycache__/mi_modulo.cpython-312.pyc b/M08_clasesyOOP/__pycache__/mi_modulo.cpython-312.pyc new file mode 100644 index 000000000..95edf9d97 Binary files /dev/null and b/M08_clasesyOOP/__pycache__/mi_modulo.cpython-312.pyc differ diff --git a/M08_clasesyOOP/__pycache__/modulo.cpython-312.pyc b/M08_clasesyOOP/__pycache__/modulo.cpython-312.pyc new file mode 100644 index 000000000..6353e820f Binary files /dev/null and b/M08_clasesyOOP/__pycache__/modulo.cpython-312.pyc differ diff --git a/M08_clasesyOOP/__pycache__/modulos_punto7.cpython-312.pyc b/M08_clasesyOOP/__pycache__/modulos_punto7.cpython-312.pyc new file mode 100644 index 000000000..bf3a9897b Binary files /dev/null and b/M08_clasesyOOP/__pycache__/modulos_punto7.cpython-312.pyc differ diff --git a/M08_clasesyOOP/ejemplo.py b/M08_clasesyOOP/ejemplo.py new file mode 100644 index 000000000..76d41ffe9 --- /dev/null +++ b/M08_clasesyOOP/ejemplo.py @@ -0,0 +1,14 @@ +# ejemplo.py + +print (dir()) + +print(__file__) + +mi_variable = "Python" + +def mi_funcion(): + pass + +print(dir()) + + diff --git a/M08_clasesyOOP/mi_modulo.py b/M08_clasesyOOP/mi_modulo.py new file mode 100644 index 000000000..259bb42e8 --- /dev/null +++ b/M08_clasesyOOP/mi_modulo.py @@ -0,0 +1,11 @@ +# mi modulo + +def suma(a,b): + return a + b + +def resta (a,b): + return a - b +def hola(): + print ("Hola") + + diff --git a/M08_clasesyOOP/modulo.py b/M08_clasesyOOP/modulo.py new file mode 100644 index 000000000..019866d63 --- /dev/null +++ b/M08_clasesyOOP/modulo.py @@ -0,0 +1,12 @@ +#def suma1(a,b): +# return a + b + +#c = suma1(1,2) +#print( "La suma es :", c) + +def suma1(a,b): + return a + b +if (__name__ == "__main__"): + c = suma1(1,2) + print( "La suma es :", c) + \ No newline at end of file diff --git a/M08_clasesyOOP/modulos_punto7.py b/M08_clasesyOOP/modulos_punto7.py new file mode 100644 index 000000000..19791f664 --- /dev/null +++ b/M08_clasesyOOP/modulos_punto7.py @@ -0,0 +1,70 @@ +class Funciones: + + def __init__(self, lista_numeros): + self.lista = lista_numeros + + def verificar_primo(self, numero): + es_primo = True + for divisor in range (2, numero): + if numero % divisor == 0: + es_primo = False + break + return es_primo + + + def calcular_moda(self, lista): + + from collections import Counter + + conteo = Counter(lista) + llave = max(conteo, key = conteo.get) + valor = conteo[llave] + + return llave, valor + + + def convertir_grados(self, valor, origen, destino): + + if origen == "Celsius": + if destino == "Celsius": + valor_destino = valor + elif destino == "Farenheit": + valor_destino = (valor * 9/5) + 32 + elif destino == "Kelvin": + valor_destino = valor + 273.15 + else: + print("Debe ingresar un destino correcto") + + if origen == "Farenheit": + if destino == "Celsius": + valor_destino = (valor -32) * 5/9 + elif destino == "Farenheit": + valor_destino = valor_destino + elif destino == "Kelvin": + valor_destino = ((valor - 32 )* 5/9) + 273.15 + else: + print("Debe ingresar un destino correcto") + + if origen == "Kelvin": + if destino == "Celsius": + valor_destino = valor - 273.15 + elif destino == "Farenheit": + valor_destino = ((valor - 273.15) * 9/5) + 32 + elif destino == "Kelvin": + valor_destino = valor + else: + print("Debe ingresar un destino correcto") + + return valor_destino + + def calcular_factorial (self,numero): + + if type(numero) != int: + return "La entrada debe ser in entero" + if numero <= 0: + return "La entrada debe ser un positivo" + if numero == 1: + return 1 + numero = numero * self.calcular_factorial(numero - 1) + + return numero \ No newline at end of file diff --git a/M09_errorhandling/Practica1.ipynb b/M09_errorhandling/Practica1.ipynb new file mode 100644 index 000000000..1ae946bc2 --- /dev/null +++ b/M09_errorhandling/Practica1.ipynb @@ -0,0 +1,93 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "## Pruebas de caja negra\n", + "import unittest" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def suma (num1, num2):\n", + " return num1 + num2" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "class CajaNegraTest(unittest.TestCase):\n", + "\n", + " def test_suma_dos_positivos(self):\n", + " num1 = 10\n", + " num2 = 5\n", + "\n", + " resultado = suma(num1, num2)\n", + "\n", + " self.assertEqual(resultado, 15)\n", + "\n", + " def test_suma_dos_negativos(self):\n", + " num1 = -10\n", + " num2= -7\n", + "\n", + " resultado = suma(num1, num2)\n", + "\n", + " self.assertEqual(resultado, -17)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'unittest' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43munittest\u001b[49m\u001b[38;5;241m.\u001b[39mmain(argv \u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m], verbosity\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m, exit \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[0;32m 2\u001b[0m test_suma_dos_positivos(__main__\u001b[38;5;241m.\u001b[39mCajaNegraTest)\n\u001b[0;32m 3\u001b[0m test_suma_dos_negativos(__main__\u001b[38;5;241m.\u001b[39mCajaNegraTest)\n", + "\u001b[1;31mNameError\u001b[0m: name 'unittest' is not defined" + ] + } + ], + "source": [ + "unittest.main(argv =[\"\"], verbosity=2, exit = False)\n", + "test_suma_dos_positivos(__main__.CajaNegraTest)\n", + "test_suma_dos_negativos(__main__.CajaNegraTest)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M09_errorhandling/Practica2.ipynb b/M09_errorhandling/Practica2.ipynb new file mode 100644 index 000000000..4095c67a5 --- /dev/null +++ b/M09_errorhandling/Practica2.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import unittest" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def es_mayor_de_edad(edad):\n", + " if edad >= 18:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Prueba_de_cristal_test(unittest.TestCase):\n", + "\n", + " def es_mayor_de_edad(self): # El metodo verifica si la función devuelve true si se le da una edad de 20\n", + "\n", + " edad = 20 # Definimos una variable con un valor de 20\n", + "\n", + " resultado = es_mayor_de_edad(edad) # llamamos a la función pasando edad como argumento y se almacena el resultado en la variable resultado\n", + "\n", + " self.assertEqual(resultado, True) # metodo de la clase test.case para comprobar si el resultado es True\n", + " \n", + " \n", + " def es_menor_de_edad(self): # El metodo verifica si la función devuelve false\n", + "\n", + " edad = 15 \n", + "\n", + " resultado = es_mayor_de_edad(edad) \n", + "\n", + " self.assertEqual(resultado, False)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n", + "----------------------------------------------------------------------\n", + "Ran 0 tests in 0.000s\n", + "\n", + "NO TESTS RAN\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unittest.main(argv= [\"\"], verbosity=2, exit = False)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M09_errorhandling/debugging.ipynb b/M09_errorhandling/debugging.ipynb new file mode 100644 index 000000000..5ca79aacb --- /dev/null +++ b/M09_errorhandling/debugging.ipynb @@ -0,0 +1,82 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def multiplica_por_dos(lista):\n", + " lista_multiplicada = []\n", + " for num in lista:\n", + " lista_multiplicada.append(num * 2)\n", + " return lista_multiplicada\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def divide_elementos_de_lista(lista, divisor):\n", + " '''\n", + " Cada elemento de una lista es dividida por un divisor definido.\n", + " En caso de error de tipo ZeroDivisionError que\n", + " significa error al dividir en cero\n", + " la función devuelve la lista inicial\n", + " '''\n", + "\n", + " try:\n", + " return [i / divisor for i in lista]\n", + "\n", + " except ZeroDivisionError as e:\n", + " print(e)\n", + " return lista\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "division by zero\n", + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "lista = list(range(10))\n", + "divisor = 0\n", + "\n", + "print(divide_elementos_de_lista(lista, divisor))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/M09_errorhandling/exep_afirm.ipynb b/M09_errorhandling/exep_afirm.ipynb new file mode 100644 index 000000000..b45e6db3d --- /dev/null +++ b/M09_errorhandling/exep_afirm.ipynb @@ -0,0 +1,117 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def dividir(num1, num2):\n", + " try:\n", + " resultado = num1 / num2\n", + " return resultado\n", + " except ZeroDivisionError:\n", + " print(\"No se puede dividir por cero.\")\n", + " return None" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No se puede dividir por cero.\n" + ] + } + ], + "source": [ + "dividir(12, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El archivo no existe.\n" + ] + } + ], + "source": [ + "try:\n", + " with open(\"archivo.txt\", \"r\") as f:\n", + " contenido = f.read()\n", + "except FileNotFoundError:\n", + " print(\"El archivo no existe.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def primera_letra(lista_de_palabras):\n", + " primeras_letras = []\n", + "\n", + " for palabra in lista_de_palabras:\n", + " assert type(palabra) == str, f'{palabra} no es str'\n", + " assert len(palabra) > 0, 'No se permiten str vacíos'\n", + "\n", + " primeras_letras.append(palabra[0])\n", + " return primeras_letras" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "42 no es str", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mprimera_letra\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHola\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mMundo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[1;32mIn[4], line 5\u001b[0m, in \u001b[0;36mprimera_letra\u001b[1;34m(lista_de_palabras)\u001b[0m\n\u001b[0;32m 2\u001b[0m primeras_letras \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m palabra \u001b[38;5;129;01min\u001b[39;00m lista_de_palabras:\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(palabra) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mstr\u001b[39m, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpalabra\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m no es str\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(palabra) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mNo se permiten str vacíos\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 8\u001b[0m primeras_letras\u001b[38;5;241m.\u001b[39mappend(palabra[\u001b[38;5;241m0\u001b[39m])\n", + "\u001b[1;31mAssertionError\u001b[0m: 42 no es str" + ] + } + ], + "source": [ + "primera_letra([\"Hola\", \"Mundo\", 42])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}