In Python, a sequence is a data structure that represents an ordered list of elements. The elements in a sequence can be of different data types, including strings, numbers, and other objects.
In this tutorial, we will learn how to work with sequences in Python, and more specifically, how to perform common sequence operations such as slicing, concatenation, and iteration.
Step 1: Understand Basic Sequence Types
In Python, there are three basic sequence types: strings, lists, and tuples. Here is a brief overview of each:
- Strings: Strings are a sequence of characters and are immutable, which means they cannot be changed.
- Lists: Lists are a sequence of elements enclosed in square brackets
[ ]
and are mutable, which allows for elements to be changed, added, or removed. - Tuples: Tuples are a sequence of elements enclosed in parentheses
( )
and are immutable like strings.
For example, here are different ways of defining sequences:
1 2 3 |
string_sequence = "Python" list_sequence = [1, 2, 3, 'a', 'b', 'c'] tuple_sequence = (1, 'a', 3.5) |
Step 2: Access Elements in a Sequence
You can access individual elements in a sequence using their index. The index is the position of an element in the sequence, with the first element being at index 0. Here is an example of how to access elements in a sequence:
1 2 3 4 5 6 7 8 |
string_sequence = "Python" print(string_sequence[0]) # Output: P list_sequence = [1, 2, 3, 'a', 'b', 'c'] print(list_sequence[3]) # Output: a tuple_sequence = (1, 'a', 3.5) print(tuple_sequence[1]) # Output: a |
Step 3: Slicing a Sequence
Slicing is a technique for obtaining a part of the sequence by specifying the start, end, and step values using the slice notation seq[start:end:step]
. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 |
string_sequence = "Python" # Slicing - Get a substring from index 1 to 4 (excluding the element at index 4) substring = string_sequence[1:4] print(substring) # Output: yth list_sequence = [1, 2, 3, 4, 5, 6] # Slicing - Get a sublist from index 2 to 5 (excluding the element at index 5) sublist = list_sequence[2:5] print(sublist) # Output: [3, 4, 5] |
Step 4: Concatenating Sequences
To concatenate two sequences, you can use the +
operator. Here is an example of concatenating two sequences:
1 2 3 4 5 6 7 8 9 |
string_sequence1 = "Hello" string_sequence2 = "World" concat_string = string_sequence1 + ' ' + string_sequence2 print(concat_string) # Output: Hello World list_sequence1 = [1, 2, 3] list_sequence2 = ['a', 'b', 'c'] concat_list = list_sequence1 + list_sequence2 print(concat_list) # Output: [1, 2, 3, 'a', 'b', 'c'] |
Step 5: Iterating Over a Sequence
To iterate over a sequence, you can use a for loop. Here is an example of iterating over a sequence:
1 2 3 4 5 6 7 8 9 10 11 |
string_sequence = "Python" # Iterate over the string for char in string_sequence: print(char) list_sequence = [1, 2, 3, 'a', 'b', 'c'] # Iterate over the list for element in list_sequence: print(element) |
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# Defining sequences string_sequence = "Python" list_sequence = [1, 2, 3, 'a', 'b', 'c'] tuple_sequence = (1, 'a', 3.5) # Accessing elements print(string_sequence[0]) print(list_sequence[3]) print(tuple_sequence[1]) # Slicing substring = string_sequence[1:4] print(substring) sublist = list_sequence[2:5] print(sublist) # Concatenating string_sequence1 = "Hello" string_sequence2 = "World" concat_string = string_sequence1 + ' ' + string_sequence2 print(concat_string) list_sequence1 = [1, 2, 3] list_sequence2 = ['a', 'b', 'c'] concat_list = list_sequence1 + list_sequence2 print(concat_list) # Iterating for char in string_sequence: print(char) for element in list_sequence: print(element) |
Conclusion
In this tutorial, we have learned how to work with sequences in Python, including common operations like accessing elements, slicing, concatenating, and iterating over them. Understanding these operations is fundamental for working with data structures in Python, as sequences are a foundation for many complex data structures and algorithms.