Understand Common Sequence Data Types in Python – String, Tuple, and List

string, tuple, and list are the three common build-in ordered collection data types in Python. Those sequence data types share some common operations.

Common Sequence Operations in Python
Name Operator Example
reference: Operations on Any Sequence in Python (interactivepython.org), 5.6 Sequence Types
indexing [n]
            data = [1,2,3,4,5]
            data[3] # return 4
concatenation +
            data = [1,2,3,4,5]
            data + [9] # return [1,2,3,4,5,9]
repetition *
            data = [1,2,3,4,5]
            data * 2 # return [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
membership in
            data = [9,2,4,4,6,2,8]
            for val in data: print(val), # return 9 2 4 4 6 2 8
length len()
            data = [1,2,3,4,5]
            len(data) # return 5
slicing with step k [i:j:k]
            data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
            data[0:10:2] # return [1, 3, 5, 7, 9]
slicing [i:j]
            data = [1,2,3,4,5]
            data[1:3] # return [2,3]
Minimum min(data)
            data = [1,2,3,4,5]
            min(data) # return 1
Maximum max(data)
            data = [1,2,3,4,5]
            max(data) # return 5
Index data.index(sub[, start[, end]])
            data = [1,2,3,4,5]
            data.index(3) # return 2
Count data.count(i)
            data = [1,2,3,4,5,3]
            data.count(3) # return 2

Continue reading