Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
135 views
in Python by (114k points)
Python set() Function

Please log in or register to answer this question.

1 Answer

0 votes
by (114k points)

The set() function is a built-in Python function that returns a new set object. A set is an unordered collection of unique elements, where each element must be hashable.

The syntax for using the set() function is as follows:

set(iterable)
 

where iterable is an optional iterable object whose elements will be added to the new set. If iterable is not provided, an empty set is returned.

Here's an example of using the set() function to create a new set:

>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}
 

In this example, we use the set() function to create a new set s with the elements [1, 2, 3]. The elements are added to the set in an arbitrary order, and duplicates are automatically removed.

You can also create a set using curly braces {} syntax, as shown below:

>>> s = {1, 2, 3}
>>> s
{1, 2, 3}
 

In this example, we use curly braces to create a new set s with the elements [1, 2, 3].

You can perform various set operations like union, intersection, difference, and symmetric difference on sets using built-in set methods like union(), intersection(), difference(), and symmetric_difference().

Here's an example of using set operations:

>>> s1 = {1, 2, 3, 4}
>>> s2 = {3, 4, 5, 6}
>>> s3 = s1.union(s2)
>>> s3
{1, 2, 3, 4, 5, 6}
>>> s4 = s1.intersection(s2)
>>> s4
{3, 4}
>>> s5 = s1.difference(s2)
>>> s5
{1, 2}
>>> s6 = s1.symmetric_difference(s2)
>>> s6
{1, 2, 5, 6}
 

In this example, we create two sets s1 and s2 with some common elements. We then perform different set operations like union, intersection, difference, and symmetric difference on these sets using built-in set methods. The resulting sets are stored in new variables s3, s4, s5, and s6.

Related questions

0 votes
1 answer
asked Aug 19, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)
+1 vote
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...