All Courses

Sequences and Collections in Python

Pallavi Dhotre

3 years ago

Sequences and Collections in Python | insideAIML
Table of Content
Introduction
Sequence data type in Python
  • Sequence Types in Python
  • Lists 
  • Tuples 
  • Strings 
  • Byte sequence  
  • Byte array  
  • Range object
  • Operations on Sequence Types in Python
  • Concatenation  
  • Replication  
  • Membership  
  • Slice
  • Functions of Sequence Types in Python
  • len() 
  • min(), max()
Collections Module in python
  • Sets
  • Dictionaries
Summary

Introduction  

          Previously we  have l earned  the data  structures  of  python. In this  article, we  are going to discuss sequence data type in python and collections module in python in depth. But before that, we should know the difference between sequence and collections. Ordered data types in python like lists, tuples strings are called sequences because we can access them sequentially. Unordered data types such as dictionaries and sets are known as collections. 

Sequence Data Type in Python

          Sequence data type in python is a collection of ordered elements. The order in which we insert the data items into the data structure, in the same order we can access them that’s why it is called sequence in python. Following are some sequence types in python let’s take a deeper look at them.

Sequence Types in Python

          There  are 6  sequence  types in  python –  lists,  tuples,  strings,  byte array,  byte sequence,  and range object. 

Lists 

          In python lists, data items are stored in an ordered manner. The sequence in which they get stored we can access them in the same sequence. Python lists are mutable which means we can modify the elements in the list. 
Program
# Sequence types in python - lists
demo_list=[4,5,6] #creating list
print(demo_list[0]) #accessing elements
print(demo_list[1])
Output
4

5

Tuples 

           In python tuples, data items are stored in an ordered manner. The sequence in which they get stored we can access them in the same sequence. Python tuples are immutable which means we cannot modify the elements in the tuple. 
Program
# Sequence types in python - tuple
demo_tuple=(4,5,6) #creating tuple
print(demo_tuple[0]) #accessing elements
print(demo_tuple[1])
Output
4

5

Strings

          A collection of characters inside the double quotes or single quotes is identified as a string. We can access a single character from the string in the same way as we do in the lists.
Program
# Sequence types in python - string
demo_string="Welcome" #creating string
print("String is ",demo_string)
print(demo_string[0]) #accessing characters of string by their location
print(demo_string[1])
Output
String is  Welcome
W
e

Byte Sequence

          To return the immutable sequence of bytes the bytes() function is used.  
Program 
# Sequence types in python - byte sequence
demo_size= 5
demo =bytes(demo_size) #creating byte sequence
print(demo)
print(bytes([4,3,5,2,1])) #converting iterables into bytes
Output 
b'\x00\x00\x00\x00\x00'
b'\x04\x03\x05\x02\x01'

Byte array 

          Since byte sequences are immutable, the bytearray() function is used for mutable types. 
Program 
# sequence in python example - a byte array
print(bytearray(4))
print(bytearray([4,3,5,2,1])) #converting iterables into bytes
Output
bytearray(b'\x00\x00\x00\x00')

bytearray(b'\x04\x03\x05\x02\x01')

Range object 

          The range() function along with the start and stop range is used to return the elements in the given range. 
Program
# sequence in python example - range object
for i in range(1,5):
    print(i)
Output
1

2

3

4

Operations on Sequence Types in Python

Concatenation 

          We can combine two sequences using the ‘+’ operator
Program 
# Sequence types in python - concatenation
a='India'
b=' is best'
print(a+b)
Output 
India is best

Replication

          We can print the sequence multiple times using the ‘*’ operator. 
Program 
# Sequence types in python - Replication
a='hello'
b=' John'
print((a*2)+b)
Output
hellohello John

Membership 

          We can check if a particular substring is a part of the main string or not using the ‘in’ keyword. 
Program
# Sequence types in python - membership
demo_string="Welcome to this new blog"
print('this' in demo_string)
Output
True

Slice

          We can display specific parts of the sequence using index slicing in python. 
Program
# Sequence types in python - slice
demo_string="Welcome to this new blog"
print(demo_string[3:7])
Output
come

Functions of Sequence Types in Python

          Following are some functions used with the sequences in python.

len()

          This will return a length of the sequence
Program 
# Sequence types in python - functions
demo_string="Welcome to this new blog"
print(len(demo_string))
Output
24

min(), max()

          The min() and max() function will return the smallest and largest value respectively. 
Program
# Sequence types in python - functions
demo_string="Welcome"
print(min(demo_string))
print(max(demo_string))
Output
W

o

Collections Module in Python 

          Unordered data structures like sets and dictionaries are called collections in python. 

Sets 

          Sets are the unordered collection of data items. Data items are immutable. We cannot access the data items in the similar order we stored them. 
Program 
# Collections module in python - sets
demo_set={2,3,4}
print(demo_set)
Output
{2, 3, 4}

Dictionaries 

          Dictionaries are the  unordered  collection of data  items. Data  items are  stored in key-value pairs. Data items are mutable in nature. We can access the data items using keys. 
Program 
# Collections module in python - dictionaries
demo_dict={2:'hello',4:'hi',6:'welcome'}
print(demo_dict)
print(demo_dict[2]) #accessing element having key 2
Output
{2: 'hello', 4: 'hi', 6: 'welcome'}
hello

Summary

          In this article, we discussed  sequence  data type in  python  and  collections module in python and python sequence types. Sequence data type in python is a collection of ordered elements. The order in which we insert the data items into the data structure, in the same order we can access them that’s why it is called sequence in python. There are 6 sequence types in python – lists, tuples, strings, byte array, byte sequence, and range object. Unordered data structures like sets and dictionaries are called collections in python. We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below. 
     
Liked what you read? Then don’t break the spree. Visit our blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our insideAIML Youtube Channel to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning
Keep Learning. Keep Growing. 
      

Submit Review