Python for Loop Code Segment

From GM-RKB
Jump to navigation Jump to search

A Python for Loop Code Segment is a loop code segment that is a Python loop code segment (that uses a Python for statement).



References

2015

  • http://www.python-course.eu/python3_for_loop.php
    • QUOTE:
      • Iterator-based for loop: Finally, we come to the one used by Python. This kind of a for loop iterates over an enumeration of a set of items. It is usually characterized by the use of an implicit or explicit iterator. In each iteration step a loop variable is set to a value in a sequence or other data collection. This kind of for loop is known in most Unix and Linux shells and it is the one which is implemented in Python.
edibles = ["ham", "spam", "eggs","nuts"]
for food in edibles:
   if food == "spam":
       print("No more spam please!")
       continue
   print("Great, delicious " + food)
   # here can be the code for enjoying our food :-)
else:
   print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")


n = 100
sum = 0
for counter in range(1,n+1):
   sum = sum + counter
print("Sum of 1 until %d: %d" % (n,sum))