2.a. Develop a program to generate Fibonacci sequence of length (N).
Read N from the console.
n = int(input("Enter the value of N: "))
# First two Fibonacci numbers
a, b = 0, 1
print("Fibonacci sequence:")
if n <= 0:
print("Please enter a positive integer.")
elif n == 1:
print(a)
else:
print(a, b, end=" ")
for i in range(2, n):
c=a+b
print(c, end=" ")
a, b = b, c
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
# Creating a list
my_list = [2, 17, 19, 4, 9, 21, 26, 35]
while True:
print("\n--- MENU ---")
print("1. Insert element")
print("2. Append element")
print("3. Remove element")
print("4. Display length")
print("5. Pop element")
print("6. Clear list")
print("7. Display list")
print("8. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
ele = int(input("Enter element: "))
pos = int(input("Enter position: "))
my_list.insert(pos, ele)
print("List:", my_list)
elif choice == 2:
ele = int(input("Enter element: "))
my_list.append(ele)
print("List:", my_list)
elif choice == 3:
ele = int(input("Enter element to remove: "))
if ele in my_list:
my_list.remove(ele)
print("List:", my_list)
else:
print("Element not found")
elif choice == 4:
print("Length of list:", len(my_list))
elif choice == 5:
if len(my_list) > 0:
print("Popped element:", my_list.pop())
print("List:", my_list)
else:
print("List is empty")
elif choice == 6:
my_list.clear()
print("List cleared")
elif choice == 7:
print("List:", my_list)
elif choice == 8:
print("Exiting...")
break
else:
print("Invalid choice")