Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: 3. Loops & Algorithms

Hello loops

#
# hello_loops.py
#


# for loop

things = [1.23, 666, "juggling balls"]

print("things:")
for thing in things:
    print(thing)


# while loop

print()
print("while loop")

value = 0

while value < 5:
    print(value)
    value += 1


# continue and break


print()
print("continue and break")

value = 0

while True:             # loop forever
    value += 1
    if value%2 == 0:    # even: do nothing
        continue
    print(value)
    if value > 5:       # break out of loop
        break

Output:

things:
1.23
666
juggling balls

while loop
0
1
2
3
4

continue and break
1
3
5
7

Next: