Sequential Data Types
Sequential Data Types in Python
Sequential Data Types are used to store multiple values in a specific order, allowing access to elements one by one. These data types maintain the sequence of values and support operations like indexing and slicing.
Frequently Used Sequential Data Types in Python:- str
- range
String (str) Data Types in Python
The str data type in Python is a built-in Sequential Data Type used to store and manipulate text in an ordered sequence. It supports operations like indexing, slicing, and concatenation, making it essential for handling textual data.
- Single-Line String data
- Multi-Line String data
# Syntax-1: Defining a Single-Line String Using Double Quotes
msg = "Welcome to Python programming!"
# Syntax-2: Defining a Single-Line String Using Single Quotes
txt = 'Follow the guidelines carefully.'
# Syntax-3: Defining a Multi-Line String Using Double Quotes
docstring = """This function retrieves user data
and returns a formatted response."""
# Syntax-4: Defining a Multi-Line String Using Single Quotes
message = '''This function processes the input data
and generates a summary report.'''
Operations on String (str) Data in Python
On str data, we can perform two types of operations:
- Indexing:
- It helps access characters in a string, like the first letter of a name.
- The index can be either positive or negative.
- If the index is valid, it returns the corresponding character.
- If the index is invalid, it raises an IndexError.
- Slicing:
- It returns characters from start to end-1 if start is less than end; otherwise, it returns an empty string.
- If the slicing indices are out of range, it does not raise an error; instead, it returns an empty string.
Indexing is the process of retrieving a character from a given `str` object using a valid index.
string = "PYTHON" # String variable
print(string, type(string)) # Output: PYTHON < class 'str' >
print(len(string)) # Output: 6
print(string[0]) # Output: P (First character)
print(string[-1]) # Output: N (Last character)
print(string[30]) # IndexError: string index out of range
Slicing is the process of retrieving a range of characters or a substring from a given `str` object using valid start and end indices.
course = "PYTHON" # String variable
# Syntax-1: string_obj [ begin:end ] -> Returns substring from begin to end-1
print(course[2:5]) # Output: THO (Characters from index 2 to 4)
print(course[-4:-1]) # Output: THO (Negative indexing from -4 to -2)
# Syntax-2: string_obj [ :end ] -> Returns substring from start to end-1
print(course[:-1]) # Output: PYTHO
print(course[:0]) # Output: "" (empty string)
# Syntax-3: string_obj [ :end ] -> Returns substring from begin index to the end
print(course[2:]) # Output: THON
print(course[:]) # Output: PYTHON (complete string)
Range (range) Data Types in Python
The range data type is a built-in class in Python and is treated as a sequential data type. It stores a sequence of integer values with a fixed interval between them. It is mainly used in loops and for creating number sequences.
- The range object is immutable, meaning its values cannot be modified after creation.
- It maintains the insertion order in which numbers are generated.
- The range object supports indexing and slicing to retrieve specific values.
# Syntax-1: range (end) -> Generates numbers from 0 to end-1
numbers = range(10)
print(list(numbers), type(numbers)) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] < class 'range' >
# Syntax-2: range(begin, end) -> Generates numbers from begin to end-1
num_range = range(50, 56)
print(list(num_range)) # Output: [50, 51, 52, 53, 54, 55]
# Syntax-3: range(begin, end, step) -> Generates numbers with a step from begin to end-1
sequence = range(100, 150, 10)
print(list(sequence)) # Output: [100, 110, 120, 130, 140]
# Syntax-4: range(begin, end, -step) -> Generates numbers from begin to end+1 in reverse order
countdown = range(10, 0, -1)
print(list(countdown)) # Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Exercise
- Fill in the Blanks:
- - In Python, the str data type is _______, meaning its values cannot be changed after creation.
- - What will be the output of `print("DEVELOPER"[-5:-1])` ? _______
- - What error is raised when an invalid index is accessed in a string? _______
- - What will be the output of `print(list(range(-5, 6, 1)))`? _______
- - What will be the output of `print(list(range(50, 56)[::-1])) `? _______