What are Sequence Data Types In Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

What are sequence data types in python?

The data structures in python are nothing but a meaningful way of arranging, and storing the data in the computer for efficient utilization and processing depending upon the situation. The data structure is one of the most fundamental topics of any programming language. The data structures in python language can be broadly divided into two categories.

We have four built-in (general) data structures in python namely - lists, dictionaries, tuples and sets. Apart from these, we have some user-defined data structures in python like stacks, queues, trees, linked lists, graphs, and hashmaps.

There are mainly six types of sequence data type in Python programming language, they are as follows:

  • strings
  • byte sequences
  • byte arrays
  • lists
  • tuples
  • range objects

To learn more about the sequence data type in Python, refer to the article - Data Types in Python

Six sequence (or sequential) data types

Let us learn about the various sequence data type in python in detail.

Strings

A string is an immutable data type that is used to store the sequence of Unicode characters. In Python, there is no character data type so even a single character is treated as a string having a length of 1.

Strings are one of the most widely used data types of any programming language. A string in Python can be easily created using quotes (either single quotes, double quotes, or triple quotes). The quotes mark the beginning and end of the string

Example: string = "Scaler Topics." or Example: string = 'Scaler Topics.' or Example: string = '''Scaler Topics.'''

Let us take a simple example and learn some of the most widely used methods of strings in Python.

Output:

Byte sequences

The byte sequence is a small sequence of integers. Since the range of a byte is 0 to 255, so the range of the elements of a byte sequence is also 0 to 255. We can also use the bytes() function to create an immutable sequence of small numbers called bytes to object.

The syntax of bytes() method is:

The bytes() function takes 3 optional parameters:

  1. source: it is the source that is used to initialize the bytes array.
  2. encoding: it is the type of encoding in case the source is a string.
  3. errors: if the encoding failed, the action that needs to be performed is specified in this parameter. This will only be needed when the source is a string.

Let us learn how to create byte sequences or a byte object.

Example (using bytes() method):

Output:

Example(without using the bytes() method):

Output:

Byte arrays

The bytearray are mutable objects which support the various mutable sequence operations. We can use the bytearray() method in Python to create Bytearray. The bytearray() method creates a bytearray object having a mutable sequence of integers lying in the range 0 to 256.

The syntax of the bytearray() method is:

Example:

Output:

Lists in Python

The Lists is a built-in data structure in python that stores the data in sequential form. Lists can be treated as dynamic arrays. List data structures in python can contain duplicate elements as each element is identified by a unique number/ position known as an index.

The list in python provides a wide variety of methods and functions. We can put heterogeneous type values inside the lists. The most common associated method of lists are list(), append(), insert(), extend(), sort(), reverse(), remove(), pop(), clear() etc.

Let us take a simple example and learn some of the most widely used methods of a list in Python.

Output:

Tuple in Python

The tuple is also a built-in data structure in python which are somewhat similar to lists data structures in python. A tuple is an immutable object that stores the elements in an ordered manner. We cannot change the elements of the tuple or the tuple itself, once it's assigned whereas we can change the elements of the list as they are mutable.

The tuple in python provides a wide variety of methods and functions. We can put heterogeneous type values inside the tuples. The most common associated method of tuples are tuple(), count(), count(), index() etc.

Let us take a simple example and learn some of the most widely used methods of a tuple in Python.

Output:

Range objects

The range objects are an immutable sequence of numbers that are generally used for iteration or for looping over a specific number of times. We can use the range() function to generate the immutable range objects.

The syntax of range() function is:

The range function takes 3 parameters:

  1. start: It is an optional parameter that marks the beginning of the iteration.
  2. stop: It is also an optional parameter that marks the end of the iteration.
  3. step: The step parameter tells us the number of increments to be made during each iteration.

Example:

Output:

Note: Dictionaries and sets are used to store the non-sequential data.

Learn more

To learn more about the sequence data type in Python, refer to the articles:

Conclusion

  • The sequence data type in Python is used to store (in an organized fashion) the sequential data. The sequence data type in python can be considered as a container that can store different data.
  • A string is an immutable data type that is used to store the sequence of Unicode characters. In Python, there is no character data type so even a single character is treated as a string having a length of 1.
  • The byte sequence is a small sequence of integers. Since the range of a byte is 0 to 255. We can also use the bytes() function to create an immutable sequence of small numbers called bytes to object.
  • The bytearray are mutable objects which support the various mutable sequence operations. The bytearray() method creates a bytearray object having a mutable sequence of integers lying in the range 0 to 256.
  • The lists store the data in sequential form. Lists can be treated as dynamic arrays. List data structures in python can contain duplicate elements as each element is identified by a unique number/ position known as an index.
  • The tuple is similar to lists data structures in python. A tuple is an immutable object that stores the elements in an ordered manner.
  • We cannot change the elements of the tuple or the tuple itself, once it's assigned whereas we can change the elements of the list as they are mutable.
  • The range objects are an immutable sequence of numbers that are generally used for iteration or for looping over a specific number of times. We can use the range() function to generate the immutable range objects.