Python

Python cmath.atan() Method with Examples

cmath.atan() Method in Python:

The cmath.atan() method returns the complex number’s arc tangent.

There are primarily two types of branch cuts:

  1. Extend from 1j along the imaginary axis to ∞ j to the right.
  2. Extending from -1j to -∞ j to the left along the imaginary axis

Syntax:

cmath.atan(x)

Parameters

x: This is Required. A number used to calculate the arc tangent of

Return Value:

Returns a complex value that represents the complex number’s arc tangent.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) arc tangent value = 
(1.4483069952314644+0.15899719167999918j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) arc tangent value = 
(1.399284356584545+0.06706599664866984j)

Note: The above input format is for dynamic input.

cmath.atan() Method with Examples in Python

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.atan() method that returns the given complex number’s arc tangent value.
  • Store it in another variable.
  • Print the arc tangent value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.atan() method that
# returns the the given complex number's arc tangent value.
# Store it in another variable.
rslt = cmath.atan(complexnumb)
# Print the arc tangent value of the given complex number.
print("The given complex number's", complexnumb,
      "arc tangent value = ")
print(rslt)

Output:

The given complex number's (3+4j) arc tangent value = 
(1.4483069952314644+0.15899719167999918j)

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in a variable.
  • Pass the given complex number as an argument to the cmath.atan() method that returns the given complex number’s arc tangent value.
  • Store it in another variable.
  • Print the arc tangent value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.atan() method that
# returns the the given complex number's arc tangent value.
# Store it in another variable.
rslt = cmath.atan(complexnumb)
# Print the arc tangent value of the given complex number.
print("The given complex number's", complexnumb,
      "arc tangent value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) arc tangent value = 
(1.399284356584545+0.06706599664866984j)

Python cmath.atan() Method with Examples Read More »

Python cmath.asinh() Method with Examples

cmath.asinh() Method in Python:

The cmath.asinh() method returns a number’s inverse hyperbolic sine.

There are primarily two types of branch cuts:

  1. Extend from 1j along the imaginary axis to ∞ j to the right.
  2. Extending from -1j to -∞ j to the left along the imaginary axis

Syntax:

cmath.asinh(x)

Parameters

x: This is Required. The number used to calculate the inverse hyperbolic sine of

Return Value:

Returns a complex value that represents the complex number’s inverse hyperbolic sine.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) inverse hyperbolic sine value = 
(2.2999140408792695+0.9176168533514787j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) inverse hyperbolic sine value = 
(2.3830308809003258+0.374670804825527j)

Note: The above input format is for dynamic input.

cmath.asinh() Method with Examples in Python

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.asinh() method that returns the given complex number’s inverse hyperbolic sine value.
  • Store it in another variable.
  • Print the inverse hyperbolic sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.asinh() method that
# returns the given complex number's inverse hyperbolic sine value.
# Store it in another variable.
rslt = cmath.asinh(complexnumb)
# Print the inverse hyperbolic sine value of the given complex number.
print("The given complex number's", complexnumb,
      "inverse hyperbolic sine value = ")
print(rslt)

Output:

The given complex number's (3+4j) inverse hyperbolic sine value = 
(2.2999140408792695+0.9176168533514787j)

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in a variable.
  • Pass the given complex number as an argument to the cmath.asinh() method that returns the given complex number’s inverse hyperbolic sine value.
  • Store it in another variable.
  • Print the inverse hyperbolic sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.asinh() method that
# returns the given complex number's inverse hyperbolic sine value.
# Store it in another variable.
rslt = cmath.asinh(complexnumb)
# Print the inverse hyperbolic sine value of the given complex number.
print("The given complex number's", complexnumb,
      "inverse hyperbolic sine value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) inverse hyperbolic sine value = 
(2.3830308809003258+0.374670804825527j)

Python cmath.asinh() Method with Examples Read More »

Python cmath.asin() Method with Examples

cmath.asin() Method in Python:

The cmath.asin() method returns the complex number’s arc sine.

There are two types of branch cuts:

  1. Extends right from 1 to ∞ along the real axis.
  2. Extends left along the real axis from -1 to -∞

Syntax:

cmath.asin(x)

Parameters

x: This is Required. A number that can be used to calculate the arc sine of

Return Value:

Returns a complex value that represents the complex number’s arc sine.

Examples:

Example1:

Input:

Given Complex Number = 4+2j

Output:

The given complex number's (4+2j) arc sine value  = 
(1.096921548830143+2.183585216564564j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) arc sine value = 
(1.184231684275022+2.37054853731792j)

Note: The above input format is for dynamic input.

cmath.asin() Method with Examples in Python

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.asin() method that returns the given complex number’s arc sine value.
  • Store it in another variable.
  • Print the arc sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 4+2j
# Pass the given complex number as an argument to the cmath.asin() method that
# returns the given complex number's arc sine value.
# Store it in another variable.
rslt = cmath.asin(complexnumb)
# Print the arc sine value of the given complex number.
print("The given complex number's", complexnumb,
      "arc sine value  = ")
print(rslt)

Output:

The given complex number's (4+2j) arc sine value  = 
(1.096921548830143+2.183585216564564j)

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in a variable.
  • Pass the given complex number as an argument to the cmath.asin() method that returns the given complex number’s arc sine value.
  • Store it in another variable.
  • Print the arc sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)
# Pass the given complex number as an argument to the cmath.asin() method that
# returns the given complex number's arc sine value.
# Store it in another variable.
rslt = cmath.asin(complexnumb)
# Print the arc sine value of the given complex number.
print("The given complex number's", complexnumb,
      "arc sine value  = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) arc sine value = 
(1.184231684275022+2.37054853731792j)

Python cmath.asin() Method with Examples Read More »

Python cmath.acosh() Method with Examples

cmath.acosh() Method in Python:

The cmath.acosh() method returns the complex number’s inverse hyperbolic cosine.

There is one branch cut:

Extending left along the real axis from 1 to -∞ , continuous from above

Syntax:

cmath.acosh(x)

Parameters

x: This is Required. The number used to calculate the inverse hyperbolic cosine of

Return Value:

Returns a complex value that represents a number’s inverse hyperbolic arc cosine.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) inverse hyperbolic cosine value  = 
(2.305509031243477+0.9368124611557198j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) inverse hyperbolic cosine value = 
(2.37054853731792+0.38656464251987466j)

Note: The above input format is for dynamic input.

cmath.acosh() Method with Examples in Python

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.acosh() method that returns the given complex number’s inverse hyperbolic cosine value.
  • Store it in another variable.
  • Print the inverse hyperbolic cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
gvn_numb = 3+4j
# Pass the given complex number as an argument to the cmath.acosh() method that
# returns the given complex number's inverse hyperbolic cosine value.
# Store it in another variable.
rslt = cmath.acosh(gvn_numb)
# Print the inverse hyperbolic cosine value of the given complex number.
print("The given complex number's", gvn_numb,
      "inverse hyperbolic cosine value  = ")
print(rslt)

Output:

The given complex number's (3+4j) inverse hyperbolic cosine value  = 
(2.305509031243477+0.9368124611557198j)

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in a variable.
  • Pass the given complex number as an argument to the cmath.acosh() method that returns the given complex number’s inverse hyperbolic cosine value.
  • Store it in another variable.
  • Print the inverse hyperbolic cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)
# Pass the given complex number as an argument to the cmath.acosh() method that
# returns the given complex number's inverse hyperbolic cosine value.
# Store it in another variable.
rslt = cmath.acosh(complexnumb)
# Print the inverse hyperbolic cosine value of the given complex number.
print("The given complex number's", complexnumb,
      "inverse hyperbolic cosine value  = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) inverse hyperbolic cosine value = 
(2.37054853731792+0.38656464251987466j)

Python cmath.acosh() Method with Examples Read More »

Python cmath.acos() Method with Examples

cmath.acos() Method in Python:

The cmath.acos() method returns the complex number’s arc cosine.

There are two types of branch cuts:

  1. Extends to the right from 1 to ∞ along the real axis.
  2. Extends to the left from -1 to -∞ along the real axis.

Syntax:

cmath.acos(x)

Parameters

x: This is Required. It is a number that can be used to calculate the arc cosine of

Return Value:

Returns a complex value that represents a number’s arc cosine.

If the return value is expressed as a real number, it has an imaginary part of 0.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j)  arc cosine value = 
(0.9368124611557198-2.305509031243477j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j)  arc cosine value = 
(0.38656464251987466-2.37054853731792j)

Note: The above input format is for dynamic input.

cmath.acos() Method with Examples in Python

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.acos() method that returns the given complex number’s arc cosine value.
  • Store it in another variable.
  • Print the arc cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
gvn_numb = 3+4j
# Pass the given complex number as an argument to the cmath.acos() method that
# returns the given complex number's arc cosine value.
# Store it in another variable.
rslt = cmath.acos(gvn_numb)
# Print the arc cosine value of the given complex number.
print("The given complex number's", gvn_numb, " arc cosine value = ")
print(rslt)

Output:

The given complex number's (3+4j)  arc cosine value = 
(0.9368124611557198-2.305509031243477j)

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

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in a variable.
  • Pass the given complex number as an argument to the cmath.acos() method that returns the given complex number’s arc cosine value.
  • Store it in another variable.
  • Print the arc cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)
# Pass the given complex number as an argument to the cmath.acos() method that
# returns the given complex number's arc cosine value.
# Store it in another variable.
rslt = cmath.acos(complexnumb)
# Print the arc cosine value of the given complex number.
print("The given complex number's", complexnumb, " arc cosine value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) arc cosine value = 
(0.38656464251987466-2.37054853731792j)

Python cmath.acos() Method with Examples Read More »

Python math.degrees() Method with Examples

math.degrees() Method in Python:

The math. degrees() method converts a radian angle to a degree angle.

PI (3.14… ) radians are equal to 180 degrees, so 1 radian equals 57.2957795 degrees.

Syntax:

math.degrees(x)

Parameters

x: This is Required. It is a number. A radian value that can be converted into a degree value.

If the parameter is not a number, a TypeError is returned.

Return Value:

Returns a float value indicating the value in degrees.

Examples:

Example1:

Input:

Given Angle = 7.5

Output:

The given angle{ 7.5 } in degrees =  429.7183463481174

Example2:

Input:

Given Angle = 14

Output:

The given angle{ 14 } in degrees =  802.1409131831525

math.degrees() 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 angle as an argument to the math.degrees() function that converts the given radian angle to a degree angle.
  • Store it in another variable.
  • Print the given angle in degrees.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword
import math
# Give the number(angle) as static input and store it in a variable.
gvn_angl = 7.5
# Pass the given angle as an argument to the math.degrees() function that
# converts the given radian angle to a degree angle.
# Store it in another variable.
degre_angl = math.degrees(gvn_angl)
# Print the given angle in degrees.
print("The given angle{", gvn_angl, "} in degrees = ", degre_angl)

Output:

The given angle{ 7.5 } in degrees =  429.7183463481174

Similarly, try for other numbers.

import math
gvn_angl = 4
degre_angl = math.degrees(gvn_angl)
print("The given angle{", gvn_angl, "} in degrees = ", degre_angl)

Output:

The given angle{ 4 } in degrees =  229.1831180523293

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

Approach:

  • Import math module using the import keyword.
  • Give the number (angle) as user input using the float(input()) function and store it in a variable.
  • Pass the given angle as an argument to the math.degrees() function that converts the given radian angle to a degree angle.
  • Store it in another variable.
  • Print the given angle in degrees.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword
import math
# Give the number(angle) as user input using the float(input()) function 
# and store it in a variable.
gvn_angl =  float(input("Enter some random number = "))
# Pass the given angle as an argument to the math.degrees() function that
# converts the given radian angle to a degree angle.
# Store it in another variable.
degre_angl = math.degrees(gvn_angl)
# Print the given angle in degrees.
print("The given angle{", gvn_angl, "} in degrees = ", degre_angl)

Output:

Enter some random number = -10.5
The given angle{ -10.5 } in degrees = -601.6056848873644

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

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 »