Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty set literal?

[] = empty list

() = empty tuple

{} = empty dict

Is there a similar notation for an empty set? Or do I have to write set()?

like image 320
Johan Råde Avatar asked May 25 '11 20:05

Johan Råde


People also ask

What is the Python syntax for defining an empty set literal?

To create an empty set in python we have to use the set() function without any arguments, if we will use empty curly braces ” {} ” then we will get an empty dictionary. After writing the above code (create an empty set in python), Ones you will print “type(x)” then the output will appear as a “ <class 'set'> ”.

How do you initialize an empty set?

Creating an empty set is a bit tricky. Empty curly braces {} will make an empty dictionary in Python. To make a set without any elements, we use the set() function without any argument.

How do you define an empty set?

In mathematical sets, the null set, also called the empty set, is the set that does not contain anything. It is symbolized or { }. There is only one null set. This is because there is logically only one way that a set can contain nothing.

How do you check whether a set is empty or not?

Set. isEmpty() method is used to check if a Set is empty or not. It returns True if the Set is empty otherwise it returns False. Return Value: The method returns True if the set is empty else returns False.


2 Answers

No, there's no literal syntax for the empty set. You have to write set().

like image 91
sepp2k Avatar answered Sep 24 '22 15:09

sepp2k


By all means, please use set() to create an empty set.

But, if you want to impress people, tell them that you can create an empty set using literals and * with Python >= 3.5 (see PEP 448) by doing:

>>> s = {*()}  # or {*{}} or {*[]} >>> print(s) set() 

this is basically a more condensed way of doing {_ for _ in ()}, but, don't do this.

like image 36
Dimitris Fasarakis Hilliard Avatar answered Sep 26 '22 15:09

Dimitris Fasarakis Hilliard