Monday, February 9th, 2026¶
Last week, we worked on using using if/elif/else statements within for loops to construct lists.
Exercise: (from last class) Out of the squares of the first $40$ positive integers, count how many have remainder $1$ after division by $7$.
squares = []
for i in range(1,41):
squares.append(i**2)
squares_with_remainder = []
for square in squares:
if (square % 7 == 1):
squares_with_remainder.append(square)
print('{} of the first 40 squares have remainder 1 after division by 7.'.format(len(squares_with_remainder)))
11 of the first 40 squares have remainder 1 after division by 7.
Some thoughts:
It would be nice if we defined some parameters to control the above cells. That is, it would be nice if we could easily change, say, the divisor that we are using.
div = 9
squares_with_remainder = []
for square in squares:
if (square % div == 1):
squares_with_remainder.append(square)
str_template = '{} of the first 40 squares have remainder 1 after division by {}.'
print(str_template.format(len(squares_with_remainder),div))
9 of the first 40 squares have remainder 1 after division by 9.
Exercise: Count how many of the first $1000$ cubes have remainder $0$ after division by $4$. Use variables (your choice of variable names) to define the parameters $1000$, $0$, and $4$. Your code should print a statement like "W of the first X cubes have remainder Y after division by Z."
div = 4
N = 1000
rem = 0
cubes = []
for i in range(1,N+1):
cubes.append(i**3)
cubes_with_remainder = []
for cube in cubes:
if (cube % div == rem):
cubes_with_remainder.append(cube)
str_template = '{} of the first {} cubes have remainder {} after division by {}.'
print(str_template.format(len(cubes_with_remainder),N,rem,div))
500 of the first 1000 cubes have remainder 0 after division by 4.
Exercise: Use the code above inside a for loop to count how many of the first $1000$ cubes have remainder $0$, then remainder $1$, then remainder $2$, then remainder $3$ after division by $4$.
div = 4
N = 1000
cubes = []
for i in range(1,N+1):
cubes.append(i**3)
str_template = '{} of the first {} cubes have remainder {} after division by {}.'
for rem in range(div):
cubes_with_remainder = []
for cube in cubes:
if (cube % div == rem):
cubes_with_remainder.append(cube)
print(str_template.format(len(cubes_with_remainder),N,rem,div))
500 of the first 1000 cubes have remainder 0 after division by 4. 250 of the first 1000 cubes have remainder 1 after division by 4. 0 of the first 1000 cubes have remainder 2 after division by 4. 250 of the first 1000 cubes have remainder 3 after division by 4.
Prime numbers¶
Our first project will deal with prime numbers. It will be useful if we can develop some code to decide whether a given integer is prime or not. One strategy for checking whether a given number is prime or not is to look for numbers that evenly divide it. That is, we can test a number $n$ for primality by looking for numbers $d$ such that the remainder from dividing $n$ by $d$ is zero.
n = 123
for d in range(2, n):
if n % d == 0:
print('{} is not prime. It has a divisor {}.'.format(n,d))
123 is not prime. It has a divisor 3. 123 is not prime. It has a divisor 41.
Exercise: Write code that will set a Boolean variable to True if an integer n is prime and will set the Boolean variable to False if not.
n = 123
n_is_prime = True
for d in range(2, n):
if n % d == 0:
n_is_prime = False
if n_is_prime:
print('{} is prime.'.format(n))
else:
print('{} is not prime.'.format(n))
123 is not prime.
for n in range(2,31):
n_is_prime = True
for d in range(2, n):
if n % d == 0:
n_is_prime = False
if n_is_prime:
print('{} is prime.'.format(n))
else:
print('{} is not prime.'.format(n))
2 is prime. 3 is prime. 4 is not prime. 5 is prime. 6 is not prime. 7 is prime. 8 is not prime. 9 is not prime. 10 is not prime. 11 is prime. 12 is not prime. 13 is prime. 14 is not prime. 15 is not prime. 16 is not prime. 17 is prime. 18 is not prime. 19 is prime. 20 is not prime. 21 is not prime. 22 is not prime. 23 is prime. 24 is not prime. 25 is not prime. 26 is not prime. 27 is not prime. 28 is not prime. 29 is prime. 30 is not prime.
The break and continue commands¶
The break command can be used within a loop to immediately exit the loop.
for i in range(10):
if i == 6:
break
print(i)
print('Done!')
0 1 2 3 4 5 Done!
Exercise: Modify the prime-checking code above to exit the loop as soon as n is determined to not be prime.
n = 123
for d in range(2, n):
if n % d == 0:
print('{} is not prime. It has a divisor {}.'.format(n,d))
123 is not prime. It has a divisor 3. 123 is not prime. It has a divisor 41.
n = 123
for d in range(2, n):
if n % d == 0:
print('{} is not prime. It has a divisor {}.'.format(n,d))
break
123 is not prime. It has a divisor 3.
n = 123
n_is_prime = True
for d in range(2, n):
if n % d == 0:
n_is_prime = False
break
if n_is_prime:
print('{} is prime.'.format(n))
else:
print('{} is not prime.'.format(n))
123 is not prime.
Other times, we may want to immediately proceed to the next iteration of a loop. The continue command can be used to accomplish this.
for i in range(10):
if 3 < i < 7:
continue
print(i)
print('Done!')
0 1 2 3 7 8 9 Done!
while loops¶
Suppose that we want to find the first $100$ cubes that have remainder $1$ after division by $4$.
Problem: We don't know ahead of time how many numbers we'd have to check before we find $100$ such cubes. In this case, a for loop isn't suitable.
For these types of problems, we can use a while loop. A while loops will iteratiavely perform some operations as long as some Boolean expression is True. The syntax is as follows:
while (some Boolean expression is True):
(do something)
Note: the (some Boolean expression) can be (and often is) a variable that will be modified within the while loop.
i = 0
while i < 10:
print(i)
i = i + 1
0 1 2 3 4 5 6 7 8 9
We can also use break to terminate a while loop:
i = 0
while True:
if i >= 10:
break
print(i)
i = i + 1
0 1 2 3 4 5 6 7 8 9
Warnings:
- Unlike with
forloops, it is very easy to end up with awhileloop that runs forever. - Always make sure that your Boolean expression will eventually be
Falseso that thewhileloop can terminate. - Check that you are incrementing any necessary variables with each iteration.
If you find your code stuck running a while loop, you can hit the stop button (black square in the toolbar near the top of the notebook) to try to get Python to interrupt the kernel. If this does not work, you can restart the kernel (refresh symbol to the right of the stop button) to shut down the notebook and restart.
There are some shortcuts for incrementing variables. In particular:
n += 1is equivalent ton = n + 1n -= 3is equivalent ton = n - 3n *= 7is equivalent ton = n * 7
i = 0
while True:
i = i + 1
i = 2
while i < 1000:
i **= 2
print(i)
4 16 256 65536
Exercise: Write code that will find the first $100$ cubes that have remainder $1$ after division by $4$.
Exercise: Write code that will find the first $10$ prime numbers.