Python Sequences — Types, Operations, and Functions
Sequences are one of the most core topics in any programming language specifically in python because python allows itself to be more readable. Sequences play an important role in writing code that’s easily readable.
So sequences in python are a collection of elements, we will learn all about this and try to understand what can you do with those sequences? Then we will look at what are the operations that we can perform on sequences.
What are Sequences in Python?
A sequence is a generic term used to define a collection of elements in which order is important.
So a sequence is basically just a term that is assigned to refer to a collection of elements in which order is important to order. Meaning if something is added first and then something else is added after it, the order in which these two things were added is preserved.
So if you would be asking from that sequence, give me the first item. So it won’t randomly select any item. It will give you the item that was added right at the beginning.
Now again there are other operations and functions that you can perform on it as well. Which we’ll take a look at but still you need to remember the order is very important.
Types of Python Sequences
There are mainly six types of sequences in python. Some of the types that have mostly been used, and others are not that well used and they have very niche and particular use cases.
Let’s discuss each type of sequence in detail with examples.
Python list
The list is the most versatile sequence type and you can create it using the [] brackets. The list is one of the basic data types in python they are used to store a collection of data.
It maintain the order in which each item that is added is added at a particular index and when they are removed the indexes are re-indexed.
list1 = [3,6,3,84,2,58]
list2 = ['Apple', 'Acer', 'HP', 'Dell']Python tuple
Tuples are basically the same as lists but the difference that they have from lists is that tuples are immutable and you need to use () parenthesis to create them.
You cannot do anything with a tuple after defining it. You cannot modify it and add data to it neither can you remove data from it.
t1 = (14,56,3,9,57)
t2 = ( "love blogging", 4,6.5, 2 )Python range()
The range() object is a built-in python function and they are very often used when we want to loop over certain things.
A range is basically a sequence of numbers that we defined. So we tell python that I want a range of numbers ranging from 0 to 100.
You can also create a list or tuple or any other kind of collection using the range() function object.
x = range(5)
for i in x:
print(i)See Also: Python Range Function — Increment and Decrement
Python string
Strings are just a collection of characters that are added one after the other and you can write in the single-quotes (”) or double-quotes (“”).
Python strings are also immutable, you can only reassign the new string to the variable but can’t make any changes in the string.
blog = "Medium"
print(blog[2])Python bytes()
The bytes() function is the core built-in type for manipulating the binary data. Bytes objects are immutable sequences that are mean you cannot modify them.
Get Your Blog Coach’s stories in your inbox
Join Medium for free to get updates from this writer.
The syntax of bytes is the same as string type but it will return the bytes sequence with a prefix of b
number = 10
b = bytes(number)
print( b )Python Sequence Operations
There are four main operations for any sequence and these operations you can implement on the sequence to check the specific element is exist or not, you can slice the element, you can concatenate the element, etc.
Membership
Membership operation implements when we want to check that the specific element exists or is not in the sequence. For this, we used the (in) and (not in) operators.
'ho' in "Python"
# true'yt' not in "Python"
#false
Concatenation
Concatenation operation means when you want to add two things and make something new.
This is what actually concatenation means is that you have one sequence and you have another sequence and you want to concatenate one sequence after the other sequence.
s1 = [2,5,7,3]
s2 = [6,7,3,9]s3 = s1+s2
print(s3)
Repeat
This Repeat operation we used when we need to print the same sequence of n times. So it will loop through the same sequence’s elements for printing for the n number of times that we will be given.
s1 = [3,5,6,6]*2print(s1)
Slicing
The slicing operator allows us to just slice of a particular element or a particular group of elements.
It will cut off the elements from the sequence based on your given order to start and end. The order always starts from 0, remember this.
print( "I am learning python"[5:10] )
# learnPython Sequence Functions
The python sequence functions are used to perform any action on sequence to modify or get some details.
Like, if you want to count the elements of the sequence, want to get min, max from the sequence, and insert, delete, clear, etc, and more.
append() function
list = [54,6,24,82,34]
list.append(73)len() function
list = [54,6,24,82,34]
print(len(list))insert() function
list = [54,6,24,82,34]
list.insert(3, 77)copy() function
list = [54,6,24,82,34]
list.copy()There are more python functions you can check on the official documentation site or you can check here original post.






