Previous Lesson Current Lesson Next Lesson

# Question 1

# Compute the first "b" multiples of "a"
a = 5
b = 10

# Store the multiples.
output = []

# Iterate from 1 to b (inclusive) and append products.
for i in range(1, b + 1):
output.append(a * i)

print(output)


Your comments do not have to look exactly like this, but here, we make it very clear what’s happening. We are generating the first "b" (10) multiples of "a" (5) and storing the multiples in a list, output. The output of this code is then:

[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]


Recall that range iterates from start to end - 1 (when both are provided), so we are really iterating over [1, 2,..., b], where start = 1 and end = b + 1.

# Question 2

# Starting values of Fibonacci Sequence.
a = 1
b = 1

# Compute the nth Fibonacci number.
n = 5
i = 1

while i <= n:
tmp = a

# The next Fibonacci number ("b") is the sum of the previous two.
# We store the current Fibonacci number at "a".
b = a + b
a = tmp

print(a)


This will output:

5


Of course, it was not required that you recognize it was Fibonacci. You could have also explained that we were generating a sequence of numbers such that the next number was the sum of the previous two, where the initial “previous two” numbers are 1 and 1.