condicionales (2017)

Apunte Español
Universidad Universidad Complutense de Madrid (UCM)
Grado Matemáticas y Estadística - 1º curso
Asignatura informatica
Año del apunte 2017
Páginas 3
Fecha de subida 08/07/2017
Descargas 0
Subido por

Descripción

apuntes de condicionales en python

Vista previa del texto

Condicionales Informática 2015-2016 October 30, 2015 1 Un condicional simple def abs(x): """ In [9]: Returns the absolute value of x Parameters ---------x : int A number Returns ------int the absolute value of x """ if x < 0: x = -x return x abs(10) In [10]: 10 Out [10]: abs(-10) In [11]: 10 Out [11]: 2 Consideramos la negación In [12]: def abs(x): """ Returns the absolute value of x Parameters ---------x : int A number Returns ------int the absolute value of x """ if x < 0: return -x else: return x abs(-6), abs(7) In [19]: (6, 7) Out [19]: 3 Podemos anidar condicionales def degree(a, b, c): """ In [13]: Returns the degree of the polynomial a*x^2+b*x+c Parameters ---------a,b,c : float Coefficients of polynomial Returns ------int Degree of the polynomial """ if a == 0: if b == 0: result = 0 else: result = 1 else: result = 2 return result degree(2, 3, 4), degree(0, 1, 2), degree(1, 0, 0),degree(0, 0, 1) In [14]: (2, 1, 2, 0) Out [14]: 4 Elecciones múltiples In [15]: def grade(mark): """ Converts the numerical grade to the Spanish word Parameters ---------mark : float the mark to be translated into a word Returns ------str the Spanish word for the numerical mark Precondition -----------mark >= 0 and mark <= 10 """ if mark < 5: return "Suspenso" elif mark < 6: return "Aprobado" elif mark < 9: return "Notable" else: return "Sobresaliente" grade(10), grade(1.23), grade(3.45), grade(4.999), grade(7), grade(5) In [16]: (’Sobresaliente’, ’Suspenso’, ’Suspenso’, ’Suspenso’, ’Notable’, Out [16]: ’Aprobado’) 5 Otro ejemplo más complicado In [20]: def solve_linear_system(a, b, c, d, e, f): """ Solves the linear system of equations: a*x + b*y = e c*x + d*y = f Parameters ---------a,b,c,d,e,f : float Coefficients of the ecuations Returns ------(float,float) Pair of values (x,y) that solve the ecuations Precondition -----------a*d - b*c != 0 """ if a == 0: y = e / b x = (f - d*y) / c else: y = (a*f - c*e)/(-c*b + a*d) x = (e - b*y) / a return x, y solve_linear_system(1.0,1.0,0.0,1.0,2.0,1.0) In [21]: (1.0, 1.0) Out [21]: In [18]: ...

Tags:
Comprar Previsualizar