2.a. Develop a program to generate Fibonacci sequence of length (N).
Read N from the console
[Link]
# Read input from user
N = int(input("Enter the length of Fibonacci sequence (N): "))
if N <= 0:
print("Error! Please enter a positive number greater than 0.")
else:
# First two Fibonacci numbers
a, b = 0, 1
print("Fibonacci sequence of length", N, ":")
for i in range(N):
print(a, end=" ")
a, b = b, a + b
OUTPUT:
Leave half a page for the output
2.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
# Create an empty list
my_list = []
# Insert an element at a specific position
my_list.insert(0, 10) # insert 10 at index 0
print("After inserting 10 at index 0:", my_list)
# Append an element at the end
my_list.append(20)
print("After appending 20:", my_list)
# Append another element
my_list.append(30)
print("After appending 30:", my_list)
# Remove an element by value
my_list.remove(20)
print("After removing 20:", my_list)
# Display the length of the list
print("Length of the list:", len(my_list))
# Pop (remove) last element
popped = my_list.pop()
print("After popping element:", my_list, "| Popped element:", popped)
# Clear the list
my_list.clear()
print("After clearing the list:", my_list)
OUTPUT: