What is a sequence data type in Python?

PythonProgrammingServer Side Programming

Sequence Data Types are used to store data in containers in the Python computer 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.

List

The sequential data-type class includes the list data type. The list is the sole 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, sorted, etc. We still have many more built-in functions to manipulate lists.

Example

In the below example we will look at how to create a list and how to access elements in of the list using indexing. Here we used normal indexing and negative indexing. Negative indexing indicates 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

The above code produces the following results

['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't manipulate the elements in a string because it's 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, spilt, 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 below example 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

The above code produces the following results

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're similar to lists in Python, but they have the property of being immutable. We can't 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 parenthesis 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

The above code produces the following results.

('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
raja
Updated on 05-Sep-2022 08:31:55

Advertisements