Wednesday, February 4th, 2026¶
On Monday, we started working with lists and for loops.
Constructing lists¶
So far, we've explicitly generated lists using square brackets and comma-separated inputs (which we've had to manually type in). Suppose we want to generate a list containing the cubes of the first 50 positive integers. Our current strategy is not reasonable for this sort of task.
The .append method (attached to a list) can be used to add an element to a list. That is, we can write something like <some list>.append(<some new element>) to add <some new element> to <some list>.
my_list = ['a', 'b', 'c', 'd', 'e']
print(my_list)
['a', 'b', 'c', 'd', 'e']
my_list.append('x')
print(my_list)
['a', 'b', 'c', 'd', 'e', 'x']
my_list.append('g')
print(my_list)
['a', 'b', 'c', 'd', 'e', 'x', 'g']
To build a list of the cubes of the first 50 positive integers, we can start with an empty list [] and then iteratively use the .append method to add elements to that list.
cubes = []
for i in range(1,51):
cube = i**3
cubes.append(cube)
print(cubes[-1])
1 8 27 64 125 216 343 512 729 1000 1331 1728 2197 2744 3375 4096 4913 5832 6859 8000 9261 10648 12167 13824 15625 17576 19683 21952 24389 27000 29791 32768 35937 39304 42875 46656 50653 54872 59319 64000 68921 74088 79507 85184 91125 97336 103823 110592 117649 125000
for cube in cubes:
print(cube)
1 8 27 64 125 216 343 512 729 1000 1331 1728 2197 2744 3375 4096 4913 5832 6859 8000 9261 10648 12167 13824 15625 17576 19683 21952 24389 27000 29791 32768 35937 39304 42875 46656 50653 54872 59319 64000 68921 74088 79507 85184 91125 97336 103823 110592 117649 125000
print(cubes)
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261, 10648, 12167, 13824, 15625, 17576, 19683, 21952, 24389, 27000, 29791, 32768, 35937, 39304, 42875, 46656, 50653, 54872, 59319, 64000, 68921, 74088, 79507, 85184, 91125, 97336, 103823, 110592, 117649, 125000]
Exercise: Generate a list of the squares of the first $40$ positive integers. Then print the remainder of each after division by $7$.
squares = []
for i in range(1,41):
squares.append(i**2)
for square in squares:
print(square % 7)
1 4 2 2 4 1 0 1 4 2 2 4 1 0 1 4 2 2 4 1 0 1 4 2 2 4 1 0 1 4 2 2 4 1 0 1 4 2 2 4
Boolean expressions¶
There are two Boolean values, namely True and False.
We can write statements that evalute to either True or False called Boolean expressions. For example, we can compare two numbers using < or > to see if one is less than the other or one is greater than the other.
7 > 3
True
3 > 7
False
10 < 10.1
True
Similarly, we can use <= or >= for less than/greater than or equal to.
10 <= 10
True
9 <= 9.1
True
Inequality checks can also be chained together.
2 < 3 < 5
True
-1 <= -1 < -1
False
We can use a double equality == to check whether two objects are equal to one another.
5 == 5
True
3.0 == 3
True
We can also check whether two lists are equal to one another (i.e. if they contain equal objects in the same order).
my_list1 = [1,2,3,4,5]
my_list2 = [1,2,3,4,5]
my_list1 == my_list2
True
my_list1 = [1,2,3,4,5]
my_list2 = [1,2,3,4,6]
my_list1 == my_list2
False
We can construct more complicated Boolean expressions using the and, or, and not operators. That is:
(some expression) and (some other expression)will evaluate asTrueif(some expression)and(some other expression)are bothTrue.(some expression) or (some other expression)will evaluate asTrueif either(some expression)or(some other expression)areTrue(or both).not (some expression)will evaluate asTrueif(some expression)isFalse.
Note: A number $n$ is even if it has remainder $0$ after division by $2$.
A number $n$ is a multiple of $3$ if it has remainder $0$ after division by $3$.
Can we check if a given number n is both even and a multiple of $3$?
for n in range(1,21):
print(n, (n % 2 == 0) and (n % 3 == 0))
1 False 2 False 3 False 4 False 5 False 6 True 7 False 8 False 9 False 10 False 11 False 12 True 13 False 14 False 15 False 16 False 17 False 18 True 19 False 20 False
(5 < 6) or (6 < 5)
True
not (True)
False
Using if statements¶
We can use an if statement to perform some operations only when a Boolean expression is True. The syntax for writing an if statement is:
if (some Boolean expression):
(do something)
Again, spacing is CRITICAL, as it indicates which operations are part of the if statement (which will only run when the Boolean expression is True), and which operations are outside of the if statement (which will run regardless).
n = 12
if n % 2 == 0:
print('{} is even.'.format(n))
if n % 2 == 1:
print('{} is odd.'.format(n))
print('Done!')
12 is even. Done!
for n in range(1,21):
if n % 2 == 0:
print('{} is even.'.format(n))
if n % 2 == 1:
print('{} is odd.'.format(n))
print('Done!')
1 is odd. 2 is even. 3 is odd. 4 is even. 5 is odd. 6 is even. 7 is odd. 8 is even. 9 is odd. 10 is even. 11 is odd. 12 is even. 13 is odd. 14 is even. 15 is odd. 16 is even. 17 is odd. 18 is even. 19 is odd. 20 is even. Done!
evens = []
odds = []
for n in range(1,21):
if n % 2 == 0:
evens.append(n)
if n % 2 == 1:
odds.append(n)
print('Done!')
Done!
evens
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
odds
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Optionally, we can include an else block immediately following an if block. In this case, the code inside the else block will only occur if the Boolean expression in the if statement was False.
n = 11
if n % 2 == 0:
print('{} is even.'.format(n))
else:
print('{} is not even.'.format(n))
11 is not even.
Exercise: Use an if/else pair to print a string stating whether an integer n is even or odd.
We very often want to perform different operations based on several Boolean expressions. We can supplement an if statement with an elif statement (which is short for "else if") with a new Boolean expression to perform operations only in the case that the first if expression was False and the new expression is True.
n = 13
if n % 3 == 0:
print('{} is a multiple of 3.'.format(n))
elif n % 3 == 1:
print('{} is one more than a multiple of 3.'.format(n))
elif n % 3 == 2:
print('{} is two more than a multiple of 3.'.format(n))
else:
print('{} is not zero, one, or two more than a multiple of 3.'.format(n))
13 is one more than a multiple of 3.
We can include many elif blocks to check different cases.
Exercise: Use an if/elif/else triple to print a string stating whether an integer n is a multiple of $3$, one more than a multiple of $3$, or two more than a multiple of $3$.
In-class exercise¶
Exercise: Out of the squares of the first $40$ positive integers, count how many have remainder $1$ after division by $7$.