Develop a program
to generate
Fibonacci sequence
of length (N). Read N
from the console.
Python Code:
num =
int(input("Enter the
Fibonacci sequence
length to be
generated: "))
firstTerm = 0
secondTerm = 1
print("The Fibonacci
series with", num,
"terms is:")
print(firstTerm,
secondTerm, end="
")
for i in range(2,
num):
curTerm = firstTerm
+ secondTerm
print(curTerm,
end=" ")
firstTerm =
secondTerm
secondTerm =
curTerm
print()
Output 1:
Enter the Fibonacci
sequence length to be
generated: 8
The Fibonacci series
with 8 terms is :
011235813
Output 2:
Enter the Fibonacci
sequence length to be
generated: 5
The Fibonacci series
with 5 terms is :
01123
Viva Questions
1 What does the
input function do in
Python?
2. Explain
2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console.
num = int(input("Enter the Fibonacci sequence length to be generated: "))
firstTerm = 0
secondTerm = 1
print("The Fibonacci series with", num, "terms is:")
print(firstTerm, secondTerm, end=" ")
for i in range(2, num):
curTerm = firstTerm + secondTerm
print(curTerm, end=" ")
firstTerm = secondTerm
secondTerm = curTerm
print()
output:
Output 2:
Enter the Fibonacci
sequence length to be
generated: 5
Output 2:
Enter the Fibonacci
sequence length to be
generated: 5
Enter the Fibonacci sequence length to be generated : 5
The Fibonacci series with 5 items is: 0 1 1 2 3
b. Write a python program to create a list and perform the following operations
• Inserting an element
• Removing an element
• Appending an element
• Displaying the length of the list
• Popping an element
• Clearing the list
[Link] Square Brackets
We use square brackets [] to create a list directly.
a = [1, 2, 3, 4, 5] # List of integers
b = ['apple', 'banana', 'cherry'] # List of strings
c = [1, 'hello', 3.14, True] # Mixed data types
print(a)
print(b)
print(c)
output :
[1, 2, 3, 4, 5]
['apple', 'banana', 'cherry']
[1, 'hello', 3.14, True]
[Link] Elements from List
We can remove elements from a list using
a = [10, 20, 30, 40, 50]
[Link](30)
print("After remove(30):", a)
popped_val = [Link](1)
print("Popped element:", popped_val)
print("After pop(1):", a)
del a[0]
print("After del a[0]:", a)
output :
After remove(30): [10, 20, 40, 50]
Popped element: 20
After pop(1): [10, 40, 50]
After del a[0]: [40, 50]
[Link] Elements into List
We can add elements to a list using the following methods
a = []
[Link](10)
print("After append(10):", a)
[Link](0, 5)
print("After insert(0, 5):", a)
[Link]([15, 20, 25])
print("After extend([15, 20, 25]):", a)
[Link]()
print("After clear():", a)
output :
After append(10): [10]
After insert(0, 5): [5, 10]
After extend([15, 20, 25]): [5, 10, 15, 20, 25]
After clear(): []
[Link] Elements into List
Since lists are mutable, we can update elements by accessing them via their index
a = [10, 20, 30, 40, 50]
a[1] = int(input("Enter the value:"))
print(a)
output :
Enter the value: 25
[10, 25, 30, 40, 50]
[Link] Over Lists
We can iterate over lists using loops, which is useful for performing actions on each item
a = ['apple', 'banana', 'cherry']
for item in a:
print(item)
output :
apple
banana
cherry