python tutorial #6: Loop


SUBMITTED BY: Guest

DATE: Dec. 16, 2013, 8:39 a.m.

FORMAT: Python

SIZE: 1.2 kB

HITS: 744

  1. # loop
  2. # display n to 1
  3. n = 5
  4. # 'C'
  5. # while(n)
  6. # {
  7. # printf("%d\n", n);
  8. # --n;
  9. # }
  10. # Python
  11. # leader:
  12. # suite
  13. n = 5
  14. while n :
  15. print(n)
  16. n -= 1
  17. # infinte loop
  18. #n = 5
  19. #while n :
  20. # print(n)
  21. #n -= 1
  22. # ok
  23. n = 5
  24. while n :
  25. print(n)
  26. n -= 1
  27. # indentation by tab
  28. n = 5
  29. while n :
  30. print(n)
  31. n -= 1
  32. # comibination of tab and spaces : not reco; error in 3.x
  33. #n = 5
  34. #while n :
  35. # print(n)
  36. # n -= 1
  37. # reco: 4 spaces for indentation
  38. a = [
  39. ["dhoni", 80, 40, 50 ],
  40. ["kohli", 20, 100, 30 ],
  41. ["raina", 78, 52, 33 ],
  42. ["sehwag", 200, 0,9]
  43. ]
  44. # len : polymorphic
  45. print("\n","size : ", len(a))
  46. i = 0
  47. while i < len(a) :
  48. print(a[i][0])
  49. j = 1
  50. total = 0
  51. while j < len(a[i]) :
  52. total += a[i][j]
  53. j += 1
  54. i += 1
  55. print(total)
  56. #increase indention for suite wrt leader
  57. # can increase indentation following the leader
  58. # when decreasing indentation, it should match the
  59. # indentation of some earlier stmt

comments powered by Disqus