What is a sequence data type in Python?

Sequence data types in Python are ordered collections that allow you to store and access multiple items using indexing. The three main sequence data types are lists, strings, and tuples. Each has distinct characteristics regarding mutability and the type of data they can store.

Lists are mutable and can hold data of any type, strings are immutable and store only text characters, while tuples are immutable but can store any type of data. Understanding these differences is crucial for choosing the right data structure for your needs.

List

Lists are the most flexible sequence type in Python. They are mutable, meaning you can modify their contents after creation. Lists can store any data type and support various operations like append, remove, insert, and extend.

Example

Here's how to create a list and access its elements using both positive and negative indexing ?

programming_languages = ["Python", "Java", "C++", "JavaScript", "Go", "Ruby"]
print("List:", programming_languages)
print("Accessing elements:")
print("First element:", programming_languages[0])
print("Third element:", programming_languages[2])
print("Last element (negative indexing):", programming_languages[-1])
print("Second last element:", programming_languages[-2])

The output of the above code is ?

List: ['Python', 'Java', 'C++', 'JavaScript', 'Go', 'Ruby']
Accessing elements:
First element: Python
Third element: C++
Last element (negative indexing): Ruby
Second last element: Go

String

Strings store text data and are immutable in Python. Once created, you cannot modify individual characters, but you can create new strings. Strings support many built-in methods like count(), split(), and join().

Example

This example shows string creation and character access using indexing ?

message = "Python Programming"
print("String:", message)
print("Type:", type(message))
print("Accessing characters:")
print("First character:", message[0])
print("Character at index 7:", message[7])
print("Last character (negative):", message[-1])
print("Third last character:", message[-3])

The output of the above code is ?

String: Python Programming
Type: <class 'str'>
Accessing characters:
First character: P
Character at index 7: P
Last character (negative): g
Third last character: i

Tuple

Tuples are immutable sequences that can store any data type. They're created using parentheses or just comma-separated values. While you can't modify tuple elements, you can perform operations like count() and index().

Example

Here's how to create and access tuple elements ?

coordinates = (10, 20, 30, 40, 50)
print("Tuple:", coordinates)
print("Accessing elements:")
print("First coordinate:", coordinates[0])
print("Third coordinate:", coordinates[2])
print("Last coordinate (negative):", coordinates[-1])
print("Second last coordinate:", coordinates[-2])

The output of the above code is ?

Tuple: (10, 20, 30, 40, 50)
Accessing elements:
First coordinate: 10
Third coordinate: 30
Last coordinate (negative): 50
Second last coordinate: 40

Comparison

Data Type Mutable? Data Types Allowed Syntax
List Yes Any [item1, item2]
String No Characters only "text" or 'text'
Tuple No Any (item1, item2)

Example: Working with All Three Types

This example demonstrates creating and using all three sequence types together ?

# Create different sequence types
student_name = "Alice Johnson"
grades = [85, 92, 78, 96]
personal_info = ("Alice", 20, "Computer Science")

# Display all sequences
print("Student name (string):", student_name)
print("Grades (list):", grades)
print("Personal info (tuple):", personal_info)

# Access elements
print("\nAccessing elements:")
print("First letter of name:", student_name[0])
print("Highest grade:", max(grades))
print("Student major:", personal_info[2])

The output of the above code is ?

Student name (string): Alice Johnson
Grades (list): [85, 92, 78, 96]
Personal info (tuple): ('Alice', 20, 'Computer Science')

Accessing elements:
First letter of name: A
Highest grade: 96
Student major: Computer Science

Conclusion

Sequence data types in Python provide ordered collections with indexing support. Use lists for mutable collections, strings for text data, and tuples for immutable collections that need to store mixed data types.

Updated on: 2026-03-24T19:06:06+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements