Q.Take a look at the series below:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55…
To form the pattern, start by writing 1 and 1. Add them together to get 2. Add the last two numbers: 1+2 = 3.Continue adding the previous two numbers to find the next number in the series. These numbers make up the famed Fibonacci sequence: previous two numbers are added to get the immediate new number.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Start with the pair 1, 1 and repeat the rule "new term = sum of the previous two": a function holding just two sliding variables prints any number of Fibonacci terms — 1 1 2 3 5 8 13 21 34 55 for ten terms.
The idea. The passage defines the series by a recurrence: every term is the sum of its two predecessors. To generate it you never need the whole list — only the last two values. Two variables plus Python's simultaneous assignment (a, b = b, a + b) implement the rule directly, wrapped in a user-defined function so the caller chooses how many terms.
def fibonacci(n):
# Print the first n terms of the Fibonacci sequence
a, b = 1, 1 # the two starting terms from the passage
for term in range(n):
print(a, end=" ")
a, b = b, a + b # previous two numbers -> the new number
print()
num = int(input("How many terms? "))
fibonacci(num)
Sample run (user asks for 10 terms)
How many terms? 10
1 1 2 3 5 8 13 21 34 55
This matches the series in the question exactly: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.
Dry run of the first six passes
| Pass | Printed a | New a (old b) | New b (old a + old b) |
|---|---|---|---|
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 3 |
| 3 | 2 | 3 | 5 |
| 4 | 3 | 5 | 8 |
| 5 | 5 | 8 | 13 |
| 6 | 8 | 13 | 21 |
Why the simultaneous assignment matters
a, b = b, a + bevaluates the whole right side first, then assigns — both updates use the old values. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.