Using loops in Python#

Programs are a sequence of instructions, but in some cases those sequences need to be repeated. For example to read all lines from a file, or do a calculation a certain number of times. Python supports both ways to execute a sequence of instructions until a criteria is met or for all elements in sequence.

While and For loops#

A while loop is a way to repeat a sequence of instructions until a certain condition is met. The example below will print i as long as it is smaller than 6. The condition is checked at the beginning of the loop and we need to manually increment i.

Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    i = 0
 5    while i < 6:
 6        print(i)
 7        i += 1
 8
 9
10if __name__ == "__main__":
11    main()

A second way is to use a for loop, but here we iterate over a sequence of values instead of evaluating a condition before the next loop. The example below will print the values of the sequence [0, 1, 2, 3, 4, 5] and will generate the same output as the previous example.

Example#
1#!/usr/bin/env python3
2
3def main():
4    for i in range(6):
5        print(i)
6
7
8if __name__ == "__main__":
9    main()

With

Old#

Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    i = 1
 5    while i < 6:
 6        print(i)
 7        if i == 3:
 8            break
 9        i += 1
10
11
12if __name__ == "__main__":
13    main()
Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    i = 0
 5    while i < 6:
 6        i += 1
 7        if i == 3:
 8            continue
 9        print(i)
10
11
12if __name__ == "__main__":
13    main()
Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    i = 1
 5    while i < 6:
 6        print(i)
 7        i += 1
 8    else:
 9        print("i is no longer less than 6")
10
11
12if __name__ == "__main__":
13    main()

For loop#

Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    fruits = ["apple", "banana", "cherry"]
 5    for x in fruits:
 6        print(x)
 7
 8
 9if __name__ == "__main__":
10    main()
Example#
1#!/usr/bin/env python3
2
3def main():
4    for x in "banana":
5        print(x)
6
7
8if __name__ == "__main__":
9    main()
Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    fruits = ["apple", "banana", "cherry"]
 5    for x in fruits:
 6        if x == "banana":
 7            break
 8        print(x)
 9
10
11if __name__ == "__main__":
12    main()
Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    fruits = ["apple", "banana", "cherry"]
 5    for x in fruits:
 6        if x == "banana":
 7            continue
 8        print(x)
 9
10
11if __name__ == "__main__":
12    main()
Example#
1#!/usr/bin/env python3
2
3def main():
4    for x in range(6):
5        print(x)
6
7
8if __name__ == "__main__":
9    main()
Example#
1#!/usr/bin/env python3
2
3def main():
4    for x in range(2, 6):
5        print(x)
6
7
8if __name__ == "__main__":
9    main()
Example#
1#!/usr/bin/env python3
2
3def main():
4    for x in range(2, 30, 3):
5        print(x)
6
7
8if __name__ == "__main__":
9    main()
Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    for x in range(6):
 5        print(x)
 6    else:
 7        print("Finally finished!")
 8
 9
10if __name__ == "__main__":
11    main()
Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    for x in range(6):
 5        if x == 3:
 6            break
 7        print(x)
 8    else:
 9        print("Finally finished!")
10
11
12if __name__ == "__main__":
13    main()
Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    adj = ["red", "big", "tasty"]
 5    fruits = ["apple", "banana", "cherry"]
 6
 7    for x in adj:
 8        for y in fruits:
 9            print(x, y)
10
11
12if __name__ == "__main__":
13    main()