One Liner Fibonacci Sequence Python Code
In this tutorial, we are going to show How to write one liner fibonacci sequence python code. We have added the video tutorial and the source code of the program.
Video Tutorial: One Liner Fibonacci Sequence Python Code
What is Fibonacci Sequence?
The Fibonacci sequence is a sequence of numbers where each number is the sum of the previous two numbers. The first two numbers in the sequence are 0 and 1, and each subsequent number is the sum of the previous two numbers. So the Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, …
The Fibonacci sequence is named after Leonardo Fibonacci, who discovered it in the 13th century. Fibonacci was studying a problem in rabbit population growth, where each pair of rabbits produces a new pair of rabbits after a month. He noticed that the population of rabbits increased by a certain percentage each month and that the percentage was very close to the Fibonacci sequence.
Source Code
#One Liner Fibonacci Python Code fib = lambda x: x if x<=1 else fib(x-1)+fib(x-2) #Print first 10 fibonnaci numbers print([fib(i) for i in range(10)])
Explanation
To calculate the Fibonacci series, we can use the lambda function for a given number, n. The Fibonacci sequence for a number, n, is the sequence of numbers that are the sum of the previous two numbers in the sequence.