Working with While Loops
Welcome to the Working with While Loops Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Already have an account? Sign In »

Introduction
Welcome to the Working with While Loops Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Learning Outcomes
In this module, you will complete the following exercise:
- Exercise 1 - Finding Armstrong Numbers in Specified Intervals
After completing this lab, you will be able to:
- Use for loops, while loops, and if…else statements in Python programs
Exam Objectives
The following exam objectives are covered in this lab:
- 2.2 Construct and analyze code segments that perform iteration
Lab Duration
It will take approximately 20 minutes to complete this lab.
Exercise 1 - Finding Armstrong Numbers in Specified Intervals
Sometimes, to keep a computer doing useful work, you may need to run the same block of code repeatedly. You can do so by putting that block of code in a loop. Think of the playlist that you can put in a loop to make the same set of songs play again and again!
Two types of loops are possible in Python:
- for loop: This loop is used to run a piece of code "n" number of times, where “n” can be specified either by the programmer or accepted as an input from the user.
- while loop: This loop runs a set of statements “while” a specific condition is true.
The difference is that the while loop checks the condition and performs iteration ONLY IF the condition is met, else it skips the block of code and moves to the next statement. So you can say that the while loop has implicit decision-making involved (depending on the specified condition), but a for loop will run the program the specified number of times, irrespective of anything else.
However, with for loop too, you can include some decision-making by using the if…else statement.
To do that, you can include a test expression within the if construct. Once you do that, the program evaluates the test expression and will execute statement(s) (mentioned in the body of if) only if the text expression is True. If the text expression is False, body of else is executed.
The indentation is used to separate the if and else blocks:
if test expression:
Body of if
else:
Body of else
The body of each block starts with an indentation, and the first un-indented line marks the end. Python interprets non-zero values as True. None and zero are interpreted as False.
In this exercise, you will learn to write a program to find Armstrong numbers within a specified interval using for loops, while loops, and if…else condition statements.
Learning Outcomes
After completing this exercise, you will be able to:
- Use for loops, while loops, and if…else statements in Python programs
See the full benefits of our immersive learning experience with interactive courses and guided career paths.