6(iii)
Write a Python program to:
- (a)Create a list of 10 numbers.
- (b)Print the elements in the 2nd and 4th index of the list.
- (c)Sort the list in ascending order.
- (d)Ask the user to enter a number and search whether it exists in the list or not.
Show SolutionHide Solution↓
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")
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