Middle
What are sequences in Python? What types do you know?
sobes.tech AI
Answer from AI
In Python, sequences are ordered collections of elements that can be accessed by index. They support slicing, concatenation, and iteration.
Main types of sequences:
- Lists (list) — mutable sequences, for example:
[1, 2, 3]. - Tuples (tuple) — immutable sequences, for example:
(1, 2, 3). - Strings (str) — sequences of characters, for example:
'hello'. - Ranges (range) — sequences of numbers, often used in loops.
Example of using a list and a tuple:
my_list = [10, 20, 30]
my_tuple = (10, 20, 30)
print(my_list[1]) # 20
print(my_tuple[1]) # 20
Sequences are convenient for storing and processing ordered data.