Author name: Vikram Chiluka

Python math.erfc() Method with Examples

math.erfc() Method in Python:

The math.erfc() method returns a number’s complementary error function.

This method accepts values between – inf and + inf and returns between 0 and 2.

Syntax:

math.erfc(x)

Parameters

x: This is Required. It is a number used to calculate the complementary error function of

Return Value:

Returns a float value that represents a number’s complementary error function.

Examples:

Example1:

Input:

Given Number = 0.35

Output:

The given number's { 0.35 } complementary error function =  0.6206179464376897

Example2:

Input:

Given Number = -5.6

Output:

The given number's { -5.6 } complementary error function =  1.9999999999999976

math.erfc() 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.erfc() function to get the given number’s complementary error function.
  • Store it in another variable.
  • Print the complementary error function of the 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 = 0.35
# Pass the given number as an argument to the math.erfc() function to get
# the given number's complementary error function.
# Store it in another variable.
rslt = math.erfc(gvn_numb)
# Print the complementary error function of the given number.
print("The given number's {", gvn_numb,
      "} complementary error function = ", rslt)

Output:

The given number's { 0.35 } complementary error function =  0.6206179464376897

Similarly, try for other numbers.

import math
gvn_numb = -1
rslt = math.erfc(gvn_numb)
print("The given number's {", gvn_numb,
      "} complementary error function = ", rslt)

Output:

The given number's { -1 } complementary error function =  1.842700792949715

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

Approach:

  • Import math module using the import keyword.
  • Give the number as user input using the float(input()) function and store it in a variable.
  • Pass the given number as an argument to the math.erfc() function to get the given number’s complementary error function.
  • Store it in another variable.
  • Print the complementary error function of the 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 float(input()) function and store it in a variable.
gvn_numb = float(input("Enter some random number = "))
# Pass the given number as an argument to the math.erfc() function to get
# the given number's complementary error function.
# Store it in another variable.
rslt = math.erfc(gvn_numb)
# Print the complementary error function of the given number.
print("The given number's {", gvn_numb,
      "} complementary error function = ", rslt)

Output:

Enter some random number = -5.6
The given number's { -5.6 } complementary error function = 1.9999999999999976

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

Python math.erf() Method with Examples

math.erf() Method in Python:

The math.erf() method returns a number’s error function.

This method accepts values ranging from – inf to + inf and returns a value ranging from – 1 to + 1.

Syntax:

math.erf(x)

Parameters

x: This is Required. It is a number used to calculate the error function of

Return Value:

Returns a float value that represents a number’s error function.

Examples:

Example1:

Input:

Given Number = 0.5

Output:

The given number's { 0.5 } error function =  0.5204998778130465

Example2:

Input:

Given Number = -4.5

Output:

The given number's { -4.5 } error function =  -0.9999999998033839

math.erf() Method with Examples in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the number(angle) as static input and store it in a variable.
  • Pass the given  as an argument to the math.erf() function to get the given number’s error function.
  • Store it in another variable.
  • Print the error function of the 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 = 0.5
# Pass the given number as an argument to the math.erf() function to get
# the given number's error function.
# Store it in another variable.
rslt = math.erf(gvn_numb)
# Print the error function of the given number.
print("The given number's {", gvn_numb, "} error function = ", rslt)

Output:

The given number's { 0.5 } error function =  0.5204998778130465

Similarly, try for other numbers.

import math
gvn_numb = -15.2
rslt = math.erf(gvn_numb)
print("The given number's {", gvn_numb, "} error function = ", rslt)

Output:

The given number's { -15.2 } error function =  -1.0

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

Approach:

  • Import math module using the import keyword.
  • Give the number as user input using the float(input()) function and store it in a variable.
  • Pass the given number as an argument to the math.erf() function to get the given number’s error function.
  • Store it in another variable.
  • Print the error function of the 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 float(input()) function and store it in a variable.
gvn_numb = float(input("Enter some random number = "))
# Pass the given number as an argument to the math.erf() function to get
# the given number's error function.
# Store it in another variable.
rslt = math.erf(gvn_numb)
# Print the error function of the given number.
print("The given number's {", gvn_numb, "} error function = ", rslt)

Output:

Enter some random number = -4.5
The given number's { -4.5 } error function = -0.9999999998033839

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

Python math.comb() Method with Examples

math.comb() Method in Python:

The math. comb() method, also known as combinations, returns the number of ways to choose k unordered outcomes from n possibilities without repetition.

Note: It should be noted that the parameters passed in this method must be positive integers.

Syntax:

math.comb(n, k)

Parameters

n: This is Required. It is the positive integers of items from which to choose

k: This is Required. It is the positive integers of items to choose

Note:

  • It should be noted that if the value of k is greater than the value of n, the result will be 0.
  • A ValueError occurs if the parameters are negative. A TypeError occurs if the parameters are not integers.

Return Value:

Returns an integer value representing the total number of possible combinations.

Examples:

Example1:

Input:

Given n = 5
Given k = 3

Output:

The total number of combinations possible for the given n, k values = 10

Example2:

Input:

Given n = 6
Given k = 4

Output:

The total number of combinations possible for the given n, k values = 15

math.comb() Method with Examples in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the number of items from which to choose(n) as static input and store it in a variable.
  • Give the number of possibilities to choose(k) as static input and store it in another variable.
  • Pass the given n, k values as the arguments to the math.comb() function to get the total number of combinations possible.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword
import math
# Give the number of items from which to choose(n) as static input and
# store it in a variable.
gvn_n_valu = 5
# Give the number of possibilities to choose as static input and
# store it in another variable.
gvn_k_valu = 3
# Pass the given n, k values as the arguments to the math.comb() function to get
# the total number of combinations possible.
# Store it in another variable.
totl_combintns = math.comb(gvn_n_valu, gvn_k_valu)
# Print the above result.
print("The total number of combinations possible for the given n, k values = ", totl_combintns)

Output:

The total number of combinations possible for the given n, k values = 10
Note:
This function works only in latest versions like 3.8

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

Approach:

  • Import math module using the import keyword.
  • Give the number of items from which to choose(n) as user input using the int(input()) function and store it in a variable.
  • Give the number of possibilities to choose(k) as user input using the int(input()) function and store it in another variable.
  • Pass the given n, k values as the arguments to the math.comb() function to get the total number of combinations possible.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword
import math
# Give the number of items from which to choose(n) as user input using 
# the int(input()) function and store it in a variable.
gvn_n_valu = int(input("Enter some random number = "))
# Give the number of possibilities to choose(k) as user input using the int(input()) 
# function and store it in another variable.
gvn_k_valu = int(input("Enter some random number = "))
# Pass the given n, k values as the arguments to the math.comb() function to get
# the total number of combinations possible.
# Store it in another variable.
totl_combintns = math.comb(gvn_n_valu, gvn_k_valu)
# Print the above result.
print("The total number of combinations possible for the given n, k values = ", totl_combintns)

Output:

Enter some random number = 6
Enter some random number = 4
The total number of combinations possible for the given n, k values = 15

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

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 »