Robotics & Artificial Intelligence

Write a Python program to:

  1. Create a list of 10 numbers.
  2. Print the elements in the 2nd and 4th index of the list.
  3. Sort the list in ascending order.
  4. Ask the user to enter a number and search whether it exists in the list or not.

Python List Manipulation

1 Like

Answer

lst = [12, 45, 7, 23, 89, 34, 56, 90, 11, 5]

second_index = lst[2]
fourth_index = lst[4]

print("Element at 2nd index:", second_index)

print("Element at 4th index:", fourth_index)

lst.sort()

print("Sorted list:")
print(lst)

num = int(input("Enter a number to search: "))

if num in lst:
    print("Number exists in the list")
else:
    print("Number does not exist in the list")

Output

Element at 2nd index: 7
Element at 4th index: 89
Sorted list:
[5, 7, 11, 12, 23, 34, 45, 56, 89, 90]
Enter a number to search: 23
Number exists in the list

Answered By

1 Like


Related Questions