By the end of this module you can do everything below. The objectives and their wording come from the official PCEP-30-03 syllabus.
2.2.1 Create condition-controlled loops using while
- Use a
whileloop to repeat instructions while a condition remains true. - Initialize and update loop-control variables (sentinel values).
- Trace the execution of a condition-controlled loop.
- Recognize conditions that may result in an infinite loop.
- Use assignment expressions with the walrus operator
:=where appropriate.
2.2.2 Iterate through sequences and iterable objects using for and in
- Use a
forloop to iterate through a sequence or iterable object. - Use the
inoperator to retrieve successive elements from an iterable. - Iterate through common iterable objects such as strings, lists, and tuples.
- Process each element of an iterable within a loop.
- Trace the values assigned to the loop variable during execution.
2.2.3 Generate numeric iterations using range()
- Use
range()to generate integer sequences for loop iteration. - Use the start, stop, and step arguments of
range(). - Use positive and negative step values.
- Trace the values produced by
range()during loop execution.
2.2.4 Construct nested loops and combined control structures
- Nest one loop inside another loop.
- Combine loops with conditional statements.
- Trace the execution order of nested loops.
- Use nested loops to process multidimensional or grouped data.
2.2.5 Control loop execution using break, continue, and pass
- Use
breakto terminate a loop before its normal completion. - Use
continueto skip the remainder of the current iteration. - Use
passas a syntactic placeholder for an empty block. - Trace loops that contain
break,continue, orpass.