Get Website Learn Coding Tech Story

Python Programming Examples

#!/usr/bin/evn python

# Define function as 'F' and pass parameter as 'n'
def F(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        # Call function 'F' two times recursively by passing parameter 'n-1' and 'n-2' respectively and calculate sum
        return F(n-1)+F(n-2) 

# Take input from the user
num = int(input("Enter a number to generate Fibonacci sequence: "))
print("Fibonacci sequence:")
# For loop iterating by sequence index
for i in range(num):
    print(F(i));


#***** Output *****
Enter a number to generate Fibonacci sequence: 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

Learn more about Fibonacci series