Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #28

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Main #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 382 additions & 0 deletions 03 - Flujos de Control/Mi_TareaResuelta.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"1) Crear una variable que contenga un elemento del conjunto de números enteros y luego imprimir por pantalla si es mayor o menor a cero"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = -2\n",
"if a > 0:\n",
" print(\"a es mayor a cero\")\n",
"else:\n",
" print(\"es menor a cero\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"2) Crear dos variables y un condicional que informe si son del mismo tipo de dato\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 10\n",
"b = \"10\"\n",
"if type(a)==type(b):\n",
" print(\"a y b son del mismo tipo\")\n",
"else:\n",
" print(\"Son de diferente tipo\")\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"3) Para los valores enteros del 1 al 20, imprimir por pantalla si es par o impar\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,21):\n",
" if i%2==0:\n",
" print(\"El \",i,\" es par\")\n",
" else:\n",
" print(\"El \",i,\" es impar\")\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"4) En un ciclo for mostrar para los valores entre 0 y 5 el resultado de elevarlo a la potencia igual a 3\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for num in range(0,6):\n",
" print(num,\" elevado a la tercera potencia es: \",num**3)\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"5) Crear una variable que contenga un número entero y realizar un ciclo for la misma cantidad de ciclos\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 6\n",
"for rep in range(x):\n",
" print(\"Esta es la repetición: \", rep)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"6) Utilizar un ciclo while para realizar el factorial de un número guardado en una variable, sólo si la variable contiene un número entero mayor a 0\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"120\n"
]
}
],
"source": [
"num = 5\n",
"factorial = 1\n",
"if (num > 0):\n",
" factorial = num\n",
" while (num > 2):\n",
" num -=1\n",
" factorial = num*factorial\n",
" print(factorial)\n",
"else:\n",
" print(\"La variable no es mayor a cero\")\n",
" \n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"7) Crear un ciclo for dentro de un ciclo while\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = 0\n",
"while y < 5:\n",
" for i in range(1,y):\n",
" print(\"For\", i)\n",
" print(\"While\", y)\n",
" y+=1\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"8) Crear un ciclo while dentro de un ciclo for\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"n = 4\n",
"for i in range(1,n):\n",
" while (n < 4):\n",
" n -= 1\n",
" print(\"while\",n)\n",
" print(\"for\",i)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"9) Imprimir los números primos existentes entre 0 y 30\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"el numero 2 es primo\n",
"el numero 3 es primo\n",
"el numero 5 es primo\n",
"el numero 7 es primo\n",
"el numero 11 es primo\n",
"el numero 13 es primo\n",
"el numero 17 es primo\n",
"el numero 19 es primo\n",
"el numero 23 es primo\n",
"el numero 29 es primo\n"
]
}
],
"source": [
"n=2\n",
"fin = 30\n",
"\n",
"while n <= fin:\n",
" primo = True\n",
" for i in range(2,n):\n",
" modulo = n%i\n",
" if modulo == 0:\n",
" primo= False\n",
" \n",
" if primo:\n",
" print(f\"el numero {n} es primo\")\n",
" n+=1\n",
" \n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"10) ¿Se puede mejorar el proceso del punto 9? Utilizar las sentencias break y/ó continue para tal fin\n",
"11) mirar qué tanto se optimizó"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n=2\n",
"fin = 30\n",
"ciclos_sin = 0 #contar el número de ciclos\n",
"while n <= fin:\n",
" primo = True\n",
" for i in range(2,n):\n",
" ciclos_sin += 1\n",
" modulo = n%i\n",
" if modulo == 0:\n",
" primo= False\n",
" break \n",
" if primo:\n",
" print(f\"el numero {n} es primo\")\n",
" n+=1\n",
"print(f\"Los ciclos sin break fueron: {ciclos_sin}\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"13) Aplicando continue, armar un ciclo while que solo imprima los valores divisibles por 12, dentro del rango de números de 100 a 300\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n=99\n",
"f=300\n",
"while (n<=300):\n",
" n+=1\n",
" if n%12 != 0:\n",
" continue\n",
" else:\n",
" print(n)\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"14) Utilizar la función **input()** que permite hacer ingresos por teclado, para encontrar números primos y dar la opción al usario de buscar el siguiente"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n=2\n",
"primo = True\n",
"buscar_primo = True\n",
"\n",
"while buscar_primo: \n",
" for i in range(2,n):\n",
" modulo = n%i\n",
" if modulo == 0:\n",
" primo= False\n",
" break\n",
" if primo:\n",
" print(f\"el numero {n} es primo\")\n",
" print(\"Desea encontrar el siguien? \")\n",
" continuar = input(\"ingrese si o no: \")\n",
" if continuar != 'si':\n",
" print(\"se finaliza la búsqueda\")\n",
" buscar_primo = False\n",
" break\n",
" else:\n",
" primo = True\n",
" n+=1"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"15) Crear un ciclo while que encuentre dentro del rango de 100 a 300 el primer número divisible por 3 y además múltiplo de 6"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"102\n"
]
}
],
"source": [
"n = 99\n",
"fin = 300\n",
"while(n<=fin):\n",
" n+=1\n",
" if n%3 == 0 and n%6 == 0:\n",
" print(n)\n",
" break\n",
" else:\n",
" continue"
]
}
],
"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.11.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading