본문 바로가기
Study Note/Python

6. Set & tuples and list

by jhleeatl 2024. 3. 27.

Set

 

In Python, a set is a data type representing a collection of unique elements. Sets do not allow duplicate elements and are unordered. This makes sets useful for operations such as set union, intersection, and difference.

 

 

Sets are defined using curly braces {}, with elements separated by commas. For example:

 

my_set = {1, 2, 3, 4, 5}

 

 

You can create a set from a list or tuple, removing any duplicate elements automatically

my_list = [1, 2, 3, 3, 4, 5, 5]
my_set = set(my_list)
print(my_set)  # Output: {1, 2, 3, 4, 5}

 

 

Since sets are mutable, you can add or remove elements. To add elements, you use the add() method, and to remove elements, you use the remove() method:

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}

my_set.remove(2)
print(my_set)  # Output: {1, 3, 4}

 

 

 

 

 These operators are used for set operations and they operate similar to their mathematical counterparts.



1. `&` (Intersection):**


   - The `&` operator is used to find the intersection of two sets. It returns a new set containing only the elements that are common to both sets.


   - Example:

    set1 = {1, 2, 3, 4}
    set2 = {3, 4, 5, 6}

    intersection_set = set1 & set2
    print(intersection_set)  # Output: {3, 4}



2. `|` (Union):


   - The `|` operator is used to find the union of two sets. It returns a new set containing all the unique elements from both sets.


   - Example:

    set1 = {1, 2, 3, 4}
    set2 = {3, 4, 5, 6}

    union_set = set1 | set2
    print(union_set)  # Output: {1, 2, 3, 4, 5, 6}



3. `-` (Difference):


   - The `-` operator is used to find the difference between two sets. It returns a new set containing the elements that are present in the first set but not in the second set.


   - Example:

    set1 = {1, 2, 3, 4}
    set2 = {3, 4, 5, 6}

    difference_set = set1 - set2
    print(difference_set)  # Output: {1, 2}


These operators provide convenient ways to perform set operations in Python and are particularly useful when working with collections of unique elements.

 

 

Difference between 'Tuples' and 'List'

 

Sure! Let me explain the differences between tuples and lists with examples in English:

1. mmutability vs. Mutability


   - Tuple: Tuples are immutable, meaning once they are created, their elements cannot be changed. For example:

    my_tuple = (1, 2, 3)
    # Attempting to modify the tuple will raise an error
    # my_tuple[0] = 10  # Raises TypeError



   - List:** Lists are mutable, allowing for elements to be modified, added, or removed. For example:

    my_list = [1, 2, 3]
    # Modifying an element in the list
    my_list[0] = 10



2. Declaration and Initialization:
   - *Tuple:* Tuples are declared and initialized using parentheses `()`.
   - *List:* Lists are declared and initialized using square brackets `[]`.

    my_tuple = (1, 2, 3)
    my_list = [1, 2, 3]




Tuples are ideal for representing fixed collections of elements, while lists are suitable for situations where flexibility and mutability are required.

 

 

 

'Study Note > Python' 카테고리의 다른 글

8. Function simplification  (0) 2024.03.27
7. F-strings & Try Except  (0) 2024.03.27
5. Uses of [] {} ()  (0) 2024.03.26
4. Function [Def]  (0) 2024.03.26
3. IF & loop  (0) 2024.03.25