What is Set in Python | EP-12 Sets in Python | How to Create Set in Python | Free Python Tutorials

Subscribers:
27,900
Published on ● Video Link: https://www.youtube.com/watch?v=130rZiWgV8U



Duration: 31:34
244 views
5


"""Python Set is an unordered collection of data types that is iterable,mutable
(a data type is mutable if its values can be changed, updated, or modified after
the data type has been created), and has no duplicate elements.Sets are written
with curly brackets.There two ways to create sets
1. using {}
2. using set constructor"""

#CreateaSet:
#1.using {}

set1 = {1,1,6.8,"a"}
print(set1)

#2.using set constructor"""

Creating an empty Set
empty_set = set()
print(empty_set)
print(type(empty_set))

Creating a Set with the use of a String
company ="cybrosys"
print(set(company))

Creating a Set with the use of a List
set2 = set([1,2,3,"a"])
print(set2)

length of set
print(len(set2))


Adding Elements to a Set in Python
"""
1. Using add() Method
2. Using update() Method
"""

#1.Using add() Method
"""Elements can be added to the Sets in Python by using the built-in add()
function. Only one element at a time can be added to the set by using add()
method, loops are used to add multiple elements at a time with the use of add()
method.

Note: Lists cannot be added to a set as elements because Lists are not hashable
whereas Tuples can be added because tuples are immutable and hence Hashable.
syntax:object.add()
"""
Creating a Set
#add elements and other data types
set3 = set()
set3.add(1)
print(set3)

set4 = set()
set4.add((1,2,3))
print(set4)

Using update() Method
"""For the addition of two or more elements, Update() method is used.
The update() method accepts lists, strings, tuples as well as other Python hash
set as its arguments. In all of these cases, duplicate elements are avoided.
syntax:object.update()"""

set5 = set()
set5.update([1,2,3])
print(set5)

Accessing a Set in Python
"""Set items cannot be accessed by referring to an index, since sets are
unordered the items has no index. But you can loop through the Python hash set
items using a for loop, or ask if a specified value is present in a set,
by using the in keyword.
"""

Creating a set
set6 = set((1,2,3))
print(set6)


Checking the element
using in keyword
set7 = {"cybrosys", "technologies"}
print("c" in set7)


Removing Elements from the Set in Python
"""

1. Using remove() Method or discard() Method
2. Using pop() Method
3. Using clear() Method"""

1. Using remove() Method or discard() Method
"""
Elements can be removed from the Sets in Python by using the built-in remove()
function but a KeyError arises if the element doesn’t exist in the hashset
Python. To remove elements from a set without KeyError, use discard(),
if the element doesn’t exist in the set, it remains unchanged."""

Removing elements from Set using Remove() method
"""syntax:object.remove()"""
set8 = {"cybrosys", "technologies"}
set8.remove(("cybrosys"))
print(set8)




Removing elements from Set using Discard() method
"""syntax:object.discard()"""
set8.discard("c")
print(set8)


2. Using pop() Method
"""Pop() function can also be used to remove and return an element from the
set, but it removes only the first element of the set.

Note: If the set is unordered then there’s no such way to determine which
element is popped by using the pop() function.
syntax:object.pop()"""
set9 = {1,2,3,6,9}
set9.pop()
print(set9)


3. Using clear() Method
"""
To remove all the elements from the set, clear() function is used.
syntax:object.clear()"""
set9.clear()
print(set9)


#copy() method
set10 = {1,2,3}
set11 = set10.copy()
print(set10, "set10")
print(set11, "set11")

Frozen Sets in Python
"""
Frozenset is similar to set in Python, except that frozensets are immutable,
which implies that once generated, elements from the frozenset cannot be added
or removed.
"""
Fset =frozenset()
Fset.add(1)
print(Fset)

num = 1,2,3
fset = frozenset(num)
print(fset)


#Python #PythonSets #LearnPython #PythonTutorials #Coding #Programming #FreeTutorials #PythonBeginners #DataScience #TechEducation #PythonDevelopment #LearnToCode #SetOperations #DataStructures #PythonForBeginners #PythonBasics #CodingForEveryone #MachineLearning #WebDevelopment

Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter:   / cybrosys  
➡️ LinkedIn:   / cybrosys  
➡️ Facebook:   / cybrosystechnologies  
➡️ Instagram:   / cybrosystech  
➡️ Pinterest:   / cybrosys  




Other Videos By Cybrosys Technologies


2024-07-17How to Manage Manufacturing Backorders in Odoo 17 | Odoo 17 Manufacturing Tutorials
2024-07-16How to Configure Re Sequence in Odoo 17 Accounting App | Odoo 17 Accounting | Functional Tutorials
2024-07-16How to Handle Missing Keys in Python Dictionaries | Ep-15 Handling Missing Keys in Dictionaries
2024-07-15How to Create a Ticket From Forum Post in Odoo 18 | New & Latest Features in Odoo 18 | Odoo 18 Forum
2024-07-15How to Create Editable Tree View in Odoo 17 | Odoo 17 Development Tutorials | Tree View in Odoo 17
2024-07-15How to Get First & Last Elements From a Tuple in Python | EP-14 Get First & Last Elements from Tuple
2024-07-14How to Create Invoice Upon Email in Odoo 17 Accounting App | Odoo 17 Functional Tutorials
2024-07-14How to Find & Print the Size of a Tuple in Python | EP-13 Find the Size of a Tuple | What is Tuple?
2024-07-11How to Create a Product in Odoo 18 POS | Odoo 18 Latest Features | Odoo 18 New & Upcoming Features
2024-07-11How to Sell Your Products in Odoo 17 Website App | Odoo 17 Website Tutorials | Functional Tutorials
2024-07-10What is Set in Python | EP-12 Sets in Python | How to Create Set in Python | Free Python Tutorials
2024-07-09How to Create an App Using Odoo 17 Studio | Customize App Using Odoo 17 Studio | Odoo 17 Tutorials
2024-07-08How to Add Language Translation in Odoo 17 | Internationalization | Adding Translation in Odoo 17
2024-07-08What is Tuple Operations in Python | EP-11 Tuple Operations in Python | Basic Tuple Operations
2024-07-08Latest Features in Odoo 18 | Upcoming Features in Odoo 18 | New Features in Odoo 18| EP-1 Tech Tonic
2024-07-07What is Tuple in Python | EP-10 Tuple in Python | How to Create Tuple in Python | Built-in Functions
2024-07-07How to Configure Sales Team & Team Members in Odoo 17 Sales | Odoo 17 Functional Tutorials
2024-06-13How to Unbuild Order in Odoo 17 Manufacturing App | Odoo 17 Manufacturing Tutorials | Odoo 17 Videos
2024-06-12How to Manage Employee Assets in Horilla HRMS | Free HR Software | An Overview of Assets Management
2024-06-12How to Configure Access Rights of Manufacturing Orders in Odoo 17 Manufacturing App | Odoo 17 Videos
2024-06-11How to Manage Work Order Dependencies in Odoo 17 Manufacturing App | Odoo 17 Functional Tutorials



Tags:
Python
Python Sets
Python Tutorials
Learn Python
Free Python Tutorials
Coding
Programming
Python for Beginners
Python Data Structures
Set Operations
Python Coding
Python Programming
Tech Education
Python Course
Python Lessons
Python Coding Tutorials
Sets
Python Tutorial
Programming Help
Data Science
Machine Learning
Computer Science
Data Structures
Python Basics
FreeCodeCamp
LearnProgramming
DataScience
MachineLearning
Sets in Python
Set in Python