filmov
tv
Sets in Python: How to Create a Set in Python? (and Add Items) - Python Tutorial for Beginners
Показать описание
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ How to Create a Set in Python and Add some Items?
There are several ways to create a new Set. In Python programming language, the simplest way to create a Set is to enclose ALL of the items, also known as elements, inside curly braces {} - and separated by commas.
my_set = {1, 2, 3, 4, 5}
It can have any number of items inside, and they can even be of different Data Types - integer, float, string, boolean, or even another object, which contains other items of different data types, and so forth.
Although Set itself is mutable (changeable), it cannot contain mutable objects. Therefore, only immutable objects like Numbers, Strings, Tuples can be a Set item. But both Lists and Dictionaries are mutable, so they cannot be.
○ Syntax of a Set in Python
Let's have a closer look at its syntax so you can declare your own Set in Python. Here is an example, in which you want to store a list of strings. First, Let's declare an empty Set - with empty curly braces {}. Eventually, you will add all of the strings, so that you can store and then access these values, later on. BUT, please note that Sets DO NOT allow any duplicate members. Consequently, they are automatically removed during its creation.
# Empty set
my_set = {}
# A set of strings
my_set = {'red', 'green', 'blue'}
# A set of mixed datatypes
my_set = {1, 'abc', 1.23, (3+4j), True}
# Set with duplicate items
my_set = {'red', 'green', 'blue', 'red'}
# my_set = {'blue', 'green', 'red'}
---
○ Set constructor in Python: set()
As you have seen, a Set containing zero items is called an empty Set, and you can create one with empty curly braces {}. But, you can also create a Set using a type constructor called set(), to declare a new Set in Python. Or even convert other data types into a Set, like String, Tuples, or any object of immutable type.
# Set of items in an iterable
my_set = set('abc')
# my_set = {'a', 'b', 'c'}
# Set of successive integers
my_set = set(range(0, 4))
# my_set = {0, 1, 2, 3}
# Convert list/tuple into set
my_set = set([1, 2, 3])
my_set = set((1, 2, 3)
# my_set = {1, 2, 3}
---
○ Sets are Mutable: Common Errors
A set is mutable (changeable), but it cannot contain mutable objects. Therefore, immutable objects like Numbers, Strings, Tuples can be a set item, but Lists and Dictionaries are mutable, so they cannot be.
my_set = {1, 'abc', ('a', 'b'), True}
my_set = {[1, 2], {'a':1, 'b':2}}
# Triggers TypeError: unhashable type: 'list'
---
That's great, but once you have created your set, you may also want to add other items, or even update some values inside, right? Then, Let's see how it all works, in the next section.
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
🖥️ How to Create a Set in Python and Add some Items?
There are several ways to create a new Set. In Python programming language, the simplest way to create a Set is to enclose ALL of the items, also known as elements, inside curly braces {} - and separated by commas.
my_set = {1, 2, 3, 4, 5}
It can have any number of items inside, and they can even be of different Data Types - integer, float, string, boolean, or even another object, which contains other items of different data types, and so forth.
Although Set itself is mutable (changeable), it cannot contain mutable objects. Therefore, only immutable objects like Numbers, Strings, Tuples can be a Set item. But both Lists and Dictionaries are mutable, so they cannot be.
○ Syntax of a Set in Python
Let's have a closer look at its syntax so you can declare your own Set in Python. Here is an example, in which you want to store a list of strings. First, Let's declare an empty Set - with empty curly braces {}. Eventually, you will add all of the strings, so that you can store and then access these values, later on. BUT, please note that Sets DO NOT allow any duplicate members. Consequently, they are automatically removed during its creation.
# Empty set
my_set = {}
# A set of strings
my_set = {'red', 'green', 'blue'}
# A set of mixed datatypes
my_set = {1, 'abc', 1.23, (3+4j), True}
# Set with duplicate items
my_set = {'red', 'green', 'blue', 'red'}
# my_set = {'blue', 'green', 'red'}
---
○ Set constructor in Python: set()
As you have seen, a Set containing zero items is called an empty Set, and you can create one with empty curly braces {}. But, you can also create a Set using a type constructor called set(), to declare a new Set in Python. Or even convert other data types into a Set, like String, Tuples, or any object of immutable type.
# Set of items in an iterable
my_set = set('abc')
# my_set = {'a', 'b', 'c'}
# Set of successive integers
my_set = set(range(0, 4))
# my_set = {0, 1, 2, 3}
# Convert list/tuple into set
my_set = set([1, 2, 3])
my_set = set((1, 2, 3)
# my_set = {1, 2, 3}
---
○ Sets are Mutable: Common Errors
A set is mutable (changeable), but it cannot contain mutable objects. Therefore, immutable objects like Numbers, Strings, Tuples can be a set item, but Lists and Dictionaries are mutable, so they cannot be.
my_set = {1, 'abc', ('a', 'b'), True}
my_set = {[1, 2], {'a':1, 'b':2}}
# Triggers TypeError: unhashable type: 'list'
---
That's great, but once you have created your set, you may also want to add other items, or even update some values inside, right? Then, Let's see how it all works, in the next section.
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
Комментарии