Which of the following operations operators cannot be applied on sets in python
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Sets in Python Show
Perhaps you recall learning about sets and set theory at some point in your mathematical education. Maybe you even remember Venn diagrams: If this doesn’t ring a bell, don’t worry! This tutorial should still be easily accessible for you. In mathematics, a rigorous definition of a set can be abstract and difficult to grasp. Practically though, a set can be thought of simply as a well-defined collection of distinct objects, typically called elements or members. Grouping objects into a set can be useful in programming as well, and Python provides a built-in set type to do so. Sets are distinguished from other object types by the unique operations that can be performed on them. Here’s what you’ll learn in this tutorial: You’ll see how to define set objects in Python and discover the operations that they support. As with the earlier tutorials on lists and dictionaries, when you are finished with this tutorial, you should have a good feel for when a set is an appropriate choice. You will also learn about frozen sets, which are similar to sets except for one important detail. Defining a SetPython’s built-in
Let’s see what all that means, and how you can work with sets in Python. A set can be created in two ways. First, you can define a set with the built-in In this case, the argument
>>>
Strings are also iterable, so a string can be passed to >>>
You can see
that the resulting sets are unordered: the original order, as specified in the definition, is not necessarily preserved. Additionally, duplicate values are only represented in the set once, as with the string Alternately, a set can be defined with curly braces (
When a set is defined this way, each Thus, the sets shown above can also be defined like this: >>>
To recap:
Observe the difference between these two set definitions: >>>
A set can be empty. However, recall that Python interprets empty curly braces ( >>>
An empty set is falsy in a Boolean context: >>>
You might think the most intuitive sets would contain similar objects—for example, even numbers or surnames: >>>
Python does not require this, though. The elements in a set can be objects of different types: >>>
Don’t forget that set elements must be immutable. For example, a tuple may be included in a set: >>>
But lists and dictionaries are mutable, so they can’t be set elements: >>>
Set Size and MembershipThe >>>
Operating on a SetMany of the operations that can be used for Python’s other composite data types don’t make sense for sets. For example, sets can’t be indexed or sliced. However, Python provides a whole host of operations on set objects that generally mimic the operations that are defined for mathematical sets. Operators vs. MethodsMost, though not quite all, set operations in Python can be performed in two different ways: by operator or by method. Let’s take a look at how these operators and methods work, using set union as an example. Given two sets, Consider these two sets:
The union of In Python, set union can be performed with the >>>
Set union can also be obtained with the >>>
The way they are used in the examples above, the operator and method behave identically. But there is a subtle difference between them. When you use the Observe the difference between these two statements: >>>
Both attempt to compute the union of Available Operators and MethodsBelow is a list of the set operations available in Python. Some are performed by operator, some by method, and some by both. The principle outlined above generally applies: where a set is expected, methods will typically accept any iterable as an argument, but operators require actual sets as operands.
>>>
More than two sets may be specified with either the operator or the method: >>>
The resulting set contains all elements that are present in any of the specified sets.
>>>
You can specify multiple sets with the intersection method and operator, just like you can with set union: >>>
The resulting set contains only elements that are present in all of the specified sets.
>>>
Another way to think of this is that Once again, you can specify more than two sets: >>>
When multiple sets are specified, the operation is performed from left to right. In the example above,
>>>
The >>>
As with the difference operator, when multiple sets are specified, the operation is performed from left to right. Curiously, although the >>>
>>>
If >>>
In set theory, a set
>>>
A set is considered to be a subset of itself: >>>
It
seems strange, perhaps. But it fits the definition—every element of
A proper subset is the same as a subset, except that the sets can’t be identical. A set
>>>
While a set is considered a subset of itself, it is not a proper subset of itself: >>>
A superset is the reverse of a subset. A set
>>>
You have already seen that a set is considered a subset of itself. A set is also considered a superset of itself: >>>
A proper superset is the same as a superset, except that the sets can’t be identical. A set
>>>
A set is not a proper superset of itself: >>>
Modifying a SetAlthough the elements contained in a set must be of immutable type, sets themselves can be modified. Like the operations above, there are a mix of operators and methods that can be used to change the contents of a set. Augmented Assignment Operators and MethodsEach of the union, intersection, difference, and symmetric difference operators listed above has an augmented assignment form that can be used to modify a set. For each, there is a corresponding method as well.
>>>
>>>
>>>
>>>
Other Methods For Modifying SetsAside from the augmented operators above, Python supports several additional methods that modify sets.
>>>
>>>
>>>
>>>
>>>
Frozen SetsPython provides another built-in type called a frozenset, which is in all respects exactly like a set, except that a frozenset is immutable. You can perform non-modifying operations on a frozenset: >>>
But methods that attempt to modify a frozenset fail: >>>
Frozensets are useful in situations where you want to use a set, but you need an immutable object. For example, you can’t define a set whose elements are also sets, because set elements must be immutable: >>>
If you really feel compelled to define a set of sets (hey, it could happen), you can do it if the elements are frozensets, because they are immutable: >>>
Likewise, recall from the previous tutorial on dictionaries that a dictionary key must be immutable. You can’t use the built-in set type as a dictionary key: >>>
If you find yourself needing to use sets as dictionary keys, you can use frozensets: >>>
ConclusionIn this tutorial, you learned how to define set objects in Python, and you became familiar with the functions, operators, and methods that can be used to work with sets. You should now be comfortable with the basic built-in data types that Python provides. Next, you will begin to explore how the code that operates on those objects is organized and structured in a Python program. Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Sets in Python Which operator Cannot be used on sets in Python?Python sets don't have an implementation for the + operator. You can use | for set union and & for set intersection. Sets do implement - as set difference. You can also use ^ for symmetric set difference (i.e., it will return a new set with only the objects that appear in one set but do not appear in both sets).
Which of the following operations Cannot be applied on sets?As sets are unordered, we cannot conduct operations like indexing and slicing on sets.
Which of the following operations allowed Python sets?Python Set Operations and Methods. We can use Python sets to carry out mathematical set operations like union, intersection, difference, and symmetric difference. These operations can be performed using either operators or methods.
What are different set operations in Python?Python set operations (union, intersection, difference and symmetric difference)
|