Some basic sequence type classes in python are, list, tuple, range. There are some additional sequence type objects, these are binary data and text string.
Some common operations for the sequence type object can work on both mutable and immutable sequences. Some of the operations are as follows −
Sr.No. | Operation/Functions & Description |
---|---|
1 | x in seq True, when x is found in the sequence seq, otherwise False |
2 | x not in seq False, when x is found in the sequence seq, otherwise True |
3 | x + y Concatenate two sequences x and y |
4 | x * n or n * x Add sequence x with itself n times |
5 | seq[i] ith item of the sequence. |
6 | seq[i:j] Slice sequence from index i to j |
7 | seq[i:j:k] Slice sequence from index i to j with step k |
8 | len(seq) Length or number of elements in the sequence |
9 | min(seq) Minimum element in the sequence |
10 | max(seq) Maximum element in the sequence |
11 | seq.index(x[, i[, j]]) Index of the first occurrence of x (in the index range i and j) |
12 | seq.count(x) Count total number of elements in the sequence |
13 | seq.append(x) Add x at the end of the sequence |
14 | seq.clear() Clear the contents of the sequence |
15 | seq.insert(i, x) Insert x at the position i |
16 | seq.pop([i]) Return the item at position i, and also remove it from sequence. Default is last element. |
17 | seq.remove(x) Remove first occurrence of item x |
18 | seq.reverse() Reverse the list |
Example Code
myList1 = [10, 20, 30, 40, 50] myList2 = [56, 42, 79, 42, 85, 96, 23] if 30 in myList1: print('30 is present') if 120 not in myList1: print('120 is not present') print(myList1 + myList2) #Concatinate lists print(myList1 * 3) #Add myList1 three times with itself print(max(myList2)) print(myList2.count(42)) #42 has two times in the list print(myList2[2:7]) print(myList2[2:7:2]) myList1.append(60) print(myList1) myList2.insert(5, 17) print(myList2) myList2.pop(3) print(myList2) myList1.reverse() print(myList1) myList1.clear() print(myList1)
Output
30 is present 120 is not present [10, 20, 30, 40, 50, 56, 42, 79, 42, 85, 96, 23] [10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50] 96 2 [79, 42, 85, 96, 23] [79, 85, 23] [10, 20, 30, 40, 50, 60] [56, 42, 79, 42, 85, 17, 96, 23] [56, 42, 79, 85, 17, 96, 23] [60, 50, 40, 30, 20, 10] []