Sequences in Python
In Python, sequences are ordered collections of items, where each item is accessible by its position (index) in the collection. Sequences support various operations including indexing, slicing, and iteration. The most common sequence types in Python are:
Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable ordered sequence of items - they can be changed. Elements can be reassigned or removed, and new elements can be inserted.
Tuples are like lists, but they are immutable ordered sequence of items - they can't be changed.
Strings are a special type of sequence that can only store characters, and they are immutable.
Sequence Operations
Indexing
Accessing an element by its position.
Example:
my_list = [10, 20, 30, 40, 50]
printtems(my_list[0]) # Output: 10
print(my_list[-1]) # Output: 50 (last element)
Slicing
Accessing a sub sequence by specifying a start and end index.
my_list = [10, 20, 30, 40, 50]
print(my_list[1:4]) # Output: [20, 30, 40]
print(my_list[:3]) # Output: [10, 20, 30] (from start to index 2)
print(my_list[3:]) # Output: [40, 50] (from index 3 to end)
Combining two sequences of the same type.+ combines two sequences in a process called concatenation.
list2 = [4, 5, 6]
print(list1 + list2) # Output: [1, 2, 3, 4, 5, 6]
str1 = "Hello"
str2 = "World"
print(str1 + " " + str2) # Output: Hello World
Repetition
Repeating a sequence a given number of times.
Membership Testing
my_list = [1, 2, 3]
print(my_list * 3) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
my_string = "Hi"
print(my_string * 3) # Output: HiHiHi
Checking if an element is in a sequence.
Iteration
len()
max()
min()
sum()
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
print(6 in my_list) # Output: False
Iterating over elements in a sequence.
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)
Useful Functions
Python provides a variety of built-in functions that can be used with sequences. These functions are useful for performing common operations such as finding the length of a sequence, sorting, and more. Here are some of the most useful sequence functions in Python:
len()
Description: Returns the number of items in a sequence.
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # Output: 5
my_string = "Hello, World!"
print(len(my_string)) # Output: 13
Description: Returns the largest item in a sequence.
my_list = [1, 2, 3, 4, 5]
print(max(my_list)) # Output: 5
my_string = "Hello, World!"
print(max(my_string)) # Output: r
Description: Returns the smallest item in a sequence.
my_list = [1, 2, 3, 4, 5]
print(min(my_list)) # Output: 1
my_string = "Hello, World!"
print(min(my_string)) # Output: ' ' (space)
sum()
Description: Returns the sum of all items in a sequence.
sorted()
my_list = [1, 2, 3, 4, 5]
print(sum(my_list)) # Output: 15
# Note: sum() is typically used with lists of numbers
Description: Returns a new sorted list from the items in a sequence.
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
print(sorted(my_list)) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
my_string = "Hello, World!"
print(sorted(my_string)) # Output: [' ', '!', ',', 'H', 'W', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r']
Description: Returns a reverse iterator over the items in a sequence.
my_list = [1, 2, 3, 4, 5]
print(list(reversed(my_list))) # Output: [5, 4, 3, 2, 1]
my_string = "Hello, World!"
print(''.join(reversed(my_string))) # Output: "!dlroW ,olleH"
Description: Returns an enumerate object which yields pairs of index and value.
my_list = ['a', 'b', 'c', 'd']
for index, value in enumerate(my_list):
print(index, value)
# Output:
# 0 a
# 1 b
# 2 c
# 3 d
Description: Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences.
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for item in zip(list1, list2):
print(item)
# Output:
# (1, 'a')
# (2, 'b')
# (3, 'c')
all()
Description: Returns True if all items in the sequence are true (or if the sequence is empty)
my_list = [1, 2, 3, 4, 5]
print(all(my_list)) # Output: True
my_list = [1, 0, 3, 4, 5]
print(all(my_list)) # Output: False
Description: Returns True if any item in the sequence is true. If the sequence is empty, returns False.
my_list = [0, 0, 0, 1]
print(any(my_list)) # Output: True
my_list = [0, 0, 0, 0]
print(any(my_list)) # Output: False
Description: Applies a function to all items in the sequence and returns an iterator.
def square(x):
return x * x
my_list = [1, 2, 3, 4, 5]
print(list(map(square, my_list))) # Output: [1, 4, 9, 16, 25]
Description: Constructs an iterator from items of the sequence for which the function returns true.
def is_even(x):
return x % 2 == 0
my_list = [1, 2, 3, 4, 5, 6]
print(list(filter(is_even, my_list))) # Output: [2, 4, 6]
These functions provide a powerful toolkit for working with sequences in Python. They help in performing a variety of operations like aggregation, transformation, sorting, and filtering, making it easier to manipulate and analyze data.
Summary
- Strings: Immutable sequences of characters.
- Lists: Ordered, mutable sequences of items.
- Tuples: Ordered, immutable sequences of items.
These basic sequence types and their operations provide a foundation for working with ordered collections of data in Python, enabling efficient and effective data manipulation and access.
Comments
Post a Comment