0% found this document useful (0 votes)
8 views3 pages

Python Programs for Fibonacci and List Operations

The document contains two Python programs: the first generates a Fibonacci sequence of a specified length (N) input by the user, while the second demonstrates various list operations such as inserting, removing, appending elements, displaying the list's length, popping an element, and clearing the list. Both programs include user prompts and print statements to display results. The document also indicates space for output results following the code examples.

Uploaded by

savindhesh2020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Python Programs for Fibonacci and List Operations

The document contains two Python programs: the first generates a Fibonacci sequence of a specified length (N) input by the user, while the second demonstrates various list operations such as inserting, removing, appending elements, displaying the list's length, popping an element, and clearing the list. Both programs include user prompts and print statements to display results. The document also indicates space for output results following the code examples.

Uploaded by

savindhesh2020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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:

You might also like