Page 300 - Computer Science Class 11 Without Functions
P. 300
List: [14, 6, 1, 8, 14, 50, 61, 89, 37, 109, 3, 21, 89, 90, 60]
>>> Enter the number to be searched:37
37 is present in the list at index 8
Continue another search? say Y/y for yes, N/n for no:Y
>>> Enter the number to be searched:10
10 is not present in the list
Continue another search? say Y/y for yes, N/n for no:n
An execution of Program 12.1
Creating a Sorted List
The function sorted() returns a sorted list comprising the elements of the list passed as argument, but without
modifying it.
>>> lst = ['Physics', 'Chemistry', 'Maths', 'Computer Sc.']
>>> sorted(lst)
['Chemistry', 'Computer Sc.', 'Maths', 'Physics']
>>> lst
['Physics', 'Chemistry', 'Maths', 'Computer Sc.']
Quick Programming Question
Program 12.2 Write a program that accepts a list of integers and displays the frequency of each element in the list.
01 '''
02 Objective: To count the frequency of each element in mylist
03 User Interface:
04 User is asked to enter:
05 number of elements in the list
06 list elements one by one
07 '''
08 myList = []
09 numElements = int(input('Enter size of the list: '))
10 print('Enter each element and press enter: ')
11 for i in range(0, numElements): #list input element-wise
12 num = int(input())
13 myList.append(num)
14 print('Contents of list:', myList)
15 uniqueMyList = set(myList)
16 for item in uniqueMyList:
17 print(f'Count of {item} in list is: {myList.count(item)}')
Sample output:
>>> Enter size of the list: 10
>>> Enter each element and press enter:
3
7
1
3
9
1
8
2
8
1
Contents of list: [3, 7, 1, 3, 9, 1, 8, 2, 8, 1]
Count of 1 in list is: 3
Count of 2 in list is: 1
Count of 3 in list is: 2
Count of 7 in list is: 1
Count of 8 in list is: 2
298 Touchpad Computer Science-XI

