Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, a sequence is an ordered collection of elements. The three most common sequence types are strings, lists, and tuples. This tutorial covers the basics of sequences, including their properties and common operations.
1. Strings:
Strings are sequences of characters. They are created using single quotes ('
), double quotes ("
), or triple quotes ('''
or """
).
Example:
string1 = 'Hello, World!' string2 = "Hello, World!"
2. Lists:
Lists are mutable sequences that can hold elements of different data types, including other sequences. They are created using square brackets ([]
).
Example:
my_list = [1, 'Python', 3.14, [4, 5, 6]]
3. Tuples:
Tuples are immutable sequences that can hold elements of different data types, including other sequences. They are created using parentheses (()
).
Example:
my_tuple = (1, 'Python', 3.14, (4, 5, 6))
Common sequence operations:
Accessing elements: Use the index operator []
to access elements in a sequence. Indices start from 0.
string1 = 'Python' print(string1[0]) # Output: P my_list = [1, 2, 3] print(my_list[1]) # Output: 2
Slicing: Use the slice notation [start:stop:step]
to extract a subsequence from the original sequence.
string1 = 'Python' print(string1[1:4]) # Output: yth my_list = [1, 2, 3, 4, 5] print(my_list[1:4]) # Output: [2, 3, 4]
Concatenation: Use the +
operator to concatenate two sequences of the same type.
string1 = 'Hello, ' string2 = 'World!' print(string1 + string2) # Output: Hello, World! list1 = [1, 2, 3] list2 = [4, 5, 6] print(list1 + list2) # Output: [1, 2, 3, 4, 5, 6]
Repetition: Use the *
operator to repeat a sequence.
string1 = 'Python' print(string1 * 3) # Output: PythonPythonPython my_list = [1, 2, 3] print(my_list * 2) # Output: [1, 2, 3, 1, 2, 3]
Finding the length: Use the len()
function to find the length of a sequence.
string1 = 'Python' print(len(string1)) # Output: 6 my_list = [1, 2, 3, 4, 5] print(len(my_list)) # Output: 5
List, Tuple, and String in Python:
# Examples my_list = [1, 2, 3] my_tuple = (4, 5, 6) my_string = "Hello"
Common Sequence Operations in Python:
len()
, max()
, min()
, and membership tests (in
and not in
) are common across sequences.# Example my_list = [1, 2, 3] length = len(my_list)
Indexing and Slicing in Python Sequences:
# Example my_string = "Python" first_char = my_string[0] sub_str = my_string[1:4] # Results in "yth"
Mutable and Immutable Sequences in Python:
# Example my_list = [1, 2, 3] my_list[0] = 10 # Valid for lists, not for tuples or strings
Iterating Through Python Sequences:
for
) to iterate through sequences.# Example my_tuple = (4, 5, 6) for item in my_tuple: print(item)
Concatenating Sequences in Python:
+
operator to concatenate sequences.# Example list1 = [1, 2, 3] list2 = [4, 5, 6] concatenated_list = list1 + list2
Built-in Functions for Python Sequences:
sum()
, sorted()
, enumerate()
, and zip()
are useful for working with sequences.# Example my_list = [3, 1, 4, 1, 5, 9] sorted_list = sorted(my_list)
Working with Sequences in Python Programming:
# Example my_string = "Hello" if 'e' in my_string: print("The letter 'e' is in the string.")