ComputerNotes_Class11th_IP_04
ComputerNotes_Class11th_IP_04
Ex:
>>>myList[0]
3 //first element of list ‘myList’
2
>>>myList[-2]
10 //4th element of list ‘myList’
Ex:2
Ex:2
>>> list3 = ['Red','Green','Blue']
>>> list4 = ['Cyan', 'Magenta', 'Yellow' ,'Black']
>>> list3 + list4
['Red','Green','Blue','Cyan','Magenta', 'Yellow','Black']
Ex:3 '+’ requires that the operands should be of list type only
>>> list1 = [1,2,3] ← list1 is type of list
>>> str1 = "abc" ← str1 is type of str
>>> list1 + str1
TypeError: can only concatenate list (not "str") to list
LAB Exercise
What will be the output of following python programs
4.2.2 Repetition
Python allows us to replicate the contents of a list using repetition operator * .
>>> list1 = ['Hello']
4.2.3 Membership
The membership operator (in) checks if the element is present in the list and returns True,
else returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Green' in list1
True
Operator (not in) returns True if the element is not present in the list, else it returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Cyan' not in list1
True
4.2.4 Slicing
Slicing operations allow us to create new list by taking out elements from an existing list.
>>> list1 =['Red','Green','Blue','Cyan', 'Magenta','Yellow','Black']
#subject from indexes 2 to 5 of list 1
4
len() Returns the length of the list passed as >>> list1 = [10,20,30,40,50]
the argument >>> len(list1)
5
extend() Appends each element of the list passed >>> list1 = [10,20,30]
as argument at the end of the given list >>> list2 = [40,50]
>>> list1.extend(list2)
>>> list1
[10, 20, 30, 40, 50]
2024-25
2024-25
Program 4-1 Write a program to allow user to perform any those list operation
given in a menu. The menu is:
1. Append an element
2. Insert an element
3. Append a list to the given list
4. Modify an existing element
5. Delete an existing element from its position
6. Delete an existing element with a given value
7. Sort the list in the ascending order
8. Sort the list in descending order
9. Display the list.
#Program 4-1
#Menu driven program to do various list operations
myList = [22,4,16,38,13] #myList having 5 elements
choice = 0
For attempt in range (3): print ("Attempt number:", attempt)
print("The list 'myList' has the following elements", myList)
print("\nL I S T O P E R A T I O N S")
print(" 1. Append an element")
print(" 2. Insert an element at the desired position")
print(" 3. Append a list to the given list")
print(" 4. Modify an existing element")
print(" 5. Delete an existing element by its position")
print(" 6. Delete an existing element by its value")
2024-25
#append element
if choice == 1:
element = eval(input("Enter the element to be appended: "))
myList.append(element)
print("The element has been appended\n")
2024-25
2024-25
The list 'myList' has the following elements [38, 22, 13, 4]
Attempt number : 3
L I S T O P E R A T I O N S
1. Append an element
2. Insert an element at the desired position
3. Append a list to the given list
4. Modify an existing element
5. Delete an existing element by its position
6. Delete an existing element by its value
7. Sort the list in ascending order
8. Sort the list in descending order
9. Display the list
ENTER YOUR CHOICE (1-9) 10
choice is not valid
2024-25
position = -1
for i in range (0, lin (list1)
if list1[i] == num: #number is present
position = i+1 #save the position of number
if position == -1 :
print("Number",num,"is not present in the list")
else:
print("Number",num,"is present at",position + 1, "position")
Output:
How many numbers do you want to enter in the list
5
2024-25
#dict3 is the dictionary that maps names of the students to marks in percentage
>>> dict3 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
>>> dict3
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
2024-25
del() Deletes the item with the given >>> dict1 = {'Mohan':95,'Ram':89,
key 'Suhel':92, 'Sangeeta':85}
To delete the dictionary from the >>> del dict1['Ram']
memory we write: >>> dict1
del Dict_name
{'Mohan':95,'Suhel':92, 'Sangeeta': 85}
>>> dict1
NameError: name 'dict1' is not defined
2024-25
(i) Delete the item from the dictionary, corresponding to the key 9. ‘ODD’
>>> del ODD[9]
>>> ODD
{1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven'}
2024-25
'Joseph' 24000
'Rahul' 30000
'Zoya' 25000
result = ''
for ch in num:
key = int(ch) #converts character to integer
value = numberNames[key]
2024-25
Summary
• Lists are mutable sequences in Python, i.e. we can
change the elements of the list.
• Elements of a list are put in square brackets
separated by comma.
• List indexing is same as that of list and starts at 0.
Two way indexing allows traversing the list in the
forward as well as in the backward direction.
• Operator + concatenates one list to the end of other
list.
• Operator * repeats the content of a list by
specified number of times.
• Membership operator in tells if an element is
present in the list or not and not in does the
opposite.
• Slicing is used to extract a part of the list.
• There are many list manipulation methods. Few
are: len(), list(), append(), extend(), insert(), count(),
find(), remove(), pop(), reverse(), sort(), sorted(),
min(), max(), sum().
• Dictionary is a mapping (non scalar) data type. It
is an unordered collection of key-value pair; key-
value pair are put inside curly braces.
• Each key is separated from its value by a colon.
• Keys are unique and act as the index.
• Keys are of immutable type but values can be
mutable.
2024-25