Author name: Vikram Chiluka

Python Set isdisjoint() Method with Examples

Prerequisite:

Python set() Function with Examples

Set isdisjoint() Method in Python:

If none of the items are present in both sets, the isdisjoint() method returns true; otherwise, it returns False.

If two sets have no common elements, they are said to be disjoint.

For Example

p={1,2,3,4}

q={5,6,7}

Here p and q are said to be disjoint since they have no common elements.

Syntax:

set.isdisjoint(set)

Parameters

set: This is Required. The set to look for items that are similar.

Return Value:

This method returns

  • True if the given two sets are disjoint.
  • False if given two sets are not disjoint.

Examples:

Example1:

Input:

Given first set =  {'a', 'b', 'c'}
Given second set = {'p', 'q', 'r'}
Given third set = {'a', 'g'}

Output:

Check if first and second sets are disjoint:  True
Check if first and third sets are disjoint:  False
Check if second and third sets are disjoint:  True

Example2:

Input:

Given first set = {10, 20, 30, 40}
Given second set = {20, 70, 80}
Given third set = {100, 200, 300}

Output:

Check if first and second sets are disjoint:  False
Check if first and third sets are disjoint:  True
Check if second and third sets are disjoint:  True

Set isdisjoint() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Give the third set as static input and store it in another variable.
  • Check if the first and second sets have any common elements using the isdisjoint() method and print it.
  • Check if the first and third sets have any common elements using the isdisjoint() method and print it.
  • Check if the second and third sets have any common elements using the isdisjoint() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {'a', 'b', 'c'}
# Give the second set as static input and store it in another variable.
scnd_set = {'p', 'q', 'r'}
# Give the third set as static input and store it in another variable.
thrd_set = {'a', 'g'}
# Check if the first and second sets have any common elements using the
# isdisjoint() method and print it.
print("Check if first and second sets are disjoint: ",
      fst_set.isdisjoint(scnd_set))
# Check if the first and third sets have any common elements using the
# isdisjoint() method and print it.
print("Check if first and third sets are disjoint: ",
      fst_set.isdisjoint(thrd_set))
# Check if the second and third sets have any common elements using the
# isdisjoint() method and print it.
print("Check if second and third sets are disjoint: ",
      scnd_set.isdisjoint(thrd_set))

Output:

Check if first and second sets are disjoint:  True
Check if first and third sets are disjoint:  False
Check if second and third sets are disjoint:  True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the third set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Check if the first and second sets have any common elements using the isdisjoint() method and print it.
  • Check if the first and third sets have any common elements using the isdisjoint() method and print it.
  • Check if the second and third sets have any common elements using the isdisjoint() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the third set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
thrd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
   
# Check if the first and second sets has any common elements using the
# isdisjoint() method and print it.
print("Check if first and second sets are disjoint: ",
      fst_set.isdisjoint(scnd_set))
# Check if the first and third sets has any common elements using the
# isdisjoint() method and print it.
print("Check if first and third sets are disjoint: ",
      fst_set.isdisjoint(thrd_set))
# Check if the second and third sets has any common elements using the
# isdisjoint() method and print it.
print("Check if second and third sets are disjoint: ",
      scnd_set.isdisjoint(thrd_set))

Output:

Enter some random Set Elements separated by spaces = 10 20 30 40
Enter some random Set Elements separated by spaces = 20 70 80
Enter some random Set Elements separated by spaces = 100 200 300
Check if first and second sets are disjoint: False
Check if first and third sets are disjoint: True
Check if second and third sets are disjoint: True

 

Python Set isdisjoint() Method with Examples Read More »

Python Set update() Method with Examples

Prerequisite:

Python set() Function with Examples

Set update() Method in Python:

The update() method adds items from another set to the current set (or any other iterable).

If an item appears in both sets, only one appearance will appear in the updated set.

Syntax:

set.update(set)

Parameters

set: This is Required. It is iterable to be inserted into the current set.

Return Value:

None is returned by the set update() method (returns nothing).

Examples:

Example1:

Input:

Given first set =  {'a', 'b', 'c'}
Given second set = {'b', 'p', 'q'}

Output:

The given first set after updating is : {'p', 'q', 'b', 'a', 'c'}
The given second set is : {'p', 'q', 'b'}
The result after applying the update() method:  None

Explanation:

Here, add items of the second set to the first set and update it.
But 'b' appears in both sets, only one appearance of 'b' will appear in the 
updated first set.

Example2:

Input:

Given first set = {9, 11, 2, 6}
Given second set = {2, 1, 3, 9}

Output:

The given first set is : {9, 2, 11, 6}
The given second set after updating is : {1, 2, 3, 6, 9, 11}
The result after applying the update() method: None

Set update() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Apply the update() method to the given first and second sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the above result after applying the update() method for the given two sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {'a', 'b', 'c'}
# Give the second set as static input and store it in another variable.
scnd_set = {'b', 'p', 'q'}
# Apply the update() method to the given first and second sets.
# Store it in another variable.
rslt = fst_set.update(scnd_set)
# print the given first set.
print("The given first set after updating is :", fst_set)
# print the given second set.
print("The given second set is :", scnd_set)
# Print the above result after applying the update() method for the
# given two sets.
print("The result after applying the update() method: ", rslt)

Output:

The given first set after updating is : {'p', 'q', 'b', 'a', 'c'}
The given second set is : {'p', 'q', 'b'}
The result after applying the update() method:  None

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Apply the update() method to the given first and second sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the above result after applying the update() method for the given two sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))

# Apply the update() method to the given first and second sets.
# Store it in another variable.
rslt = scnd_set.update(fst_set)
# print the given first set.
print("The given first set is :", fst_set)
# print the given second set.
print("The given second set after updating is :", scnd_set)
# Print the above result after applying the update() method for the
# given two sets.
print("The result after applying the update() method: ", rslt)

Output:

Enter some random Set Elements separated by spaces = 9 11 2 6
Enter some random Set Elements separated by spaces = 2 1 3 9
The given first set is : {9, 2, 11, 6}
The given second set after updating is : {1, 2, 3, 6, 9, 11}
The result after applying the update() method: None

 

 

 

Python Set update() Method with Examples Read More »

Python Set symmetric_difference() Method with Examples

Prerequisite:

Python set() Function with Examples

Set symmetric_difference() Method in Python:

The symmetric_difference() method returns a set that includes all items from both sets but excludes items that are present in both sets.

That means the returned set contains a mix of items that aren’t in either set.

For Example:

If P and Q are two distinct sets. The  Symmetric difference of given two sets is :

The set of elements that are in either P or Q but not in their intersection is the symmetric difference.

Let P={4, 5, 6, 7}

Q={5, 6, 8, 9}

Here {5, 6} are the common elements in both sets. so, we exclude them.

The symmetric difference of P and Q = {4, 7, 8, 9}

Syntax:

set.symmetric_difference(set)

Parameters

set: This is Required. The set to look for matches in.

Examples:

Example1:

Input:

Given first set = {10, 20, 50, 60, 30}
Given second set = {20, 60, 80}

Output:

The symmetric difference of first and secondset =  {10, 80, 50, 30}

Example2:

Input:

Given first set = {4, 5, 6, 7}
Given second set = {5, 6, 8, 9}

Output:

The symmetric difference of first and secondset =  {4, 7, 8, 9}

Set symmetric_difference() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Give the third set as static input and store it in another variable.
  • Get the symmetric difference of the first set and second set using the symmetric_difference() method and print it.
  • Get the symmetric difference of the second set and first set using the symmetric_difference() method and print it.
  • Get the symmetric difference of the first set and third set using the symmetric_difference() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {10, 20, 50, 60, 30}
# Give the second set as static input and store it in another variable.
scnd_set = {20, 60, 80}
# Give the third set as static input and store it in another variable.
thrd_set = {}
# Get the symmetric difference of the first set and second set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and secondset = ",
      fst_set.symmetric_difference(scnd_set))
# Get the symmetric difference of the second set and first set using
# the symmetric_difference() method and print it.
print("The symmetric difference of second and firstset = ",
      scnd_set.symmetric_difference(fst_set))
# Get the symmetric difference of the first set and third set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and thirdset = ",
      fst_set.symmetric_difference(thrd_set))

Output:

The symmetric difference of first and secondset =  {10, 80, 50, 30}
The symmetric difference of second and firstset =  {80, 50, 10, 30}
The symmetric difference of first and thirdset =  {10, 50, 20, 60, 30}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the third set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Get the symmetric difference of the first set and second set using the symmetric_difference() method and print it.
  • Get the symmetric difference of the second set and first set using the symmetric_difference() method and print it.
  • Get the symmetric difference of the first set and third set using the symmetric_difference() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the third set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
thrd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
   
# Get the symmetric difference of the first set and second set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and secondset = ",
      fst_set.symmetric_difference(scnd_set))
# Get the symmetric difference of the second set and first set using
# the symmetric_difference() method and print it.
print("The symmetric difference of second and firstset = ",
      scnd_set.symmetric_difference(fst_set))
# Get the symmetric difference of the first set and third set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and thirdset = ",
      fst_set.symmetric_difference(thrd_set))

Output:

Enter some random Set Elements separated by spaces = 3 4 5 6
Enter some random Set Elements separated by spaces = 3 7 8 9
Enter some random Set Elements separated by spaces = 9 5 7 1
The symmetric difference of first and secondset = {4, 5, 6, 7, 8, 9}
The symmetric difference of second and firstset = {4, 5, 6, 7, 8, 9}
The symmetric difference of first and thirdset = {1, 3, 4, 6, 7, 9}

Python Set symmetric_difference() Method with Examples Read More »

Python Set intersection_update() Method with Examples

Prerequisite:

Python set() Function with Examples

Set intersection_update() Method in Python:

The intersection_update() method removes items that do not appear in both sets (or in all sets if the comparison is done between more than two sets).

The intersection_update() method differs from the intersection() method in that the former returns a new set without the unwanted items, whereas the latter removes the unwanted items from the original set.

Syntax:

set.intersection_update(set1, set2,.....)

Parameters

set1: This is Required. The set to look for items that are similar in

set2: This is Optional. The other set to look for similar items in. You are free to compare as many sets as you want. Use a comma to separate the sets.

Return Value:

None is returned by this method (meaning it does not have a return value). It only calls the intersection_update() method to update the set.

Let P, Q, R be three sets

When you execute the code,

  • the end result will be None
  • P will be updated to the intersection of P, Q, and R.
  • Q does not change.
  • R remains constant.

Examples:

Example1:

Note: Here we are updating the third set.

Input:

Given first set =  {10, 20, 30, 40, 50}
Given second set = {100, 20, 80, 70, 30}
Given third set = {200, 20, 80, 30}

Output:

The given first set is : {40, 10, 50, 20, 30}
The given second set is : {100, 70, 80, 20, 30}
The given third set is : {20, 30}
The result after applying intersection_update() method:  None

Explanation:

Here {20,30} are only the common elements in all the three sets.
Hence we are updating the third set as {20, 30}

Example2:

Note: Here we are updating the first set.

Input:

Given first set =  {'a', 'b', 'c'}
Given second set = {'b', 'p', 'q'}

Output:

The given first set is : {'b'}
The given second set is : {'p', 'q', 'b'}
The result after applying intersection_update() method:  None

Set intersection_update() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Give the third set as static input and store it in another variable.
  • Apply the intersection_update() method to the given first, second and third sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the given third set.
  • Print the above result after applying the intersection_update() method for the given three sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {10, 20, 30, 40, 50}
# Give the second set as static input and store it in another variable.
scnd_set = {100, 20, 80, 70, 30}
# Give the third set as static input and store it in another variable.
thrd_set = {200, 20, 80, 30}
# Apply the intersection_update() method to the given first, second and third
# sets.
# Store it in another variable.
rslt = thrd_set.intersection_update(fst_set, scnd_set)
# print the given first set.
print("The given first set is :", fst_set)
# print the given second set.
print("The given second set is :", scnd_set)
# print the given third set.
print("The given third set is :", thrd_set)
# Print the above result after applying intersection_update() method for the
# given three sets.
print("The result after applying intersection_update() method: ", rslt)

Output:

The given first set is : {40, 10, 50, 20, 30}
The given second set is : {100, 70, 80, 20, 30}
The given third set is : {20, 30}
The result after applying intersection_update() method:  None
Similarly, do the same for only two sets
# Give the first set as static input and store it in a variable.
fst_set = {'a', 'b', 'c'}
# Give the second set as static input and store it in another variable.
scnd_set = {'b', 'p', 'q'}
# Apply the intersection_update() method to the given first and second sets.
# Store it in another variable.
rslt = fst_set.intersection_update(scnd_set)
# print the given first set.
print("The given first set is :", fst_set)
# print the given second set.
print("The given second set is :", scnd_set)
# Print the above result after applying intersection_update() method for the
# given two sets.
print("The result after applying intersection_update() method: ", rslt)

Output:

The given first set is : {'b'}
The given second set is : {'p', 'q', 'b'}
The result after applying intersection_update() method:  None

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the third set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Apply the intersection_update() method to the given first, second and third sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the given third set.
  • Print the above result after applying the intersection_update() method for the given three sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the third set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
thrd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
   
# Apply the intersection_update() method to the given first, second and third
# sets.
# Store it in another variable.
rslt = thrd_set.intersection_update(fst_set, scnd_set)
# print the given first set.
print("The given first set is :", fst_set)
# print the given second set.
print("The given second set is :", scnd_set)
# print the given third set.
print("The given third set is :", thrd_set)
# Print the above result after applying intersection_update() method for the
# given three sets.
print("The result after applying intersection_update() method: ", rslt)

Output:

Enter some random Set Elements separated by spaces = 1 8 9
Enter some random Set Elements separated by spaces = 2 9 4
Enter some random Set Elements separated by spaces = 9 4 1
The given first set is : {8, 1, 9}
The given second set is : {9, 2, 4}
The given third set is : {9}
The result after applying intersection_update() method: None

Python Set intersection_update() Method with Examples Read More »

Python Set intersection() Method with Examples

Prerequisite:

Python set() Function with Examples

Set intersection() Method in Python:

The intersection() method returns a set containing the similarity(common items) of two or more sets.

If the comparison is done with more than two sets, the returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.

For Example:

If P and Q are two distinct sets. A set intersection between P and Q is :

Let P={4,5,6,7}

Q={5,6,8,9}

P intersection Q = {5,6}

Since {5,6} are only the common elements in both the sets

Syntax:

set.intersection(set1, set2,..........)

Parameters

set1: This is Required. The set to look for similar items in

set2: This is Optional. The other set to look for similar items in. You are free to compare as many sets as you want. Use a comma to separate the sets.

Return Value:

The intersection() method returns the intersection of set A and all the other sets (passed as argument).

If the argument is not passed to an intersection(), a shallow copy of the set is returned (A).

Examples:

Example1:

Input:

Given first set =  {10, 20, 30, 40, 50}
Given second set = {100, 20, 80, 70, 30}
Given third set = {200, 40, 80, 30}

Output:

firstset intersection secondset =  {20, 30}
firstset intersection thirdset =  {40, 30}
secondset intersection thirdset =  {80, 30}

Example2:

Input:

Given first set =  {'a', 'b', 'c'}
Given second set = {'p', 'q', 'r'}
Given third set = {'q', 'a', 'k'}

Output:

firstset intersection secondset =  set()
firstset intersection thirdset =  {'a'}
secondset intersection thirdset =  {'q'}

Set intersection() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Give the third set as static input and store it in another variable.
  • Get the intersection(common elements) of the first set and second set using the intersection () method and print it.
  • Get the intersection(common elements) of the first set and third set using the intersection () method and print it.
  • Get the intersection(common elements) of the second set and third set using the intersection () method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {10, 20, 30, 40, 50}
# Give the second set as static input and store it in another variable.
scnd_set = {100, 20, 80, 70, 30}
# Give the third set as static input and store it in another variable.
thrd_set = {200, 40, 80, 30}
# Get the intersection(common elements) of the first set and second set using
# the intersection () method and print it.
print("firstset intersection secondset = ", fst_set.intersection(scnd_set))
# Get the intersection(common elements) of the first set and third set using
# the intersection () method and print it.
print("firstset intersection thirdset = ", fst_set.intersection(thrd_set))
# Get the intersection(common elements) of the second set and third set using
# the intersection () method and print it.
print("secondset intersection thirdset = ", scnd_set.intersection(thrd_set))

Output:

firstset intersection secondset =  {20, 30}
firstset intersection thirdset =  {40, 30}
secondset intersection thirdset =  {80, 30}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the third set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Get the intersection(common elements) of the first set and second set using the intersection () method and print it.
  • Get the intersection(common elements) of the first set and third set using the intersection () method and print it.
  • Get the intersection(common elements) of the second set and third set using the intersection () method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the third set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
thrd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
   
# Get the intersection(common elements) of the first set and second set using
# the intersection () method and print it.
print("firstset intersection secondset = ", fst_set.intersection(scnd_set))
# Get the intersection(common elements) of the first set and third set using
# the intersection () method and print it.
print("firstset intersection thirdset = ", fst_set.intersection(thrd_set))
# Get the intersection(common elements) of the second set and third set using
# the intersection () method and print it.
print("secondset intersection thirdset = ", scnd_set.intersection(thrd_set))

Output:

Enter some random Set Elements separated by spaces = 1 2 3
Enter some random Set Elements separated by spaces = 1 5 7
Enter some random Set Elements separated by spaces = 9 8 0
firstset intersection secondset = {1}
firstset intersection thirdset = set()
secondset intersection thirdset = set()

 

 

Python Set intersection() Method with Examples Read More »

Python Set difference_update() Method with Examples

Prerequisite:

Python set() Function with Examples

Set difference_update() Method in Python:

The difference_update() method removes items from both sets.

The difference_update() method differs from the difference() method in that the difference() method returns a new set that does not include the unwanted items, whereas the difference_update() method removes the unwanted items from the original set.

For Example

If P and Q are two distinct sets. A set difference between P and Q is a set of elements that exist only in set P but not in set Q.

The difference_update() replaces set P with the P-Q set difference.

Syntax:

set.difference_update(set)

Parameters

set: This is Required. The set used to look for differences.

Return Value:

difference_update() produces a result None, indicating that the object (set) has been mutated.

Let P, Q be two sets

When you execute the code,

  • The result is None.
  • P will be updated to P-Q.
  • Q will remain unchanged.

Examples:

Example1:

Input:

Given first set = {10, 11, 12, 13}
Given second set = {11, 12, 20, 14}

Output:

The given first set is : {10, 13}
The given second set is : {11, 12, 20, 14}
The result after applying difference_update method:  None

Explanation:

Here it removes the common elements{11, 12} from the first set and
updates the given first set as {10, 13}.

Example2:

Input:

Given first set = {20, 30, 40, 20, 30, 50, 60}
Given second set = {40, 50, 60, 100, 80}

Output:

The given first set is : {20, 30}
The given second set is : {100, 40, 80, 50, 60}
The result after applying difference_update method:  None

Set difference_update() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Apply the difference_update() method to the given first and second sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the second set.
  • Print the above result after applying the difference_update() method for the given sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_Set = {10, 11, 12, 13}
# Give the second set as static input and store it in another variable.
scnd_set = {11, 12, 20, 14}
# Apply the difference_update() method to the given first and second sets.
# Store it in another variable.
rslt = fst_Set.difference_update(scnd_set)
# print the given first set.
print("The given first set is :", fst_Set)
# print the given second set.
print("The given second set is :", scnd_set)
# Print the above result after applying difference_update() method for the given sets.
print("The result after applying difference_update method: ", rslt)

Output:

The given first set is : {10, 13}
The given second set is : {11, 12, 20, 14}
The result after applying difference_update method:  None

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Apply the difference_update() method to the given first and second sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the second set.
  • Print the above result after applying the difference_update() method for the given sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Apply the difference_update() method to the given first and second sets.
# Store it in another variable.
rslt = fst_set.difference_update(scnd_set)
# print the given first set.
print("The given first set is :", fst_set)
# print the second first set.
print("The given second set is :", scnd_set)
# Print the above result after applying difference_update method for the given sets.
print("The result after applying difference_update method: ", rslt)

Output:

Enter some random Set Elements separated by spaces = 10 20 15 16
Enter some random Set Elements separated by spaces = 20 15 16 80
The given first set is : {10}
The given second set is : {16, 80, 20, 15}
The result after applying difference_update method: None

Python Set difference_update() Method with Examples Read More »

Python Set difference() Method with Examples

Prerequisite:

Python set() Function with Examples

Set difference() Method in Python:

The difference() method returns a set containing the difference of two sets.

The returned set contains items that only exist in the first set and not in both.

For Example:

If P and Q are two distinct sets. A set difference between P and Q is a set of elements that exist only in the set P but not in the set Q.

Let P={4,5,6,7}

Q={5,6,8,9}

P-Q={4,7}

Q-P ={8,9}

Syntax:

set.difference(set)

Parameters

set: This is Required. The set used to look for differences.

Return Value:

difference() computes the difference between two sets, each of which is a set. It makes no changes to the original sets.

Examples:

Example1:

Input:

Given first set = {10, 11, 12, 13}
Given second set = {11, 12, 20, 14}

Output:

firstset-secondset :  {10, 13}
secondset-firstset :  {20, 14}

Example2:

Input:

Given first set = {20, 30, 40, 20, 30, 50, 60}
Given second set = {40, 50, 60, 100, 80}

Output:

firstset-secondset :  {20, 30}
secondset-firstset :  {80, 100}

Set difference() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Set Difference Using difference() method

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Get the difference between the( first set – second set ) using the difference() method.
  • Store it in another variable.
  • Get the difference between the (second set – first set ) using the difference() method.
  • Store it in another variable.
  • Print the result (first set – second set).
  • Print the result (second set – first set ).
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_Set = {10, 11, 12, 13}
# Give the second set as static input and store it in another variable.
scnd_set = {11, 12, 20, 14}
# Get the difference between the( first set - second set ) using the difference()
# method.
# Store it in another variable.
rslt1 = fst_Set.difference(scnd_set)
# Get the difference between the (second set - first set ) using the difference()
# method.
# Store it in another variable.
rslt2 = scnd_set.difference(fst_Set)
# Print the result (first set - second set).
print("firstset-secondset : ", rslt1)
# Print the result (second set - first set ).
print("secondset-firstset : ", rslt2)

Output:

firstset-secondset :  {10, 13}
secondset-firstset :  {20, 14}
2)Set Difference Using ‘-‘ Operator

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Get the difference between the( first set – second set ) using the ‘-‘ operator and print it.
  • Get the difference between the (second set – first set ) using the ‘-‘ operator and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {'u', 'v', 'w', 'x', 'y', 'u'}
# Give the second set as static input and store it in another variable.
scnd_set = {'w', 'z', 'x', 'a'}
# Get the difference between the( first set - second set ) using the '-' operator
# and print it
print("firstset-secondset : ", fst_set-scnd_set)
# Get the difference between the (second set - first set ) using the '-' operator
# print it.
print("secondset-firstset : ", scnd_set-fst_set)

Output:

firstset-secondset :  {'y', 'v', 'u'}
secondset-firstset :  {'z', 'a'}

Method #2: Using Built-in Functions (User Input)

1)Set Difference Using difference() method

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Get the difference between the( first set – second set ) using the difference() method.
  • Store it in another variable.
  • Get the difference between the (second set – first set ) using the difference() method.
  • Store it in another variable.
  • Print the result (first set – second set).
  • Print the result (second set – first set ).
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Get the difference between the( first set - second set ) using the difference()
# method.
# Store it in another variable.
rslt1 = fst_set.difference(scnd_set)
# Get the difference between the (second set - first set ) using the difference()
# method.
# Store it in another variable.
rslt2 = scnd_set.difference(fst_set)
# Print the result (first set - second set).
print("firstset-secondset : ", rslt1)
# Print the result (second set - first set ).
print("secondset-firstset : ", rslt2)

Output:

Enter some random Set Elements separated by spaces = 20 30 40 10 10
Enter some random Set Elements separated by spaces = 60 50 20 30
firstset-secondset : {40, 10}
secondset-firstset : {50, 60}
2)Set Difference Using ‘-‘ Operator

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Get the difference between the( first set – second set ) using the ‘-‘ operator and print it.
  • Get the difference between the (second set – first set ) using the ‘-‘ operator and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Get the difference between the( first set - second set ) using the '-' operator
# and print it
print("firstset-secondset : ", fst_set-scnd_set)
# Get the difference between the (second set - first set ) using the '-' operator
# print it.
print("secondset-firstset : ", scnd_set-fst_set)

Output:

Enter some random Set Elements separated by spaces = 10 10 13 15
Enter some random Set Elements separated by spaces = 12 15
firstset-secondset : {10, 13}
secondset-firstset : {12}

 

 

Python Set difference() Method with Examples Read More »

Python Dictionary values() Function with Examples

Dictionary values() Function in Python:

The values() method returns a view object that displays a list of all the dictionary’s values.

Syntax:

dictionary.values()

Parameters: This method doesn’t accept any parameters

Return Value:

The values() method returns a view object containing a list of all values in a given dictionary.

Examples:

Example1:

Input:

Given dictionary = {20: 'good', 30: 'morning', 40: 'btechgeeks'}

Output:

The values of the given dictionary is:
dict_values(['good', 'morning', 'btechgeeks'])

Example2:

Input:

Given dictionary = {'hello': 1, 'this': 3, 'is': 4, 'btechgeeks': 8}

Output:

The values of the given dictionary is:
dict_values([1, 3, 4, 8])

Dictionary values() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Get all the values of the given dictionary using the values() method.
  • Store it in another variable.
  • Print all the values of the given dictionary.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict = {20: 'good', 30: 'morning', 40: 'btechgeeks'}
# Get all the values of the given dictionary using the values() method.
# Store it in another variable.
dict_valus = gvn_dict.values()
# Print all the values of the given dictionary.
print("The values of the given dictionary is:")
print(dict_valus)

Output:

The values of the given dictionary is:
dict_values(['good', 'morning', 'btechgeeks'])

Method #2: Using Built-in Functions (User Input)

Approach:

  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Get all the values of the given dictionary using the values() method.
  • Store it in another variable.
  • Print all the values of the given dictionary.
  • The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
    input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
        # Inside the for loop scan the key and value as
    # user input using input(),split() functions
    # and store them in two separate variables.
    keyy, valuee = input(
        'Enter key and value separated by spaces = ').split()
    # Initialize the key with the value of the dictionary.
    gvn_dict[keyy] = valuee

# Get all the values of the given dictionary using the values() method.
# Store it in another variable.
dict_valus = gvn_dict.values()
# Print all the values of the given dictionary.
print("The values of the given dictionary is:")
print(dict_valus)

Output:

Enter some random number of keys of the dictionary = 4
Enter key and value separated by spaces = hello 350
Enter key and value separated by spaces = this 450
Enter key and value separated by spaces = is 550
Enter key and value separated by spaces = btechgeeks 650
The values of the given dictionary is:
dict_values(['350', '450', '550', '650'])

Python Dictionary values() Function with Examples Read More »

Python Dictionary copy() Function with Examples

Dictionary copy() Function in Python:

The copy() method makes a duplicate of the specified dictionary(copy).

Syntax:

dictionary.copy()

Parameters: This method doesn’t accept any parameters

Return Value:

This method gives you a shallow copy of the dictionary. It makes no changes to the original dictionary.

Examples:

Example1:

Input:

Given dictionary = {20: 'good', 30: 'morning', 40: 'btechgeeks'}

Output:

The given original dictionary is:
{20: 'good', 30: 'morning', 40: 'btechgeeks'}
The copied new dictionary is:
{20: 'good', 30: 'morning', 40: 'btechgeeks'}

Example2:

Input:

Given dictionary = {'hello': 100, 'btechgeeks': 200}

Output:

The given original dictionary is:
{'hello': 100, 'btechgeeks': 200}
The copied new dictionary is:
{'hello': 100, 'btechgeeks': 200}

Dictionary copy() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Copy the given dictionary into a new dictionary using the copy() function.
  • Store it in another variable.
  • Print the given original dictionary.
  • Print the copied new dictionary.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict = {20: 'good', 30: 'morning', 40: 'btechgeeks'}
# Copy the given dictionary into a new dictionary using the copy() function.
# Store it in another variable.
new_dict = gvn_dict.copy()
# Print the given original dictionary.
print("The given original dictionary is:")
print(gvn_dict)
# Print the copied new dictionary.
print("The copied new dictionary is:")
print(new_dict)

Output:

The given original dictionary is:
{20: 'good', 30: 'morning', 40: 'btechgeeks'}
The copied new dictionary is:
{20: 'good', 30: 'morning', 40: 'btechgeeks'}
Difference between Dictionary copy() Function , = Operator

When the copy() method is used, a new dictionary is created that contains a copy of the original dictionary’s references.
When the = operator is used, it creates a new reference to the original dictionary.

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Copy the given dictionary into a new dictionary using the ‘=’ Operator.
  • Store it in another variable.
  • Remove all the elements from the new dictionary using the clear() function.
  • Print the given original dictionary.
  • Print the copied new dictionary.
  • The Exit of the Program.
# Give the dictionary as static input and store it in a variable.
gvn_dict = {'hello': 100, 'btechgeeks': 200}
# Copy the given dictionary into a new dictionary using the '=' Operator.
# Store it in another variable.
new_dict = gvn_dict
# Remove all the elements from the new dictionary using the clear() function.
new_dict.clear()
# Print the given original dictionary.
print("The given original dictionary is:")
print(gvn_dict)
# Print the copied new dictionary.
print("The copied new dictionary is:")
print(new_dict)

Output:

The given original dictionary is:
{}
The copied new dictionary is:
{}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Copy the given dictionary into a new dictionary using the copy() function.
  • Store it in another variable.
  • Print the given original dictionary.
  • Print the copied new dictionary.
  • The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
    input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
        # Inside the for loop scan the key and value as
    # user input using input(),split() functions
    # and store them in two separate variables.
    keyy, valuee = input(
        'Enter key and value separated by spaces = ').split()
    # Initialize the key with the value of the dictionary.
    gvn_dict[keyy] = valuee

# Copy the given dictionary into a new dictionary using the copy() function.
# Store it in another variable.
new_dict = gvn_dict.copy()
# Print the given original dictionary.
print("The given original dictionary is:")
print(gvn_dict)
# Print the copied new dictionary.
print("The copied new dictionary is:")
print(new_dict)

Output:

Enter some random number of keys of the dictionary = 2
Enter key and value separated by spaces = 100 hello
Enter key and value separated by spaces = 200 btechgeeks
The given original dictionary is:
{'100': 'hello', '200': 'btechgeeks'}
The copied new dictionary is:
{'100': 'hello', '200': 'btechgeeks'}

 

 

Python Dictionary copy() Function with Examples Read More »

Python str() Function with Examples

str() Function in Python:

The str() function returns a string from the specified value.

Syntax:

str(object, encoding=encoding, errors=errors)

Parameters

object: It may be any object. The object to be converted into a string is specified.

encoding: The object’s encoding. UTF-8 is the default.

errors: Specifies what should be done if the decoding fails.

Errors are classified into six types:

strict – the default response, which throws a UnicodeDecodeError exception if it fails.

ignore – removes any unencodable Unicode from the output.

replace – converts an unencodable Unicode character to a question mark.

xmlcharrefreplace – inserts an XML character reference rather than an unencodable Unicode character.

backslashreplace – replaces unencodable Unicode with an /uNNNN espace sequence.

namereplace – replaces unencodable Unicode with an N… escape sequence.

Return Value:

The str() method returns a string, which is an informal or nicely printable representation of the object passed in.

Examples:

Example1:

Input:

Given Number = 25

Output:

The given number after converting it into a string =  25
<class 'str'>

Example2:

Input:

Given Number = 80

Output:

The given number after converting it into a string =  80
<class 'str'>

str() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the str() function that converts the given number into a string. (integer to string)
  • Store it in another variable.
  • Print the given number after converting it into a string.
  • Print the type of the above result (string number).
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 25
# Pass the given number as an argument to the str() function that converts
# the given number into a string. (integer to string)
# Store it in another variable.
str_numb = str(gvn_numb)
# Print the given number after converting it into a string.
print("The given number after converting it into a string = ", str_numb)
# Print the type of the above result.(string number)
print(type(str_numb))

Output:

The given number after converting it into a string =  25
<class 'str'>
str() for bytes

If both the encoding and errors parameters are specified, the first parameter, object, must be a bytes-like-object (bytes or bytearray).

If the object is bytes or bytearray, str() calls bytes internally.

bytes.decode(encoding, errors)

Otherwise, it loads the bytes object into the buffer before invoking the decode() method.

# str() for bytes
k = bytes('Prögrams', encoding='utf-8')
print(str(k, encoding='ascii', errors='ignore'))

Output:

Prgrams

Explanation:

ASCII cannot decode the character ‘ö’ in this case. As a result, it should throw an error. We have, however, set the errors to ‘ignore’. As a result, Python ignores the character that cannot be decoded by str ().

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the str() function that converts the given number into a string. (integer to string)
  • Store it in another variable.
  • Print the given number after converting it into a string.
  • Print the type of the above result (string number).
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the str() function that converts
# the given number into a string. (integer to string)
# Store it in another variable.
str_numb = str(gvn_numb)
# Print the given number after converting it into a string.
print("The given number after converting it into a string = ", str_numb)
# Print the type of the above result.(string number)
print(type(str_numb))

Output:

Enter some random number = 70
The given number after converting it into a string = 70
<class 'str'>

Python str() Function with Examples Read More »