0% found this document useful (0 votes)
6 views4 pages

Program 2

The document contains two Python programs: one for generating a Fibonacci sequence of a specified length and another for performing various operations on a list, including inserting, removing, appending elements, and displaying the list's length. The Fibonacci program prompts the user for input and handles edge cases for non-positive integers. The list program features a menu-driven interface allowing users to interactively manage the list until they choose to exit.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Program 2

The document contains two Python programs: one for generating a Fibonacci sequence of a specified length and another for performing various operations on a list, including inserting, removing, appending elements, and displaying the list's length. The Fibonacci program prompts the user for input and handles edge cases for non-positive integers. The list program features a menu-driven interface allowing users to interactively manage the list until they choose to exit.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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")

You might also like