Junior — Middle
Can you explain what sequences are in Python and how they are used?
sobes.tech AI
Answer from AI
In Python, sequences are ordered collections of elements that can be accessed by index. The main types of sequences are lists (list), tuples (tuple), strings (str), and ranges (range).
Sequences support operations such as:
- Indexing: accessing an element by position, e.g.,
seq[0] - Slicing: obtaining a subsequence, e.g.,
seq[1:4] - Iteration: looping through elements
- Concatenation and repetition
Example of using a list:
fruits = ['apple', 'banana', 'cherry']
print(fruits[1]) # banana
for fruit in fruits:
print(fruit)
Sequences are widely used for storing and processing ordered data.