List
List
elements.
▪The elements can be of any data type.
▪The list is a most versatile data type available in
Python which can be written as a list of comma-
separated values (items) between square brackets.
▪List are mutable, meaning, their elements can
be changed.
• In Python programming, a list is created by placing all the items
(elements) inside a square bracket [ ], separated by commas.
• It can have any number of items and they may be of different types
(integer, float, string etc.).
my_list = ['p','r','o','b','e']
# Output: e
print(my_list[-1])
# Output: p
print(my_list[-5])
#Add Elements
▪ add one item to a list using append()
#Change Elements method
= operator with index ▪ add several items using extend()
▪ insert one item at a desired location by
>>> marks=[90,60,80] using the method insert()
>>> print(marks) >>> marks.append(50)
>>> print(marks)
[90, 60, 80]
[90, 100, 80,
>>> marks[1]=100 50]
>>> marks.extend([60,80,70])
>>> >>> print(marks)
print(marks) [90, 100, 80, 50, 60, 80, 70]
[] 100
>>> print(marks)
[20]
▪ >>>
marks.extend([40,50,60,70])
▪ >>> marks
▪ [90, 40, 50, 60, 70]
▪ >>> marks.pop()
▪ 70
▪ >>> marks.pop()
▪ 60
▪ Slicing [::] (i.e)
list[start:stop:step]
▪ Concatenation = +
▪ Repetition= *
▪ Membership = in
▪ Identity = is
append() - Add an element to the end of the list