Learn about Fibonacci Series in Python using For loop in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Fibonacci Series in Python using For loop
# Python program to print fibonacci series up to n-th term
# input from user
num = int(input('Enter number of terms: '))
# display fibonacci series
a, b = 0, 1
# check if the number of terms is valid
if num <= 0:
print('Please enter a positive integer.')
elif num == 1:
print('The Fibonacci series: ')
print(a)
else:
print('The Fibonacci series: ')
for i in range (1, num+1):
print(a, end=' ')
c = a + b
a = b
b = c
Output:
Enter number of terms: 7 The Fibonacci series: 0 1 1 2 3 5 8
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Fibonacci Series in Python