48. Problem to create a set of favorite actors

In this Python set program, we will create a set of favorite actors with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Set of favorite actors:

Steps to solve the program

1. Create a set using {}.
2. Add name of your favorite actors in the set.
3. Print the set to see the output.

				
					Set = {"Shan Rukh Khan","Akshay Kumar","Tom Hardy","Robert Pattinson"}
print("Set of actors: ",Set)
				
			

Output :

				
					Set of actors:  {'Shan Rukh Khan', 'Akshay Kumar', 'Robert Pattinson', 'Tom Hardy'}
				
			

Related Articles

create a set of odd numbers from 1 to 20.

create a set of your favorite movies.

47. Problem to create a set of odd numbers

In this Python set program, we will create a set of odd numbers from 1 to 20 with the help of the below-given steps.

Odd number:
Odd numbers are those numbers that cannot be divided into two equal parts.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Set of odd numbers:

Steps to solve the program

1. Create a set using the set() function.
2. Use a for loop to iterate over numbers from 1 to 20.
3. Use an if statement to check whether the number is a odd number or not.
4. If yes then add it to the set using the add() function.
5. Print the set to see the output.

				
					Set = set()
for num in range(1,21):
    if num%2 != 0:
        Set.add(num)
print("Set of odd number: ",Set)
				
			

Output :

				
					Set of odd number:  {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
				
			

Related Articles

create a set of even numbers from 1 to 20.

create a set of your favorite actors.

46. Problem to create a set of even numbers

In this Python set program, we will create a set of even numbers from 1 to 20 with the help of the below-given steps.

Even number:
even numbers are those numbers that can be divided into two equal parts.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Set of even numbers:

Steps to solve the program

1. Create a set using the set() function.
2. Use a for loop to iterate over numbers from 1 to 20.
3. Use an if statement to check whether the number is a even number or not.
4. If yes then add it to the set using the add() function.
5. Print the set to see the output.

				
					Set = set()
for num in range(1,21):
    if num%2 == 0:
        Set.add(num)
print("Set of even number: ",Set)
				
			

Output :

				
					Set of even number:  {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
				
			

Related Articles

convert a set to a dictionary with each element as key and value to an empty set.

create a set of odd numbers from 1 to 20.

45. Problem to convert set to dictionary with the help of Python

In this Python set program, we will convert set to dictionary with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Convert set to dictionary:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create an empty dictionary.
4. Use a for loop to iterate over elements in the set.
5. During iteration add the element as key and an empty set ({}) as its value to the dictionary.
6. Print the dictionary.

				
					a = {1,2,4,6}
print("Original set: ",Set)
Dict = {}
for ele in a:
    Dict[ele] = {}
print("Dictionary: ",Dict)
				
			

Output :

				
					Original set:  {1, 2, 4, 6}
Dictionary:  {1: {}, 2: {}, 4: {}, 6: {}}
				
			

Related Articles

find the index of an element in a set.

create a set of even numbers from 1 to 20.

44. Problem to find the index of an element in a set

In this Python set program, we will find the index of an element in a set with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Index of an element in a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Convert the set to a list using list().
4. Find the index of an element in a set i.e. list using index() function.
5. Print the output.

				
					Set = {1,2,4,6}
print("Set: ",Set)
List = list(Set)
print("The index of 4: ",List.index(4))
				
			

Output :

				
					Set:  {1, 2, 4, 6}
The index of 4:  2
				
			

Related Articles

convert a set to a dictionary

41. Problem to check if an element in a set is a substring of a string

In this Python set program, we will check whether an element in a set is a substring of a string or not with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Element in a set is a substring of a string:

Steps to solve the program

1. Create a string and a set containing words.
2. Create a count variable and assign its value equal to 0.
3. Use a for loop to iterate over words in the set.
4. Use an if statement to check whether the word is in string or not.
5. If yes then add 1 to the count variable.
6. If the value of the count variable is greater than 0 then an element in a set is a substring of a string, else not.
7. Print the respective output.

				
					String = "Learning python is fun"
Set = {"fun","python"}
count = 0
print("String: ",String)
print("Set: ",Set)
for word in Set:
    if word in String:
        count += 1
if count > 0:
    print("An element in a set is a substring of a given string")
else:
    print("An element in a set is not a substring of a given string")
				
			

Output :

				
					String:  Learning python is fun
Set:  {'fun', 'python'}
An element in a set is a substring of a given string
				
			

Related Articles

find the intersection between multiple sets.

40. Problem to find the intersection between multiple sets

In this Python set program, we will find the intersection between multiple sets with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Intersection of two sets: The intersection is common elements between the two sets.

Intersection between multiple sets:

Steps to solve the program

1. Create three sets using {}.
2. Add some elements in the set.
3. Use intersection() function to find the intersection of multiple sets.
4. Print the output.

				
					a = {1,2,4,6}
b = {7,8,9,6}
c = {5,8,9,6}
print("Original set1: ",a)
print("Original set2: ",b)
print("Original set3: ",c)
print("Interssection between above sets: ")
print(a.intersection(b.intersection(c)))
				
			

Output :

				
					Original set1:  {1, 2, 4, 6}
Original set2:  {8, 9, 6, 7}
Original set3:  {8, 9, 5, 6}
Interssection between above sets: 
{6}
				
			

Related Articles

find the difference between multiple sets.

check if any element in a set is a substring of a given string.

39. Problem to find the difference between multiple sets

In this Python set program, we will find the difference between multiple sets with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Difference of two sets: The difference between sets A and B is the set of all elements of A that are not elements of B.

Difference between multiple sets:

Steps to solve the program

1. Create three sets using {}.
2. Add some elements in the set.
3. Use the difference() function to find the difference between multiple sets.
4. Print the output.

				
					a = {1,2,4,6}
b = {7,8,9,6}
c = {5,8,9}
print("Original set1: ",a)
print("Original set2: ",b)
print("Original set3: ",c)
print("Difference between above sets: ")
print(a.difference(b.difference(c)))
				
			

Output :

				
					Original set1:  {1, 2, 4, 6}
Original set2:  {8, 9, 6, 7}
Original set3:  {8, 9, 5}
Difference between above sets: 
{1, 2, 4}
				
			

Related Articles

create a frozen set.

find the intersection between multiple sets.

38. Problem to create a Frozen set in python

In this Python set program, we will create a frozen set in Python with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Frozen set: It is an immutable version of a Python set object. We can not make changes in the set after it is created.

Create a Frozen set in Python:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create a frozen set using the frozenset() function.
4. Print the type of the new set to verify.

				
					a = {1, 2, 4, 5, 7, 8, 9}
print("Original set1: ",a)
b = frozenset(a)
print(b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5, 7, 8, 9}
frozenset({1, 2, 4, 5, 7, 8, 9})

				
			

Related Articles

check if a set is a frozen set.

find the difference between multiple sets.

37. Problem to check if a set is a frozen set

In this Python set program, we will check whether the given set is a frozen set or not with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Frozen set: It is an immutable version of a Python set object. We can not make changes in the set after it is created.

Set is a Frozen set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Print type of the set using type() function to check if a set is a frozen set or not.

				
					a = {1, 2, 4, 5, 7, 8, 9}
print("Original set1: ",a)
print(type(a))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5, 7, 8, 9}
<class 'set'>
				
			

Related Articles

Python program to create a frozen set.

Python program to find the difference between multiple sets.

Python program to find the intersection between multiple sets.

Python program to check if any element in a set is a substring of a given string.

Python program to check if any element in a set is a prefix of a given string.

Python program to check if any element in a set is a suffix of a given string.

 

 

 

check if two sets are equal.

create a frozen set.