Python: Sequence Types

By Xah Lee. Date: . Last updated: .

In Python, String, List, Tuple, are called “sequence types”. They all have the same methods. Here's example of operations that can be used on sequence type.

# operations on sequence types

# a list
ss = [0, 1, 2, 3]

# length
print(len(ss)) # 4

# ith item
print(ss[0]) # 0

# slice of items
print(ss[0:3])    # [0, 1, 2]

# slice of items with jump step
print(ss[0:10:2]) # [0, 2]

# check if a element exist
print(3 in ss)    # True. (or False)

# check if a element does NOT exist
print(3 not in ss) # False

# concatenation
print(ss + ss)   # [0, 1, 2, 3, 0, 1, 2, 3]

# repeat
print(ss * 2)    # [0, 1, 2, 3, 0, 1, 2, 3]

# smallest item
print(min(ss))    # 0

# largest item
print(max(ss))    # 3

# index of the first occurrence
print(ss.index(3))   # 3

# total number of occurrences
print(ss.count(3))   # 1

Python Data Structure

Python

Overview

Help

Comment

String

Arithmetic

Boolean

Conditional

Loop, Iteration

Data Structure

Function and Class

Module

Misc

Regex

Text Processing

Web

Misc