Learn about Fibonacci Series in Python in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Fibonacci Series in Python
# Python program to print fibonacci series up to n-th term # input from user num = int(input('Enter number of terms: ')) # displaying fibonacci series a, b = 0, 1 i = 0 # 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: ') while i < num: print(a, end=' ') c = a + b a = b b = c i = i+1
Enter number of terms: 5 The Fibonacci series: 0 1 1 2 3
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Simple Calculator in Python