Summary
In this lab, we introduced several major important topics in Python. Let’s quickly review them.
Python While Loops
While loops in Python will execute while a Boolean expression evaluates to true
.
while <boolean expression>:
<block of statements>
Range Function
The range()
function in Python is used to generate a list of numbers. It can be used in three ways:
range(stop)
- numbers from $0$ up to (but not including)stop
range(start, stop)
- numbers fromstart
up to (but not including)stop
range(start, stop, step)
- numbers fromstart
up to (but not including)stop
, withstep
between each number.
Python For Loops
For loops in Python will execute a set number of times.
for <iterator variable> in <list>:
<block of statements>
Input with Loops
Loops can be used to request new input from the user if invalid input is received.
def input_percentage():
x = float(input("Enter a percentage as a decimal number from 0 to 1: "))
while(x < 0 or x > 1):
print("Invalid Input!")
x = float(input("Enter a percentage as a decimal number from 0 to 1: "))
return x
Testing Loops
Loops can be tested for both branch and path coverage. In general, achieving path coverage involves writing code that will enter the loop, and also code that will bypass the loop entirely.
Loops should also be tested for termination and situations that may result in infinite loops. Using a loop variant and showing that it is monotonically decreasing is a helpful technique.