Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Sequence Types
In Python programming, sequence types are fundamental data structures that hold an ordered collection of items. The main sequence types include Lists, Strings, Tuples, and Range objects. These data structures allow us to access their elements through indexing and iteration.
Sequence Types in Python
Sequence types in Python are categorized into two main types: mutable and immutable sequences.
Mutable Sequence Types
These sequences can be changed after their creation. You can modify elements, add new elements, and remove existing ones.
- Lists: A mutable, ordered collection of items that can store different data types.
- Byte Arrays: A mutable sequence of bytes, used for handling binary data.
- Arrays: A collection of items similar to lists, but optimized for numeric operations and storing elements of the same data type.
Example
import array
# List example
my_list = [10, 20, 30, 40, 50]
my_list.append(60)
print("After append:", my_list)
my_list.remove(10)
print("After remove:", my_list)
# Byte Array example
my_byte_array = bytearray([15, 16, 17, 18])
my_byte_array[1] = 19
print("Modified byte array:", my_byte_array)
# Array example
my_array = array.array('i', [21, 22, 23, 24, 25])
my_array[2] = 26
print("Modified array:", my_array)
After append: [10, 20, 30, 40, 50, 60]
After remove: [20, 30, 40, 50, 60]
Modified byte array: bytearray(b'\x0f\x13\x11\x12')
Modified array: array('i', [21, 22, 26, 24, 25])
Immutable Sequence Types
These sequences cannot be changed once created. Any operation that appears to modify them actually creates a new sequence.
- Tuples: Ordered collections of objects that are immutable.
- Strings: Sequences of characters enclosed in single ('') or double ("") quotes.
- Range Objects: Immutable sequences of numbers commonly used for looping.
Example
# Tuple example
my_tuple = (10, 20, 30, 40, 50)
print("Element at index 3:", my_tuple[3])
new_tuple = my_tuple + (60, 70)
print("New tuple:", new_tuple)
# String example
my_string = "Tutorials Point"
print("Character at index 4:", my_string[4])
new_string = my_string + " Python"
print("New string:", new_string)
# Range example
my_range = range(1, 6)
print("Range elements:", list(my_range))
Element at index 3: 40 New tuple: (10, 20, 30, 40, 50, 60, 70) Character at index 4: r New string: Tutorials Point Python Range elements: [1, 2, 3, 4, 5]
Common Sequence Operations
Python provides several built−in operations that work with all sequence types. Here are the most commonly used ones.
Membership Operators
Membership operators (in and not in) check if an element exists in a sequence ?
| Operation | Syntax | Description |
|---|---|---|
| in | x in seq | Returns True if x is found in sequence |
| not in | x not in seq | Returns True if x is not found in sequence |
languages = ['java', 'python', 'c++']
print('python' in languages)
print('javascript' not in languages)
True True
Concatenation and Repetition
Most sequence types support concatenation and repetition operations ?
| Operation | Syntax | Description |
|---|---|---|
| Concatenation | seq1 + seq2 | Combines two sequences together |
| Repetition | seq * n | Repeats the sequence n times |
list1 = [10, 20, 30]
list2 = [40, 50]
# Concatenation
result = list1 + list2
print("Concatenated:", result)
# Repetition
repeated = list1 * 3
print("Repeated:", repeated)
Concatenated: [10, 20, 30, 40, 50] Repeated: [10, 20, 30, 10, 20, 30, 10, 20, 30]
Indexing and Slicing
Indexing accesses individual elements using their position (starting from 0). Slicing extracts subsequences using start, stop, and step values ?
| Operation | Syntax | Description |
|---|---|---|
| Indexing | seq[index] | Access element at specific position |
| Slicing | seq[start:stop:step] | Extract subsequence with optional step |
items = ['a', 'b', 'c', 'd', 'e']
# Indexing
print("Element at index 2:", items[2])
# Slicing
print("Slice [1:4]:", items[1:4])
# Slicing with step
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print("Every second element:", numbers[1:8:2])
Element at index 2: c Slice [1:4]: ['b', 'c', 'd'] Every second element: [1, 3, 5, 7]
Length, Search, and Count
These operations help you analyze sequence contents ?
| Operation | Syntax | Description |
|---|---|---|
| Length | len(seq) | Returns number of elements |
| Search | seq.index(item) | Finds first occurrence index |
| Count | seq.count(item) | Counts element occurrences |
numbers = [1, 5, 4, 5, 3, 20, 12, 5, 43]
print("Length:", len(numbers))
print("Index of 12:", numbers.index(12))
print("Count of 5:", numbers.count(5))
Length: 9 Index of 12: 6 Count of 5: 3
Mutable Sequence Operations
These operations work only with mutable sequences like lists and byte arrays ?
| Operation | Syntax | Description |
|---|---|---|
| Append | seq.append(item) | Add element at the end |
| Pop | seq.pop([index]) | Remove and return element |
| Remove | seq.remove(value) | Remove first occurrence |
data = [10, 20, 30, 40, 50]
# Append element
data.append(60)
print("After append:", data)
# Pop element at index 1
removed = data.pop(1)
print("Popped:", removed)
print("After pop:", data)
# Remove element by value
data.remove(40)
print("After remove:", data)
After append: [10, 20, 30, 40, 50, 60] Popped: 20 After pop: [10, 30, 40, 50, 60] After remove: [10, 30, 50, 60]
Conclusion
Python sequence types provide powerful tools for organizing and manipulating ordered data. Use mutable sequences like lists when you need to modify data, and immutable sequences like tuples for fixed collections that won't change.
