Tuple
Tuple
▪ my_list = ['p','r','o','b','e']
▪ # Output: e
▪ print(my_list[-1])
▪ # Output: p
▪ print(my_list[-5])
▪ To change elements use = operator with index[]
▪ >>> marks=[90,60,80]
▪ >>> print(marks)
▪ [90, 60, 80]
▪ >>> marks[1]=100
▪ >>> print(marks) [90, 100, 80]
▪ A new element or item can be added to an existing list using append() method.
▪ This method adds the item to the end of the list
▪ >>> marks=[90,60,80]
▪ >>> marks.append(50)
▪ >>> print(marks)
▪ >>> [90, 60, 80, 50] #output
▪ To insert one item at a desired location use the method insert()
▪ >>list.insert(index,element)
▪ >>> marks.insert(2,40)
▪ >>> print(marks)
▪ [90, 60, 40, 80, 50]
▪ extend Method: This method works like concatenation.
▪ It takes a list as an argument and adds it to the end of another list.
▪ >>> list1 = [‘x’,’y’,’z’]
▪ >>> list2 = [1,2,3]
▪ >>> list1.extend(list2)
▪ >>> print list1
▪ [‘x’, ‘y’, ‘z’, 1, 2, 3] # Output
>>>
▪ >>> marks.extend([60,80,70])
▪ >>> print(marks)
▪ [90, 60, 40,80, 50, 60, 80, 70]
▪ Python provides many ways in which the elements in a list can be deleted
1.Pop(): pop() method is used to remove an item at the given index.
▪ The pop operator deletes the element on the provided index and stores that
element in a variable for further use
▪ >>> list = [10,20,30,40]
▪ >>> a = list.pop(2)
▪ >>> print(list)
▪ [10,20,40] # Output
▪ >>> print (a)
▪ 30 # Output
2. del Operator :The del operator deletes the value on the provided index, but it
does not store the value for further use.
▪ It can even delete the list entirely.
>>> list = [‘w’,‘x’,’y’,’z’]
>>> del (list[1])
>>> print(list)
[‘w’, ‘y’, ‘z’] # Output
▪ >>> del list
▪ >>>print (list)
▪ Name Error: name ‘list' is not defined
▪ clear() method to empty a list.
▪ list.clear()
▪ print(list)
▪ [] #output
3. remove Operator
▪ We use the remove operator if we know the item that we want to remove or
delete from the list (but not the index).
>>> list = [10,20,30,40]
>>> list.remove(10)
>>> print(list)
[20,30,40] # Output
▪ In order to delete more than one value from a list, del operator with slicing is
used.
>>> list = [1,2,3,4,5,6,7,8]
>>> del list[1:3]
>>> print (list)
[1,4,5,6,7,8] # Output
▪ Slicing [::] (i.e) list[start:stop:step]
▪ Concatenation = +
▪ Repetition= *
▪ Membership = in
▪ 1. Concatenation
▪ The concatenation operator works in lists in the same way it does in a
string. This operator concatenates two lists. This is done by the + operator
in Python
▪ >>> list1 = [10,20,30,40]
▪ >>> list2 = [50,60,70]
▪ >>> list3 = list1 + list2
▪ >>> print (list3)
▪ [10,20,30,40,50,60,70] # Output
▪ 2. Repetition
▪ The repetition operator works as suggested by its name; it repeats the list
for a given number of times.
▪ Repetition is performed by the * operator.
▪ >>> list1 = [1,2,3]
▪ >>> list1 * 4
▪ [1,2,3,1,2,3,1,2,3,1,2,3] # Output
▪ >>> [2] * 6
▪ [2,2,2,2,2,2] # Output
▪ 3. In Operator
▪ The In operator tells the user whether the given string exists in the list or not.
▪ It gives a Boolean output, i.e., True or False.
▪ If the given input exists in the list, it gives True as output, otherwise, False
▪ >>> list = [‘Hello’, ‘Python’, ‘Program’]
▪ >>> ‘Hello’ in list
▪ True # Output
▪ >>> list = [10,20,30,40]
▪ >>> 10 in list
▪ True # Output
▪ >>> 50 in list
▪ False # Output
4.Slicing
▪ List can be sliced in the same way as strings
▪ list[start:stop:step]
▪ list[n:m] produces a slice with elements from index n to index m-1
▪ list=[2,3,4,5,6]
▪ print(list[1:4])
▪ [3,4,5]
▪ print(list[-4:-1])
▪ [3,4,5]