Hướng dẫn why we use set function in python? - tại sao chúng ta sử dụng hàm set trong python?

❮ Chức năng tích hợp sẵn


Thí dụ

Tạo một bộ chứa tên trái cây:

x = set (('apple', 'chuối', 'anh đào'))))

Hãy tự mình thử »


Định nghĩa và cách sử dụng

Hàm

{1, 2, 3, 4}
{1, 2, 3}
Traceback (most recent call last):
  File "", line 15, in 
    my_set = {1, 2, [3, 4]}
TypeError: unhashable type: 'list'
0 tạo ra một đối tượng đã đặt.

Các mục trong danh sách thiết lập không được đặt hàng, vì vậy nó sẽ xuất hiện theo thứ tự ngẫu nhiên.

Đọc thêm về các bộ trong chương Python.


Cú pháp

Giá trị tham số

Tham sốSự mô tả
Có thể lặp lạiYêu cầu. Trình tự, bộ sưu tập hoặc đối tượng Iterator

❮ Chức năng tích hợp sẵn


Một bộ là một bộ sưu tập không có thứ tự của các mặt hàng. Mỗi yếu tố tập hợp là duy nhất (không có bản sao) và phải là bất biến (không thể thay đổi).

Tuy nhiên, một tập hợp là có thể thay đổi. Chúng ta có thể thêm hoặc loại bỏ các mục khỏi nó.

Các bộ cũng có thể được sử dụng để thực hiện các hoạt động tập toán học như liên minh, giao lộ, sự khác biệt đối xứng, v.v.


Tạo bộ Python

Một tập hợp được tạo bằng cách đặt tất cả các mục (phần tử) bên trong niềng răng xoăn

{1, 2, 3, 4}
{1, 2, 3}
Traceback (most recent call last):
  File "", line 15, in 
    my_set = {1, 2, [3, 4]}
TypeError: unhashable type: 'list'
1, được phân tách bằng dấu phẩy hoặc bằng cách sử dụng hàm
{1, 2, 3, 4}
{1, 2, 3}
Traceback (most recent call last):
  File "", line 15, in 
    my_set = {1, 2, [3, 4]}
TypeError: unhashable type: 'list'
0 tích hợp.

Nó có thể có bất kỳ số lượng mục nào và chúng có thể thuộc các loại khác nhau (số nguyên, float, tuple, chuỗi, v.v.). Nhưng một bộ không thể có các yếu tố có thể thay đổi như danh sách, bộ hoặc từ điển là các yếu tố của nó.

# Different types of sets in Python
# set of integers
my_set = {1, 2, 3}
print(my_set)

# set of mixed datatypes
my_set = {1.0, "Hello", (1, 2, 3)}
print(my_set)

Đầu ra

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}

Lặp lại thông qua một bộ

Chúng ta có thể lặp qua từng mục trong một bộ bằng cách sử dụng vòng lặp

8.

Đầu ra

{1, 2, 3, 4}
{1, 2, 3}
Traceback (most recent call last):
  File "", line 15, in 
    my_set = {1, 2, [3, 4]}
TypeError: unhashable type: 'list'

Lặp lại thông qua một bộ

Chúng ta có thể lặp qua từng mục trong một bộ bằng cách sử dụng vòng lặp


8.

# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))

Đầu ra



Lặp lại thông qua một bộ

Chúng ta có thể lặp qua từng mục trong một bộ bằng cách sử dụng vòng lặp


8.

Các chức năng tích hợp với tập hợp

Các chức năng tích hợp như


9,
# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
0,
# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
1,
# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
2,
# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
3,
# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
4,
# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
5,
# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
6, v.v. thường được sử dụng với các bộ để thực hiện các nhiệm vụ khác nhau.

# initialize my_set
my_set = {1, 3}
print(my_set)

# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)

Đầu ra

{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}

Lặp lại thông qua một bộ

Chúng ta có thể lặp qua từng mục trong một bộ bằng cách sử dụng vòng lặp


8.

Các chức năng tích hợp với tập hợp

Ví dụ sau đây sẽ minh họa điều này.

# Difference between discard() and remove()

# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)

# discard an element
# Output: {1, 3, 5, 6}
my_set.discard(4)
print(my_set)

# remove an element
# Output: {1, 3, 5}
my_set.remove(6)
print(my_set)

# discard an element
# not present in my_set
# Output: {1, 3, 5}
my_set.discard(2)
print(my_set)

# remove an element
# not present in my_set
# you will get an error.
# Output: KeyError

my_set.remove(2)

Đầu ra

{1, 3, 4, 5, 6}
{1, 3, 5, 6}
{1, 3, 5}
{1, 3, 5}
Traceback (most recent call last):
  File "", line 28, in 
KeyError: 2

Tương tự, chúng ta có thể xóa và trả về một mục bằng phương pháp

# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
2.

Vì Set là một kiểu dữ liệu không có thứ tự, không có cách nào để xác định mục nào sẽ được bật ra. Nó hoàn toàn tùy ý.

Chúng tôi cũng có thể xóa tất cả các mục khỏi một bộ bằng phương pháp

# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
3.

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
0

Đầu ra

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
1

Tương tự, chúng ta có thể xóa và trả về một mục bằng phương pháp # Distinguish set and dictionary while creating empty set # initialize a with {} a = {} # check data type of a print(type(a)) # initialize a with set() a = set() # check data type of a print(type(a))2.

Vì Set là một kiểu dữ liệu không có thứ tự, không có cách nào để xác định mục nào sẽ được bật ra. Nó hoàn toàn tùy ý.

Chúng tôi cũng có thể xóa tất cả các mục khỏi một bộ bằng phương pháp

# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
3.

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
2

Python thiết lập các hoạt động

Hướng dẫn why we use set function in python? - tại sao chúng ta sử dụng hàm set trong python?
Các bộ có thể được sử dụng để thực hiện các hoạt động tập toán học như liên minh, giao lộ, sự khác biệt và sự khác biệt đối xứng. Chúng ta có thể làm điều này với các nhà khai thác hoặc phương pháp.

Hãy để chúng tôi xem xét hai bộ sau đây cho các hoạt động sau.

Đặt Liên minh

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
3

Đầu ra

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
4

Đặt liên minh trong Python

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
5

Liên minh A và B là một tập hợp tất cả các yếu tố từ cả hai bộ.

Hướng dẫn why we use set function in python? - tại sao chúng ta sử dụng hàm set trong python?
Liên minh được thực hiện bằng toán tử
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
4. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
5.

Hãy thử các ví dụ sau đây về vỏ Python.

Đặt giao lộ

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
6

Đầu ra

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
7

Đặt liên minh trong Python

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
8

Liên minh A và B là một tập hợp tất cả các yếu tố từ cả hai bộ.

Hướng dẫn why we use set function in python? - tại sao chúng ta sử dụng hàm set trong python?
Liên minh được thực hiện bằng toán tử
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
4. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
5.

Hãy thử các ví dụ sau đây về vỏ Python.

Đặt giao lộ

{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
9

Đầu ra

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
0

Đặt liên minh trong Python

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
1

Liên minh A và B là một tập hợp tất cả các yếu tố từ cả hai bộ.

Hướng dẫn why we use set function in python? - tại sao chúng ta sử dụng hàm set trong python?
Liên minh được thực hiện bằng toán tử
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
4. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
5.

Hãy thử các ví dụ sau đây về vỏ Python.

Đặt giao lộ

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
2

Đầu ra

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
3

Đặt liên minh trong Python

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
4

Liên minh A và B là một tập hợp tất cả các yếu tố từ cả hai bộ.

Liên minh được thực hiện bằng toán tử

# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
4. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
5.

Hãy thử các ví dụ sau đây về vỏ Python.Đặt giao lộ
Đặt giao điểm trong PythonGiao điểm của A và B là một tập hợp các yếu tố phổ biến trong cả hai tập hợp.
Giao lộ được thực hiện bằng toán tử
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
6. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
7.
Đặt sự khác biệt
Đặt sự khác biệt trong PythonSự khác biệt của tập B từ SET A (A - B) là một tập hợp các phần tử chỉ trong A nhưng không phải trong B. Tương tự, B - A là một tập hợp các phần tử trong B nhưng không phải trong A.
Sự khác biệt được thực hiện bằng toán tử
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
8. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
9.
Đặt sự khác biệt đối xứng
Đặt sự khác biệt đối xứng trong PythonSự khác biệt đối xứng của A và B là một tập hợp các phần tử trong A và B nhưng không phải trong cả hai (không bao gồm giao điểm).
Sự khác biệt đối xứng được thực hiện bằng toán tử

0. Tương tự có thể được thực hiện bằng phương pháp

1.
Các phương thức đặt python khác
Có nhiều phương pháp đã đặt, một số trong đó chúng tôi đã sử dụng ở trên. Dưới đây là danh sách tất cả các phương thức có sẵn với các đối tượng đã đặt:Phương pháp
Sự mô tảcộng()
Thêm một phần tử vào tập hợpxa lạ()
Xóa tất cả các phần tử khỏi tập hợpsao chép ()
Trả về một bản sao của bộSự khác biệt()
Trả về sự khác biệt của hai hoặc nhiều bộ như một bộ mớiarce_update ()
Xóa tất cả các phần tử của một bộ khác khỏi tập hợp nàyloại bỏ ()
Xóa một phần tử khỏi tập hợp nếu nó là thành viên. (Không làm gì nếu phần tử không được đặt)ngã tư()
Trả về giao điểm của hai bộ dưới dạng bộ mớiGiao lộ_Update ()
Cập nhật tập hợp với giao điểm của chính nó và mộtisdisjoint ()
Trả về

2 nếu hai bộ có giao điểm null
ISSUBSET ()


Trả về 2 Nếu một bộ khác chứa tập hợp này

phát hành ()

Trả về


2 nếu bộ này chứa một bộ khác

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

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

Đầu ra

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
6

nhạc pop()

Loại bỏ và trả về một phần tử đặt tùy ý. Tăng


5 nếu bộ trống

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
7

gỡ bỏ()

Loại bỏ một phần tử khỏi tập hợp. Nếu yếu tố không phải là thành viên, hãy tăng


5

symmetric_difference ()Đặt giao lộ
Đặt giao điểm trong PythonGiao điểm của A và B là một tập hợp các yếu tố phổ biến trong cả hai tập hợp.
Giao lộ được thực hiện bằng toán tử
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
6. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
7.
Đặt sự khác biệt
Đặt sự khác biệt trong PythonSự khác biệt của tập B từ SET A (A - B) là một tập hợp các phần tử chỉ trong A nhưng không phải trong B. Tương tự, B - A là một tập hợp các phần tử trong B nhưng không phải trong A.
Sự khác biệt được thực hiện bằng toán tử
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
8. Tương tự có thể được thực hiện bằng phương pháp
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
9.
Đặt sự khác biệt đối xứng
Đặt sự khác biệt đối xứng trong PythonSự khác biệt đối xứng của A và B là một tập hợp các phần tử trong A và B nhưng không phải trong cả hai (không bao gồm giao điểm).
Sự khác biệt đối xứng được thực hiện bằng toán tử

0. Tương tự có thể được thực hiện bằng phương pháp

1.
Các phương thức đặt python khác
Có nhiều phương pháp đã đặt, một số trong đó chúng tôi đã sử dụng ở trên. Dưới đây là danh sách tất cả các phương thức có sẵn với các đối tượng đã đặt:Phương pháp
Sự mô tảcộng()


Thêm một phần tử vào tập hợp

xa lạ()

Các bộ có thể thay đổi là không thể không thể vượt qua, vì vậy chúng không thể được sử dụng làm khóa từ điển.Mặt khác, Frozensets có thể băm và có thể được sử dụng làm chìa khóa cho một từ điển.

Frozensets có thể được tạo bằng hàm frozenset ().

Kiểu dữ liệu này hỗ trợ các phương thức như

{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}
0,
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
9,
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
7,
{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}
3,
{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}
4,
{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}
5,

1 và
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a))

# initialize a with set()
a = set()

# check data type of a
print(type(a))
5.Là bất biến, nó không có các phương pháp thêm hoặc loại bỏ các yếu tố.

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
8

Hãy thử các ví dụ này trên vỏ Python.

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}
9