Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to find squares of the summation of n numbers.
Here is an example of a Python program that prints the Fibonacci series of a given number:
Code For Python Program To Find The Fibonacci Series
def fibonacci_series(n): fib = [0, 1] for i in range(2, n): fib.append(fib[i-1] + fib[i-2]) return fib n = int(input("Enter the number of elements: ")) print("Fibonacci series of", n, "elements is", fibonacci_series(n))
Enter the number of elements: 4
Fibonacci series of 4 elements is [0, 1, 1, 2]
In this program, the
fibonacci_series(n) function takes one argument: a number n. The function uses a
for loop to generate the Fibonacci series of n numbers, which is stored in a list called
The function uses two initial values 0 and 1 to generate the fibonacci series. And in each iteration, it sums the previous two numbers in the list and append it to the list. Finally, the function returns the list of Fibonacci series of n numbers.
The program starts by asking the user to enter the number of elements. This value is stored in the variable
n. The function fibonacci_series is called with n as an argument. The returned value of the function is printed with message “Fibonacci series of n elements is “.
As before, it’s always a good practice to validate the input, in this case, make sure the entered number of elements is not negative or zero.
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.