Write a Python program to create a set


The program is creating and printing sets in Python. A set is an unordered collection of unique elements.

  • The first set a is created as an empty set using the set() constructor. The type of the set is confirmed using the type() function, which returns set.
  • The second set b is created as a non-empty set using the set() constructor and passing a set literal as an argument. The set literal contains elements of different data types, including integers, strings, floating-point numbers, and a boolean value. The type of the set is confirmed using the type() function, which returns set.
  • The third set c is created using a set literal without using the set() constructor. This is a more concise and commonly used method of creating sets in Python. The set literal contains elements of different data types, including integers, strings, floating-point numbers, and a boolean value. The type of the set is confirmed using the type() function, which returns set.

In all three cases, the sets are printed to the console to show the contents of the sets and their respective types.

Source Code

print("Create a new set:")
a = set()
print(a)
print(type(a))
 
print("\nCreate a non empty set:")
b = set({23,45,56,"Joes",'T',45.6,True})
print(b)
print(type(b))
 
print("\nUsing a literal:")
c = {23,45,56,"Joes",'T',45.6,True}
print(c)
print(type(c))

Output

Create a new set:
set()
<class 'set'>

Create a non empty set:
{'T', True, 45, 23, 56, 'Joes', 45.6}
<class 'set'>

Using a literal:
{'T', True, 45, 23, 56, 'Joes', 45.6}
< class 'set'>