As you may have noticed, enciphering or deciphering a message means doing the same operation many times, once for each letter. You’d get tired of adding and subtracting by hand! When programming, it’s often impractical or impossible to type out every single operation. Instead, a programmer looks for the pattern in the repeated steps and describes that pattern once. One way to do that is with a loop, which iterates, or repeats, a block of code.
Python has two kinds of loops:
A while loop repeats as long as a condition is True. You don’t always know in advance how many times it will run, so this is called indefinite iteration.
A for loop runs once for each item in a sequence, such as each character of a string or each number in a range. You know before it starts how many times it will run, so this is called definite iteration.
Each kind has its uses. We’ll soon see how loops let one short piece of code encipher or decipher a message of any length.