Python

Python sqrt() Method with Examples

sqrt() Method in Python:

The square root of a number is returned by the math.sqrt() method.

Note: It should be noted that the number must be greater than or equal to 0.

Syntax:

math.sqrt(number)

Parameters

number: This is Required. A number whose square root is to be found.

  • If the number is less than zero, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value that represents a number’s square root.

Examples:

Example1:

Input:

Given Number = 36

Output:

The square root of a given number{ 36 } =  6.0

Example2:

Input:

Given Number = 225

Output:

The square root of a given number{ 225 } =  15.0

sqrt() Method with Examples in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the math.sqrt() function to get the square root value of a given number.
  • Store it in another variable.
  • Print the square root value of a given number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as static input and store it in a variable.
gvn_numb = 36
# Pass the given number as an argument to the math.sqrt() function to get the
# square root value of a given number.
# Store it in another variable.
squre_rootrslt = math.sqrt(gvn_numb)
# Print the square root value of a given number.
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

The square root of a given number{ 36 } =  6.0
Similarly Check out for other numbers
import math
gvn_numb = -25
squre_rootrslt = math.sqrt(gvn_numb)
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

Traceback (most recent call last):
  File "/home/47ab19f64e175f148c2a3f48359df760.py", line 3, in <module>
    squre_rootrslt = math.sqrt(gvn_numb)
ValueError: math domain error
import math
gvn_numb = 45.5
squre_rootrslt = math.sqrt(gvn_numb)
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

The square root of a given number{ 45.5 } =  6.745368781616021

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

Approach:

  • Import math module using the import keyword.
  • 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 math.sqrt() function to get the square root value of a given number.
  • Store it in another variable.
  • Print the square root value of a given number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# 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 math.sqrt() function to get the
# square root value of a given number.
# Store it in another variable.
squre_rootrslt = math.sqrt(gvn_numb)
# Print the square root value of a given number.
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

Enter some random number = 225
The square root of a given number{ 225 } = 15.0

Python sqrt() Method with Examples Read More »

Python pow() Function with Examples

pow() Function in Python:

The pow() function returns the x raised to the power of y value (xy).

If there is a third parameter, it returns x to the power of y modulus z.

Syntax:

pow(x, y, z)

Parameters

x: It’s a number. It is the base value.

y: It’s a number. It is the exponent value.

z: This is optional. It is the modulus value.

Examples:

Example1:

Input:

Given base = 5
Given exponent = 2

Output:

The value of given base{ 5 } to the power{ 2 }=  25

Example2:

Input:

Given base = 5
Given exponent = 6
Given modulus = 7

Output:

The value of given base{ 5 } to the power{ 6 } modulus{ 7 } =  1

pow() Function with Examples in Python

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

1) passing only two parameters

Approach:

  • Give the base value as static input and store it in a variable.
  • Give the exponent value as static input and store it in another variable.
  • Pass the given base and exponent values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as static input and store it in a variable.
gvn_baseval = 5
# Give the exponent value as static input and store it in another variable.
gvn_expont = 2
# Pass the given base and exponent values as the arguments to the pow()
# function to get the value of the given base raised to the power of
# the given exponent.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "}= ", rslt)

Output:

The value of given base{ 5 } to the power{ 2 }=  25
2) passing three parameters

Approach:

  • Give the base value as static input and store it in a variable.
  • Give the exponent value as static input and store it in another variable.
  • Give the modulus value as static input and store it in another variable.
  • Pass the given base, exponent, and modulus values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent modulus given modulus value.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as static input and store it in a variable.
gvn_baseval = 6
# Give the exponent value as static input and store it in another variable.
gvn_expont = 2
# Give the modulus value as static input and store it in another variable.
gvn_moduls = 5
# Pass the given base, exponent, and modulus values as the arguments to the
# pow() function to get the value of the given base raised to the power
# of the given exponent modulus given modulus value.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont, gvn_moduls)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "} modulus{", gvn_moduls, "} = ", rslt)

Output:

The value of given base{ 6 } to the power{ 2 } modulus{ 5 } =  1

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

1) passing only two parameters

Approach:

  • Give the base value as user input using the int(input()) function and store it in a variable.
  • Give the exponent value as user input using the int(input()) function and store it in another variable.
  • Pass the given base and exponent values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as user input using the int(input()) function and store it in a variable.
gvn_baseval = int(input("Enter some random number = "))
# Give the exponent value as user input using the int(input()) function 
# and store it in another variable.
gvn_expont = int(input("Enter some random number = "))
# Pass the given base and exponent values as the arguments to the pow()
# function to get the value of the given base raised to the power of
# the given exponent.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "}= ", rslt)

Output:

Enter some random number = 2
Enter some random number = 5
The value of given base{ 2 } to the power{ 5 }= 32
2) passing three parameters

Approach:

  • Give the base value as user input using the int(input()) function and store it in a variable.
  • Give the exponent value as user input using the int(input()) function and store it in another variable.
  • Give the modulus value as user input using the int(input()) function and store it in another variable.
  • Pass the given base, exponent, and modulus values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent modulus given modulus value.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as user input using the int(input()) function and store it in a variable.
gvn_baseval = int(input("Enter some random number = "))
# Give the exponent value as user input using the int(input()) function 
# and store it in another variable.
gvn_expont = int(input("Enter some random number = "))
# Give the modulus value as user input using the int(input()) function 
# and store it in another variable.
gvn_moduls = int(input("Enter some random number = "))
# Pass the given base, exponent, and modulus values as the arguments to the
# pow() function to get the value of the given base raised to the power
# of the given exponent modulus given modulus value.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont, gvn_moduls)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "} modulus{", gvn_moduls, "} = ", rslt)

Output:

Enter some random number = 5
Enter some random number = 6
Enter some random number = 7
The value of given base{ 5 } to the power{ 6 } modulus{ 7 } = 1

Python pow() Function with Examples Read More »

Python math.log10() Method with Examples

math.log10() Method in Python:

The math.log10() method returns a number’s base-10 logarithm.

Syntax:

math.log10(x)

Parameters

x: This is Required. Specifies the value for which the logarithm should be calculated.

  • If the value is 0 or a negative number, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value that represents a number’s base-10 logarithm.

Examples:

Example1:

Input:

Given Number = 4
Given List = [2, 3.5, 4, 5.62, 1]

Output:

The base-10 logarithm of a given number { 4 } = 0.6020599913279624
The base-10 logarithm of a list element gvn_lst[1] = 0.5440680443502757

Example2:

Input:

Given Number = 2
Given List = [8, 2, 9, 1, 0]

Output:

The base-10 logarithm of a given number { 2.0 } = 0.3010299956639812
The base-10 logarithm of a list element gvn_lst[2] = 0.9542425094393249

math.log10() Method with Examples in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Apply math.log10() method to the given number to get base-10 logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Apply the math.log10() method to the given list to get base-10 logarithm of a list element.
  • Store it in another variable.
  • Print the base-10 logarithm of a given list element.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [2, 3.5, 4, 5.62, 1]
# Give the number as static input and store it in another variable.
gvn_numb = 4
# Apply math.log10() method to the given number to get base-10 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log10(gvn_numb)
# Print the above result.
print("The base-10 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log10() method to the given list to get base-10 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log10(gvn_lst[1])
# Print the base-10 logarithm of a given list element.
print("The base-10 logarithm of a list element gvn_lst[1] =", rslt2)

Output:

The base-10 logarithm of a given number { 4 } = 0.6020599913279624
The base-10 logarithm of a list element gvn_lst[1] = 0.5440680443502757

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

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the number as user input using the float(input()) function and store it in another variable.
  • Apply math.log10() method to the given number to get base-10 logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Apply the math.log10() method to the given list to get base-10 logarithm of a list element.
  • Store it in another variable.
  • Print the base-10 logarithm of a given list element.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the number as user input using the float(input()) function and 
# store it in another variable.
gvn_numb = float(input("Enter some random number = "))
# Apply math.log10() method to the given number to get base-10 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log10(gvn_numb)
# Print the above result.
print("The base-10 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log10() method to the given list to get base-10 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log10(gvn_lst[2])
# Print the base-10 logarithm of a given list element.
print("The base-10 logarithm of a list element gvn_lst[2] =", rslt2)

Output:

Enter some random List Elements separated by spaces = 8 2 9 1 0
Enter some random number = 2 
The base-10 logarithm of a given number { 2.0 } = 0.3010299956639812
The base-10 logarithm of a list element gvn_lst[2] = 0.9542425094393249

Python math.log10() Method with Examples Read More »

Python math.log2() Method with Examples

math.log2() Method in Python:

The math.log2() method returns a number’s base-2 logarithm.

Syntax:

math.log2(x)

Parameters

x: This is Required. Specifies the value for which the logarithm should be calculated.

  • If the value is 0 or a negative number, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value that represents a number’s base-2 logarithm.

Examples:

Example1:

Input:

Given Number = 4
Given List = [2, 3.5, 4, 5.62, 1]

Output:

The base-2 logarithm of a given number { 4 } = 2.0
The base-2 logarithm of a list element gvn_lst[1] = 1.8073549220576042

Example2:

Input:

Given Number = 5.56
Given List = [4, 5, 2, 1, 6]

Output:

The base-2 logarithm of a given number { 5.56 } = 2.475084882948783
The base-2 logarithm of a list element gvn_lst[2] = 1.0

math.log2() Method with Examples in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Apply math.log2() method to the given number to get base-2 logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Apply math.log2() method to the given list to get base-2 logarithm of a list element.
  • Store it in another variable.
  • Print the base-2 logarithm of a given list element.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [2, 3.5, 4, 5.62, 1]
# Give the number as static input and store it in another variable.
gvn_numb = 4
# Apply math.log2() method to the given number to get base-2 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log2(gvn_numb)
# Print the above result.
print("The base-2 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log2() method to the given list to get base-2 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log2(gvn_lst[1])
# Print the base-2 logarithm of a given listelement.
print("The base-2 logarithm of a list element gvn_lst[1] =", rslt2)

Output:

The base-2 logarithm of a given number { 4 } = 2.0
The base-2 logarithm of a list element gvn_lst[1] = 1.8073549220576042

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

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the number as user input using the float(input()) function and store it in another variable.
  • Apply math.log2() method to the given number to get base-2 logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Apply math.log2() method to the given list to get base-2 logarithm of a list element.
  • Store it in another variable.
  • Print the base-2 logarithm of a given list element.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the number as user input using the float(input()) function and 
# store it in another variable.
gvn_numb = float(input("Enter some random number = "))
# Apply math.log2() method to the given number to get base-2 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log2(gvn_numb)
# Print the above result.
print("The base-2 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log2() method to the given list to get base-2 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log2(gvn_lst[2])
# Print the base-2 logarithm of a given list element.
print("The base-2 logarithm of a list element gvn_lst[2] =", rslt2)

Output:

Enter some random List Elements separated by spaces = 4 5 2 1 6
Enter some random number = 5.56
The base-2 logarithm of a given number { 5.56 } = 2.475084882948783
The base-2 logarithm of a list element gvn_lst[2] = 1.0

Python math.log2() Method with Examples Read More »

Python Set issuperset() Method with Examples

Prerequisite:

Python set() Function with Examples

Set issuperset() Method in Python:

If all items in the specified set exist in the original set, the issuperset() method returns true; otherwise, it returns False.

For Example,

Let p = {1, 2, 3}

q= {1, 2, 3, 4, 5, 6}

Here all the elements of set ‘p ‘ are in the set ‘q’ . Hence set q is the superset of set p.

And set p is the subset of set q.

Syntax:

set.issuperset(set)

Parameters

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

Return Value:

issuperset() gives

  • If A is a superset of B, then this statement is true.
  • If A is not a superset of B, the statement is false.

Examples:

Example1:

Input:

Given first set = {20, 30, 100}
Given second set = {100, 20, 80, 70, 30}

Output:

Check if the given second set is superset of the first set: True

Example2:

Input:

Given first set = {20, 30, 100, 12, 15, 16}
Given second set = {12, 15, 16}

Output:

Check if the given second set is superset of the first set: False

Explanation:

Here the first set is the superset of the second set. 
But Not the second set is a superset of the first. Hence it returns False.

Set issuperset() 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 issuperset() method to the given first and second sets to check if the given second set is the superset of the first set or not.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {20, 30, 100}
# Give the second set as static input and store it in another variable.
scnd_set = {100, 20, 80, 70, 30}
# Apply the issuperset() method to the given first and second sets to check if the
# given second set is the superset of the first set or not.
# Store it in another variable.
rslt = scnd_set.issuperset(fst_set)
# Print the above result.
print("Check if the given second set is superset of the first set:", rslt)

Output:

Check if the given second set is superset of the first set: True
Similarly, check for the other example
fst_set = {20, 30, 100, 12, 15, 16}
scnd_set = {12, 15, 16}
rslt = scnd_set.issuperset(fst_set)
print("Check if the given second set is superset of the first set:", rslt)

Output:

Check if the given second set is superset of the first set: False

Explanation:

Here the first set is the superset of the second set. 
But Not the second set is a superset of the first. Hence it returns False.

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 issuperset() method to the given first and second sets to check if the given second set is the superset of the first set or not.
  • Store it in another variable.
  • Print the above result.
  • 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 issuperset() method to the given first and second sets to check if the
# given second set is the superset of the first set or not.
# Store it in another variable.
rslt = scnd_set.issuperset(fst_set)
# Print the above result.
print("Check if the given second set is superset of the first set:", rslt)

Output:

Enter some random Set Elements separated by spaces = 10 12 14 16
Enter some random Set Elements separated by spaces = 12 13
Check if the given second set is superset of the first set: False

 

Python Set issuperset() Method with Examples Read More »

Python Set issubset() Method with Examples

Prerequisite:

Python set() Function with Examples

Set issubset() Method in Python:

If all of the items in the set exist in the specified set, the issubset() method returns true; otherwise, it returns False.

For Example,

Let p = {1, 2, 3}

q= {1, 2, 3, 4, 5, 6}

Here all the elements of set ‘p ‘ are in the set ‘q’ .Hence set p is a subset of set q.

Syntax:

set.issubset(set)

Parameters

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

Return Value:

issubset() produces a result.

  • If A is a subset of B, then this statement is true.
  • If A is not a subset of B, the statement is false.

Examples:

Example1:

Input:

Given first set = {20, 30, 100}
Given second set = {100, 20, 80, 70, 30}

Output:

Check if the given first set is subset of the second set: True

Example2:

Input:

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

Output:

Check if the given first set is subset of the second set: False

Set issubset() 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 issubset() method to the given first and second sets to check if the given first set is a subset of the second set or not.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {20, 30, 100}
# Give the second set as static input and store it in another variable.
scnd_set = {100, 20, 80, 70, 30}
# Apply the issubset() method to the given first and second sets to check if the
# given first set is subset of the second set or not.
# Store it in another variable.
rslt = fst_set.issubset(scnd_set)
# Print the above result.
print("Check if the given first set is subset of the second set:", rslt)

Output:

Check if the given first set is subset of the second set: True
Similarly, check for the other example
fst_set = {'a', 'l', 'm'}
scnd_set = {'m', 'a', 'l'}
rslt = fst_set.issubset(scnd_set)
print("Check if the given first set is subset of the second set:", rslt)

Output:

Check if the given first set is subset of the second set: 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.
  • Apply the issubset() method to the given first and second sets to check if the given first set is a subset of the second set or not.
  • Store it in another variable.
  • Print the above result.
  • 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 issubset() method to the given first and second sets to check if the
# given first set is subset of the second set or not.
# Store it in another variable.
rslt = fst_set.issubset(scnd_set)
# Print the above result.
print("Check if the given first set is subset of the second set:", rslt)

Output:

Enter some random Set Elements separated by spaces = 3 5 6 1
Enter some random Set Elements separated by spaces = 3 7 8 9
Check if the given first set is subset of the second set: False

 

Python Set issubset() Method with Examples Read More »

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 »