Sitemap

Member-only story

Sequence Type and Iterables in Python

2 min readAug 21, 2023

--

Press enter or click to view image in full size
Photo by Bradyn Trollip on Unsplash

Sequence Type in python are strings, lists, tuples, byte sequences, byte arrays and range objects which are indexable and we can iterate through them. All the sequence type are iterables you can get each element one by one.

But all iterables are not sequence type like for example set in which you can iterate through a set but you cannot use indexing.

Lets take an example using sequence type and iterables:

value = [1,2,4,5]
for i in value:
print(i)

print('Access Element via index: '+str(value[0]))
Output:
1
2
4
5
Access Element via index: 1

thisset = {"apple", "mango", "cherry"}

for x in thisset:
print(x)

print('Accessing Element will fail using index:'+str(thisset[0]))
# Output:
TypeError: 'set' object is not subscriptable # [[[Error is thrown basically because in set you cannot use indexing]]]
# Below gets printed because set is iterable
cherry
mango
apple

Most sequence type support ‘in’ and ‘not in’ operator.

Lets take another example:

this_list = ["apple", "mango", "cherry"]

if 'apple' in this_list:
print('Present')


if 'pineapple' not in this_list:
print('True')

# Output:
Present
True

min() and max() are other methods supported by sequence type.

value =…

--

--

Shilpa Sreekumar
Shilpa Sreekumar

Written by Shilpa Sreekumar

Python Developer at TimeClick Software