0% found this document useful (0 votes)
32 views

P2 Programación 2

The document discusses Python programming concepts including conditional statements, code blocks, loops, functions, and parameter passing. It uses examples of a Fahrenheit to Celsius temperature conversion program to demonstrate improving the program through successive versions. The program is refactored to use functions to encapsulate logic, avoid duplicated code, and make the program more modular and readable. The document concludes with an exercise to implement min, max, and mean functions to analyze a list of values without modifying the list.

Uploaded by

delacoleccion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

P2 Programación 2

The document discusses Python programming concepts including conditional statements, code blocks, loops, functions, and parameter passing. It uses examples of a Fahrenheit to Celsius temperature conversion program to demonstrate improving the program through successive versions. The program is refactored to use functions to encapsulate logic, avoid duplicated code, and make the program more modular and readable. The document concludes with an exercise to implement min, max, and mean functions to analyze a list of values without modifying the list.

Uploaded by

delacoleccion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Curso de nivelación de

algoritmos
Taller - Clase 2
Python
Estructuras de control: condicionales
>>> x = 5
>>> if x < 0:
... print('negativo')
... elif x == 0:
... print('cero')
... else:
... print('positivo')
Python
Estructuras de control: condicionales
>>> x = 5
>>> if x < 0:
... print('negativo')
... elif x == 0:
... print('cero')
... else:
... print('positivo')
...
positivo
Python
Estructuras de control: bloques
>>> x = 5
>>> if x < 0 :
...[TAB]print('negativo')
... elif x == 0:
... print('cero')
... else:
... print('positivo')
...
positivo
Python
Estructuras de control: ciclos
>>> a, b = 0, 1
>>> while b < 10:
... print(b)
... a, b = b, a+b
Python
Estructuras de control: ciclos
>>> a, b = 0, 1
>>> while b < 10:
... print(b)
... a, b = b, a+b
...
1
1
2
3
5
8
Python
Nueva línea
>>> # Serie de fibonacci
... a, b = 0, 1
>>> while b < 10:
... print(b)
... a, b = b, a+b
...
1
1
2
3
5
8

>>> a, b = 0, 1
>>> while b < 10:
... print(b, end=' ')
... a, b = b, a+b
...
1 1 2 3 5 8
Conversor de Fahrenheit a Celsius
Clase pasada (f2c_v5.py)
# conversor fahrenheit a celsius
import sys

fahr = float(sys.argv[1])
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), " -> cel=", round(cel,2))
Conversor de Fahrenheit a Celsius
Lectura de argumentos (f2c_v5.py)
# conversor fahrenheit a celsius
import sys

fahr = float(sys.argv[1])
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), " -> cel=", round(cel,2))
Conversor de Fahrenheit a Celsius
Lectura de argumentos
# python f2c_v5.py 80
fahr=80.00 -> cel=26.67
Conversor de Fahrenheit a Celsius
Lectura de argumentos
# python f2c_v5.py 80
fahr=80.00 -> cel=26.67
Conversor de Fahrenheit a Celsius
Lectura de argumentos
# python f2c_v5.py 80
fahr=80.00 -> cel=26.67

# python f2c_v5.py
Traceback (most recent call last):
File "f2c_v5.py", line 4, in <module>
fahr = float(sys.argv[1])
IndexError: list index out of range
Conversor de Fahrenheit a Celsius
Lectura de argumentos
# python f2c_v5.py 80
fahr=80.00 -> cel=26.67

# python f2c_v5.py
Traceback (most recent call last):
File "f2c_v5.py", line 4, in <module>
fahr = float(sys.argv[1])
IndexError: list index out of range
Conversor de Fahrenheit a Celsius
¿Cuál es el problema?
# conversor fahrenheit a celsius
import sys

fahr = float(sys.argv[1])
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), " -> cel=", round(cel,2))
Conversor de Fahrenheit a Celsius
Sentencias condicionales (f2c_v6.py)
import sys

# conversor fahrenheit a celsius


if len(sys.argv) != 2:
print("uso: f2c valor\n")
else:
fahr = float(sys.argv[1])
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
Conversor de Fahrenheit a Celsius
Sentencias condicionales (f2c_v6.py)
import sys

# conversor fahrenheit a celsius


if len(sys.argv) != 2:
print("uso: f2c valor\n")
else:
fahr = float(sys.argv[1])
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
Conversor de Fahrenheit a Celsius
Bloques de código (f2c_v6.py)
import sys

# conversor fahrenheit a celsius


if len(sys.argv) != 2:
[TAB] print("uso: f2c valor\n")
else:
[TAB] fahr = float(sys.argv[1])
[TAB] cel = (5*(fahr-32))/9
[TAB] print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
Conversor de Fahrenheit a Celsius
Lectura de argumentos

# python f2c_v6.py 80
fahr=80.00 -> cel=26.67

# python f2c_v6.py
uso: f2c valor
Conversor de Fahrenheit a Celsius
Ejercicio

Generar una tabla de conversión de grados


Fahrenheit a Celsius partiendo de 0 hasta
100 a intervalos de 10.
Conversor de Fahrenheit a Celsius
Ciclos (f2c_v7.py)
# conversor fahrenheit a celsius

fahr = 0
while fahr < 101:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + 10
Conversor de Fahrenheit a Celsius
Ciclos (f2c_v7.py)
# conversor fahrenheit a celsius

fahr = 0
while fahr < 101:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + 10
Conversor de Fahrenheit a Celsius
Salida

# python f2c_v7.py
fahr= 0 -> cel=-17.78
fahr= 10 -> cel=-12.22
fahr= 20 -> cel= -6.67
fahr= 30 -> cel= -1.11
fahr= 40 -> cel= 4.44
fahr= 50 -> cel= 10.00
fahr= 60 -> cel= 15.56
fahr= 70 -> cel= 21.11
fahr= 80 -> cel= 26.67
fahr= 90 -> cel= 32.22
fahr= 100 -> cel= 37.78
Conversor de Fahrenheit a Celsius
Ejercicio

Generar una tabla de conversión de grados


Fahrenheit a Celsius partiendo de 0 hasta
100 a intervalos de 10 y a continuación la
conversión de todos los valores entre 101 y
110.
Conversor de Fahrenheit a Celsius
f2c_v8.py
# conversor fahrenheit a celsius

fahr = 0
while fahr < 101:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + 10

fahr = 101
while fahr < 111:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + 1
Conversor de Fahrenheit a Celsius
Código repetido (f2c_v8.py)
# conversor fahrenheit a celsius

fahr = 0
while fahr < 101:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + 10

fahr = 101
while fahr < 111:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + 1
Conversor de Fahrenheit a Celsius
Funciones (f2c_v9.py)
# conversor fahrenheit a celsius

def convertir(valorInicial, valorFinal, intervalo):


fahr = valorInicial
while fahr < valorFinal+1:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + intervalo

convertir(0, 100, 10)


convertir(101, 110, 1)
Conversor de Fahrenheit a Celsius
Funciones (f2c_v9.py)
# conversor fahrenheit a celsius

def convertir(valorInicial, valorFinal, intervalo):


fahr = valorInicial
while fahr < valorFinal+1:
cel = (5*(fahr-32))/9
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + intervalo

convertir(0, 100, 10)


convertir(101, 110, 1)
Conversor de Fahrenheit a Celsius
Funciones: encapsular y abstraer (f2c_v10.py)
# conversor fahrenheit a celsius

def fahrenheit2Celsius(valor):
return (5*(valor-32))/9

def convertir(valorInicial, valorFinal, intervalo):


fahr = valorInicial
while fahr < valorFinal+1:
cel = fahrenheit2Celsius(fahr)
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + intervalo

convertir(0, 100, 10)


convertir(101, 110, 1)
Conversor de Fahrenheit a Celsius
Funciones: valor de retorno (f2c_v10.py)
# conversor fahrenheit a celsius

def fahrenheit2Celsius(valor):
return (5*(valor-32))/9

def convertir(valorInicial, valorFinal, intervalo):


fahr = valorInicial
while (fahr < valorFinal+1):
cel = fahrenheit2Celsius(fahr)
print("fahr=", round(fahr,2), "-> cel=", round(cel,2))
fahr = fahr + intervalo

convertir(0, 100, 10)


convertir(101, 110, 1)
Ejercicio
Implementar las funciones getMin, getMax y computeMean
para que el siguiente programa pueda ejecutarse
correctamente.
Las funciones no deben modificar el contenido de la lista.

valores = [23,4,67,32,13]
print("El mínimo valor es: ", getMin(valores))
print("El máximo valor es: ", getMax(valores))
print("El promedio es: ", computeMean(valores))

You might also like