All categories

    Tuples & Sets

    Immutable sequences and unique collections.

    01Tuple basics

    main.py
    point = (3, 4)
    x, y = point
    print(x, y)
    print(point[0])
    Output
    3 4
    3

    02Set operations

    main.py
    a = {1, 2, 3, 4}
    b = {3, 4, 5, 6}
    print(a | b)
    print(a & b)
    print(a - b)
    Output
    {1, 2, 3, 4, 5, 6}
    {3, 4}
    {1, 2}

    03Remove duplicates with a set

    main.py
    nums = [1, 2, 2, 3, 3, 3, 4]
    print(sorted(set(nums)))
    Output
    [1, 2, 3, 4]

    04Swap with tuple unpacking

    main.py
    a, b = 1, 2
    a, b = b, a
    print(a, b)
    Output
    2 1