Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

What is a Sequence Data Type in Python



Sequence Data Types are used to store data in containers in the Python programming language. The different types of containers used to store the data are List, Tuple, and String.

Lists are mutable and can hold data of any type, whereas Strings are immutable and can only store data of the str type. Tuples are immutable data types that can store any sort of value.

So let's discuss these data types one by one in the coming section -

List

The sequential data-type class includes the list data type. The list is the only mutable data type in the sequential category. It can store any data type's values or components. Many procedures in the list can be changed and performed, such as append, remove, insert, extend, reverse, etc. We still have many more built-in functions to manipulate lists.

Example

In the example below, we will look at how to create a list and how to access elements of the list using indexing. Here we used normal indexing and negative indexing. Negative indexing shows starting at the end, with -1 being the last item, -2 denoting the second-to-last item, and so on.

List = ["Tutorialspoint", "is", "the", "best", "platform", "to", "learn", "new", "skills"]
print(List)
print("Accessing element from the list")
print(List[0])
print(List[3])
print("Accessing element from the list by using negative indexing")
print(List[-2])
print(List[-3])

Output

When you run the program, it will show this output:

['Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills']
Accessing element from the list
Tutorialspoint
best
Accessing element from the list by using negative indexing
new
learn

Strings

The string values are stored using string data types. We can not modify the elements in a string because it is immutable. Strings have a lot of built-in functions, and we can use them to do a lot of things. The following are some of the built-in string functions: count, isupper, islower, split, join, etc.

In Python, single quotes, double quotes, and even triple quotes can be used to create strings. Generally, we use a triple quote to create a multiple-line string.

Example

In the example below, we will look at how to create a string and how to access characters of the string using indexing. Strings also support negative indexing.

String = "Tutorialspoint is the best platform to learn new skills"
print(String)
print(type(String))
print("Accessing characters of a string:")
print(String[6])
print(String[10])
print("Accessing characters of a string by using negative indexing")
print(String[-6])
print(String[-21])

Output

After running the program, you will get this result:

Tutorialspoint is the best platform to learn new skills
<class 'str'>
Accessing characters of a string:
a
o
Accessing characters of a string by using negative indexing
s
m

Tuple

Tuples are a data type that belongs to the sequence data type category. They are similar to lists in Python, but they have the property of being immutable. We can not change the elements of a tuple, but we can execute a variety of actions on them, such as count, index, type, etc.

Tuples are created in Python by placing a sequence of values separated by a 'comma', with or without the use of parentheses for data grouping. Tuples can have any number of elements and any type of data (like strings, integers, lists, etc.).

Example

In the below example we will look at how to create a tuple and how to access elements of the tuple using indexing. Tuples also support negative indexing.

tuple = ('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills')
print(tuple)
print("Accessing elements of the tuple:")
print(tuple[5])
print(tuple[2])
print("Accessing elements of the tuple by negative indexing: ")
print(tuple[-6])
print(tuple[-1])

Output

This output will be displayed when the program runs-

('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills')
Accessing elements of the tuple:
to
the
Accessing elements of the tuple by negative indexing: 
best
skills

Example: Using List, String and Tuple Together

In this example, we will create a list, a string, and a tuple. We will then print them and access specific elements from each data type.

# Using List, String, and Tuple together
# String: a sentence
my_string = "Learning Python is fun"

# List: a collection of different types of data
my_list = [25, "Hello", 2.5, True]

# Tuple: a fixed collection of values
my_tuple = ("Apple", "Banana", "Cherry")

# Print all three
print("String:", my_string)
print("List:", my_list)
print("Tuple:", my_tuple)

# Accessing elements
print("\nAccessing elements:")
print("First word in string:", my_string[0])
print("Second item in list:", my_list[1]) 
print("Last fruit in tuple:", my_tuple[-1])

Output

You will see this result after executing the program:

String: Learning Python is fun
List: [25, 'Hello', 2.5, True]
Tuple: ('Apple', 'Banana', 'Cherry')
Accessing elements:
First word in string: L
Second item in list: Hello
Last fruit in tuple: Cherry
Updated on: 2025-05-16T18:29:47+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements