Iteration

  • repeating portion of an algorithm, repeats a specified number of times or until a given condition is met
import random

i = 10

while (i < 100):
     print('you are less than 100 years old')
     i = i + 30
     if (i > 100):
        break
nums = [44, 55, 33, 66, 77, 88, 99, 100]

for n in nums:
    if n % 2 == 0:
        print('This did not affect you at all')

This did not affect you at all
This did not affect you at all
This did not affect you at all
This did not affect you at all

Iteration Statement

  • Iteration statements cause statements to be executed zero or more times, subject to some loop-termination criteria
for i in range(100,105):
    i = i + 98
    print(i)
else:
    print('this is how you count from 198 to 202')

you are less than 100 years old
you are less than 100 years old
you are less than 100 years old
This did not affect you at all
This did not affect you at all
This did not affect you at all
This did not affect you at all
198
199
200
201
202
this is how you count from 198 to 202

a = [3,16,29,42,55,68,81]
c = 2
while (c) > 1:
    c +=1
    print(a);
    c == 3
    break

[3, 16, 29, 42, 55, 68, 81]

nums = ["30", "40", "50", "15", "20", "433"]
minimum = min(nums)
print(minimum, "This is the lowest number")

# Part 2

low = (nums[0])
for i in range(len(nums)):
    if (nums[i]) < low:
        low = nums[i]
print("the lowest number in nums is:", low)

15 This is the lowest number
the lowest number in nums is: 15