Advertisement
teslariu

for

Sep 24th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. lista = [23, 23.4, True, "Hola"]
  5. notas = [10,5,8,9,7]
  6. palabra = "mariposa"
  7. numeros = [1,2,3,4,5,6,7,8,9,10]
  8.  
  9. """
  10. i = 0
  11.  
  12. while i < len(lista):
  13.     print(lista[i])
  14.     i = i + 1
  15.  
  16. # fuentes de error del while:
  17. # inicializar mal el contador (i=0)
  18. # no incrementar el contador -> bucle infinito (no poner i = i + 1)
  19. # poner mal el tope (por ejemplo, i + len(lista) en lugar de len(lista)
  20.  
  21. # POR TODO LO DE ARRIBA; LO MEJOR ES ITERAR CON UN FOR
  22. # el for controla todo lo de arriba automáticamente
  23. # El iterador es una variable con contenido semántico
  24. # TODA COLECCION ES ITERABLE (lista, string)
  25.  
  26. # EL FOR NO TIENE POSIBILIDAD DE ERROR COMO EL WHILE PORQUE
  27. # EL INTERPRETE DE PYTHON HACE TODO EL SOLO
  28.  
  29.  
  30. for elemento in lista:
  31.     print(elemento)
  32.  
  33. for nota in notas:  # for <iterador> in <iterable>:
  34.     print(nota)
  35.    
  36. for letra in palabra:
  37.     print(letra,end=".")
  38. """
  39.    
  40. for numero in range(2,101,2):
  41.     # imprime todos los pares de 2 a 100 con sus cuadrados
  42.     print(f"x={numero}   cuadrado={numero**2}")
  43.        
  44. # range(inicio, tope, salto)
  45. # range(inicio, tope)  salto por defecto igual a 1
  46. # range(tope) inicio x defecto = 0, salto x defecto = 1
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement