Advertisement
JPablos

Raíz cuadrada. Python

Apr 4th, 2021
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. Se muestran aqui dos formas de aplicar formato
  5.  
  6.    (a)    < %[(keyname)][flags][width][.precision]typecode >
  7.  
  8.            '.2f' corresponde a [.precision]
  9.  
  10.    (b)    < {fieldname component !conversionflag :formatspec} >
  11.  
  12.            con formatspec
  13.  
  14.            [[fill]align][sign][#][0][width][,][.precision][typecode]
  15.  
  16.            ':.2f' corresponde a [.precision]
  17.    
  18. """
  19.  
  20. def raicesCuadradas(N):
  21.     """
  22.    Función simple que muestra un uso de < yield >
  23.    
  24.    Se omite el 0 por razones obvias
  25.    
  26.            'f': Floating-point decimal
  27.            
  28.        ':.2f' ajustado a solo 2 decimales
  29.  
  30.    help(raicesCuadradas) --> muestra esta información
  31.    """
  32.     for i in range(N):
  33.         if i != 0:
  34.             yield f"{i} {chr(8594)} {(i ** 0.5):.2f}"
  35.  
  36. msg = "N rango, cantidad a generar"
  37. print(msg)
  38.  
  39. N = int(input("N = "))
  40.  
  41. F = raicesCuadradas(N)
  42.  
  43. G = (x ** 0.5 for x in range(N) if x !=0)
  44.  
  45. Gf = ('%.2f' %(x ** 0.5) for x in range(N) if x !=0 )
  46.  
  47. print("Raices cuadradas, función con formato:")
  48. print(list(F), '\n')
  49.  
  50. print("Raices cuadradas, 'expresión generadora' sin formato:")
  51. print(list(G), '\n')
  52.  
  53. print("Raices cuadradas, 'expresión generadora' con formato:")
  54. print(list(Gf), '\n')
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement