Python List
For 5th Semester Hons
By
Sumana Bandyopadhyay
to a list can be of different data type.
>>> math = 45
>>> science = 23
>>> social = 28
>>> marksList = [math, science, social]
>>>
>>> print(marksList) [45, 23, 28]
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
# Create list areas
# Print areas
SN | Function with Description |
1 | len(list) Gives the total length of the list. |
2 | max(list) Returns item from the list with max value. |
3 | min(list) Returns item from the list with min value. |
4. | sum(list) Returns the sum of all elements of list |
operator.
| | |
Insertion | Append(ele) | Add element at end of List |
Insert(index , ele) | Add element at given index | |
Extend( seq ) | Appends all the elements of seq to list | |
| | |
Deletion | Pop(index) | Delete element based on Index |
Remove(key) | Delete element based on key | |
| | |
| Count(key) | Returns the number of occurrences of key |
| Index(key) | Returns the index of key element |
| Sort | Sorts elements of List |
| Reverse | Reverses elements of list |
mylist = [] mylist.append(5) mylist.append(8) mylist.append(12)
#prints [5,8,12]
print(mylist)
insert(index, element)
element to insert or till end of list
a ValueError exception indicating the element is not in the list.
If the item isn't in the list, it raises a ValueError.
2
0
ValueError: 'z' is not in list