Python Sequence Data Types - Complete Guide
1. Sequence Data Types
A sequence is an ordered collection of items where each item is stored at a specific position called an index. Python supports the following sequence types:
- Lists
- Tuples
- Dictionaries (although technically not a sequence, they are often grouped due to their key-value structure)
2. Lists
Definition: A list is an ordered, mutable collection of items. Lists are written using square brackets [ ]
, and items are separated by commas.
Characteristics of Lists:
- Ordered: Items maintain a specific order.
- Mutable: Items can be changed, added, or removed.
- Duplicates allowed: Lists can contain duplicate items.
- Heterogeneous: Lists can store multiple data types.
# Example: List in Python fruits = ['apple', 'banana', 'cherry', 'apple'] print(fruits[0]) # Output: apple fruits[1] = 'blueberry' print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'apple'] fruits.append('mango') print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'apple', 'mango']
3. Tuples
Definition: A tuple is an ordered, immutable collection of items. Tuples are written using parentheses ( )
, and items are separated by commas.
Characteristics of Tuples:
- Ordered: Items maintain a specific order.
- Immutable: Items cannot be changed after creation.
- Duplicates allowed: Tuples can contain duplicate items.
- Heterogeneous: Tuples can store multiple data types.
# Example: Tuple in Python numbers = (1, 2, 3, 4, 5) print(numbers[1]) # Output: 2 # numbers[1] = 10 # TypeError: 'tuple' object does not support item assignment a, b, c, d, e = numbers print(a, b) # Output: 1 2
4. Dictionaries
Definition: A dictionary is an unordered collection of key-value pairs. Dictionaries are written using curly braces { }
, and key-value pairs are separated by a colon :
.
Characteristics of Dictionaries:
- Key-Value Pairs: Keys are unique, and values are associated with these keys.
- Mutable: Key-value pairs can be added, modified, or removed.
- Unordered: Keys do not maintain a specific order.
# Example: Dictionary in Python student = {'name': 'John', 'age': 25, 'course': 'Python'} print(student['name']) # Output: John student['age'] = 26 print(student) # Output: {'name': 'John', 'age': 26, 'course': 'Python'} student['grade'] = 'A' print(student) # Output: {'name': 'John', 'age': 26, 'course': 'Python', 'grade': 'A'}
5. Slicing
Definition: Slicing is used to extract a subset of elements from a sequence using the syntax:
sequence[start:stop:step]
Example:
numbers = [0, 1, 2, 3, 4, 5] print(numbers[1:4]) # Output: [1, 2, 3] print(numbers[:3]) # Output: [0, 1, 2] print(numbers[::2]) # Output: [0, 2, 4]
6. Indexing
Definition: Indexing is used to access individual elements in a sequence. Indexing starts at 0.
fruits = ['apple', 'banana', 'cherry'] print(fruits[0]) # Output: apple print(fruits[-1]) # Output: cherry (last element)
7. Concatenation
Definition: Concatenation combines two or more sequences using the +
operator.
list1 = [1, 2, 3] list2 = [4, 5, 6] combined = list1 + list2 print(combined) # Output: [1, 2, 3, 4, 5, 6]
8. Other Operations on Sequence Data Types
- len(): Returns the length of the sequence.
- max(): Returns the maximum value.
- min(): Returns the minimum value.
- in: Checks if an element exists in the sequence.
numbers = [10, 20, 30, 40] print(len(numbers)) # Output: 4 print(max(numbers)) # Output: 40 print(min(numbers)) # Output: 10 print(20 in numbers) # Output: True