LearnFrenzy
Sign in Take a free test

Loops · Loops Coding Problems · Question 1

Fibonacci series in Python Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

Print the first 12 terms of the Fibonacci series, starting 0, 1.

Solution
main.py Run
n = 12
a, b = 0, 1
terms = []
for _ in range(n):          # _ : the loop counter is not needed
    terms.append(a)
    a, b = b, a + b
print(*terms)

Output Verified by running it on Python 3

0 1 1 2 3 5 8 13 21 34 55 89

The simultaneous assignment a, b = b, a + b moves the pair one step along without a temporary variable.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links