Python program to find cartesian product of two sets.

Cartesian product of two sets, Let’s consider we have setA = {3, 5} and setB = {6, 7} then the Cartesian product of two sets will be {(3, 6), (3, 7), (5, 6), (5, 7)}

Cartesian product of two sets with Python

Steps to solve the program

1. Initiate two sets setA and setB.
2. Initiate a result set where we will add the combined value of setA and setB.
3. Apply a nested loop, the first loop will pick the value of setA and the second loop will value of setB.
4. Combine setA and setB values in the tuple and add them to the result set. 
5. Print result set.

				
					# initiate two sets setA and setB
setA = {1, 3}
setB = {2, 6, 7}
result = set()
# use nested loop to interate over setA and setB elements
for val1 in setA:
    for val2 in setB:
        # add combination setA value and setB value to result.
        result.add((val1, val2))
# print output
print(result)

				
			

Output :  Cartesian product of two sets values.

				
					{(1, 2), (3, 7), (1, 7), (3, 6), (1, 6), (3, 2)}
				
			

Related Articles

create two sets of books and find the intersection of sets.

51. Problem to find the longest word in a set

In this Python set program, we will find the longest word in a set from a set of words 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.

Longest word in a set:

Steps to solve the program

1. Take a set of words.
2. Create two variables to store the word having maximum length and longest word in a set.
3. Assign their values equal to 0.
4. Use a for loop to iterate over words from the set.
5. Use an if statement with the len() function to check whether the length of the word is greater than the max_len variable.
6. If yes then assign the length of that word to max_len variable and word to max_word variable.
7. Print both the variables after loop is finished to see the result.

				
					Set = {"I","am","Learning","Python"}
max_len = 0
max_word = 0
print("Original Set: ",Set)
for word in Set:
    if len(word)>max_len:
        max_len =len(word)
        max_word = word
print("Word having maximum length: ",max_word)
print("Length of the word: ",max_len)
				
			

Output :

				
					Original Set:  {'Learning', 'am', 'Python', 'I'}
Word having maximum length:  Learning
Length of the word:  8
				
			

Related Articles

create two sets of books and find the intersection of sets.

50. Problem to find intersection of sets

In this Python set program, we will find the intersection of sets i.e. common books between two 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 sets:

Steps to solve the program

1. Create two sets having books name in it.
2. Use a for loop to iterate over books in the first set.
3. Use an if statement to check whether the book is present in the second set i.e. intersection of sets.
4. If yes then print that book.

				
					Set1 = {"Lord of the Rings","Harry Potter and the Sorcerer's Stone","The Magic Tree","Tower To The Stars"}
Set2 = {"Wizards of Ice","Call of the Forest","Lord of the Rings"}
print("Original Set1: ",Set1)
print("Original Set2: ",Set2)
print("Common books: ")
for book_name in Set1:
    if book_name in Set2:
        print(book_name)
				
			

Output :

				
					Original Set1:  {'Lord of the Rings', 'Tower To The Stars', "Harry Potter and the Sorcerer's Stone", 'The Magic Tree'}
Original Set2:  {'Lord of the Rings', 'Call of the Forest', 'Wizards of Ice'}
Common books: 
Lord of the Rings
				
			

Related Articles

create a set of your favorite movies.

find the longest word in a set.

49. Problem to create a set of favorite movies

In this Python set program, we will create a set of favorite movies 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 movies:

Steps to solve the program

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

				
					Set = {"Avengers: Endgame","John wick","The Matrix"}
print("Set of movies: ",Set)
				
			

Output :

				
					Set of movies:  {'John wick', 'The Matrix', 'Avengers: Endgame'}
				
			

Related Articles

create a set of your favorite actors.

create two sets of books and find the intersection of sets.

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.