So, as a beginner in Python, I have been trying to solve practice problems in loops and if-else statements to get a good grip on the basics of program flow and control statements. I was working on this problem where I have to print the Fibonacci series up to ten elements like this:
Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 Link: https://pynative.com/python-if-else-and-for-loop-exercise-with-solutions/#h-exercise-12-display-fibonacci-series-up-to-10-terms.
And I have managed to do this but there are also a bunch of random values printed after the number 55 and I have no idea why this happened. Please guide me on how to remove these values. Also, tell me how do you make a python list with placeholder values? I have used "None" keyword in my list to do this but I feel like there has to be a better way to do this:
list1=[0, 1, None, None, None, None, None, None, None, None]
for i in range(2, 11):
list1[i]=list1[i-1]+list1[i-2]
list1.append(list1[i])
print(list1)
OUTPUT: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 1, 2, 3, 5, 8, 13, 21, 34]. Why are these extra values after 34 being printed in the list? Please explain. I expect the output of this to be: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34].
Instead of preallocating None's in a list, you can
appendnew values: