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)

Python sequence

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:

  1. 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
    
  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]
    
  3. 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]
    
  4. 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]
    
  5. 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
    
  1. List, Tuple, and String in Python:

    • List: Ordered, mutable, and can contain elements of different types.
    • Tuple: Ordered, immutable, and typically contains elements of different types.
    • String: Ordered sequence of characters, immutable.
    # Examples
    my_list = [1, 2, 3]
    my_tuple = (4, 5, 6)
    my_string = "Hello"
    
  2. Common Sequence Operations in Python:

    • Operations like len(), max(), min(), and membership tests (in and not in) are common across sequences.
    # Example
    my_list = [1, 2, 3]
    length = len(my_list)
    
  3. Indexing and Slicing in Python Sequences:

    • Indexing starts at 0, and negative indices count from the end.
    • Slicing allows extracting sub-sequences.
    # Example
    my_string = "Python"
    first_char = my_string[0]
    sub_str = my_string[1:4]  # Results in "yth"
    
  4. Mutable and Immutable Sequences in Python:

    • Lists are mutable; you can change their elements.
    • Tuples and strings are immutable; you cannot change their elements once defined.
    # Example
    my_list = [1, 2, 3]
    my_list[0] = 10  # Valid for lists, not for tuples or strings
    
  5. Iterating Through Python Sequences:

    • Use loops (e.g., for) to iterate through sequences.
    # Example
    my_tuple = (4, 5, 6)
    for item in my_tuple:
        print(item)
    
  6. Concatenating Sequences in Python:

    • Use the + operator to concatenate sequences.
    # Example
    list1 = [1, 2, 3]
    list2 = [4, 5, 6]
    concatenated_list = list1 + list2
    
  7. 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)
    
  8. Working with Sequences in Python Programming:

    • Sequences are fundamental in Python; understanding their properties and operations is crucial for effective programming.
    # Example
    my_string = "Hello"
    if 'e' in my_string:
        print("The letter 'e' is in the string.")