Python

Python Program for gamma() Function

In the previous article, we have discussed Python Program for tanh() Function
gamma() Function in Python:

The gamma function at a number is returned by the math.gamma() method.

Tip: Use the math.lgamma() method to find the log gamma value of a number.

Gamma Function’s Mathematical Formula:

Math.gamma(Number) = (Number – 1)!

Syntax:

math.gamma(x)

Parameters:

x: This is required. It is a number for which the gamma function must be found. If the number is a negative integer, a ValueError is returned. It throws a TypeError if it is not a number.

Return Value:

Returns a float value that represents the gamma function at x.

  • The gamma function returns the output if the number argument is a positive integer, positive or negative decimal.
  • The gamma function returns ValueError if the number argument is a negative integer.
  • If it’s not a number, the gamma function throws a TypeError.

Examples:

Example1:

Input:

Given first number = 6
Given second number = 2

Output:

The result after applying gamma() function on above given first number 6  =  120.0
The result after applying gamma() function on above given second number 2  =  1.0

Example2:

Input:

Given first number = -0.5
Given second number = 3

Output:

The result after applying gamma() function on above given first number -0.5  =  -3.544907701811032
The result after applying gamma() function on above given second number 3  =  2.0

Program for gamma() Function 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 first number as static input and store it in another variable.
  • Apply math.gamma() function to the given first number to get the gamma value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.gamma() function to the given list element and print it.
  • The Exit of 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, -1.5, 3, 0.4]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 6
# Apply math.gamma() function to the given first number to get the gamma value
# of a given number.
# Store it in another variable.
fnl_rslt = math.gamma(gvn_numb1)
# Print the above result
print("The result after applying gamma() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 2
print("The result after applying gamma() function on above given second number",
      gvn_numb2, " = ", math.gamma(gvn_numb2))
# Apply math.gamma() function to the given list element and print it.
print(
    "The result after applying gamma() function on given list element gvnlst[1] = ", math.gamma(gvn_lst[1]))

Output:

The result after applying gamma() function on above given first number 6  =  120.0
The result after applying gamma() function on above given second number 2  =  1.0
The result after applying gamma() function on given list element gvnlst[1] =  2.3632718012073544

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.gamma() function to the given first number to get the gamma value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.gamma() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.gamma() function to the given first number to get the gamma value
# of a given number.
# Store it in another variable.
fnl_rslt = math.gamma(gvn_numb1)
# Print the above result
print("The result after applying gamma() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying gamma() function on above given second number",
      gvn_numb2, " = ", math.gamma(gvn_numb2))
# Apply math.gamma() function to the given list element and print it.
print(
    "The result after applying gamma() function on given list element gvnlst[2] = ", math.gamma(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 1 3 2 5 7
Enter some random number = 1.5
The result after applying gamma() function on above given first number 1.5 = 0.886226925452758
Enter some random number = 3
The result after applying gamma() function on above given second number 3.0 = 2.0
The result after applying gamma() function on given list element gvnlst[2] = 1.0

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

Python Program for gamma() Function Read More »

Python Program for tanh() Function

In the previous article, we have discussed Python Program for sinh() Function
tanh() Function in Python:

The math.tanh() method returns a number’s hyperbolic tangent.

Syntax:

math.tanh(x)

Parameters:

x: This is Required. It is a number for which the hyperbolic tangent must be calculated. If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value representing a number’s hyperbolic tangent.

  • The tanh() function returns the hyperbolic Tangent value if the number argument is positive or negative.
  • The tanh()  function returns TypeError if it is not a number.

Examples:

Example1:

Input:

Given first number = 7
Given second number = 5.5

Output:

The result after applying tanh() function on above given first number 7  =  0.9999983369439447
The result after applying tanh() function on above given second number 5.5  =  0.9999665971563038

Example2:

Input:

Given first number = 2
Given second number = 1

Output:

The result after applying tanh() function on above given first number 2  =  0.9640275800758169
The result after applying tanh() function on above given second number 1  =  0.7615941559557649

Program for cosh() Function 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 first number as static input and store it in another variable.
  • Apply math.tanh() function to the given first number to get the hyperbolic tangent value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.tanh() function to the given list element and print it.
  • The Exit of 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 = [-1, 1, 3, 0.8]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 7
# Apply math.tanh() function to the given first number to get the hyperbolic
# tangent value of a given number.
# Store it in another variable.
fnl_rslt = math.tanh(gvn_numb1)
# Print the above result
print("The result after applying tanh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 5.5
print("The result after applying tanh() function on above given second number",
      gvn_numb2, " = ", math.tanh(gvn_numb2))
# Apply math.tanh() function to the given list element and print it.
print(
    "The result after applying tanh() function on given list element gvnlst[1] = ", math.tanh(gvn_lst[1]))

Output:

The result after applying tanh() function on above given first number 7  =  0.9999983369439447
The result after applying tanh() function on above given second number 5.5  =  0.9999665971563038
The result after applying tanh() function on given list element gvnlst[1] =  0.7615941559557649

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.tanh() function to the given first number to get the hyperbolic tangent value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.tanh() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.tanh() function to the given first number to get the hyperbolic
# tangent value of a given number.
# Store it in another variable.
fnl_rslt = math.tanh(gvn_numb1)
# Print the above result
print("The result after applying tanh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying tanh() function on above given second number",
      gvn_numb2, " = ", math.tanh(gvn_numb2))
# Apply math.tanh() function to the given list element and print it.
print(
    "The result after applying tanh() function on given list element gvnlst[2] = ", math.tanh(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 2 3 10 4 1
Enter some random number = 2
The result after applying tanh() function on above given first number 2.0 = 0.9640275800758169
Enter some random number = 1
The result after applying tanh() function on above given second number 1.0 = 0.7615941559557649
The result after applying tanh() function on given list element gvnlst[2] = 0.9999999958776927

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

Python Program for tanh() Function Read More »

Python Program for sinh() Function

In the previous article, we have discussed Python Program for cosh() Function
sinh() Function in Python:

The sinh() function in Python computes the Trigonometric Hyperbolic Sine for the given expression.

Syntax:

math.sinh(x)

Parameters:

x: This is Required. It is a number for which the hyperbolic sine must be calculated. If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value representing a number’s hyperbolic sine.

  • The sinh() function returns the Sine value if the number argument is positive or negative.
  • The sinh function returns TypeError if the number argument is not a number.

Examples:

Example1:

Input:

Given first number = 0.3
Given second number = 1

Output:

The result after applying sinh() function on above given first number 0.3  =  0.3045202934471426
The result after applying sinh() function on above given second number 1  =  1.1752011936438014

Example2:

Input:

Given first number = 3
Given second number = 5.5

Output:

The result after applying sinh() function on above given first number 3  =  10.017874927409903
The result after applying sinh() function on above given second number 5.5  =  122.34392274639096

Program for cosh() Function 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 first number as static input and store it in another variable.
  • Apply math.sinh() function to the given first number to get the hyperbolic sine value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.sinh() function to the given list element and print it.
  • The Exit of 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 = [-1, -3, 1, 0.8]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 0.3
# Apply math.sinh() function to the given first number to get the hyperbolic
# sine value of a given number.
# Store it in another variable.
fnl_rslt = math.sinh(gvn_numb1)
# Print the above result
print("The result after applying sinh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 1
print("The result after applying sinh() function on above given second number",
      gvn_numb2, " = ", math.sinh(gvn_numb2))
# Apply math.sinh() function to the given list element and print it.
print(
    "The result after applying sinh() function on given list element gvnlst[1] = ", math.sinh(gvn_lst[1]))

Output:

The result after applying sinh() function on above given first number 0.3  =  0.3045202934471426
The result after applying sinh() function on above given second number 1  =  1.1752011936438014
The result after applying sinh() function on given list element gvnlst[1] =  -10.017874927409903

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.sinh() function to the given first number to get the hyperbolic sine value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.sinh() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.sinh() function to the given first number to get the hyperbolic
# sine value of a given number.
# Store it in another variable.
fnl_rslt = math.sinh(gvn_numb1)
# Print the above result
print("The result after applying sinh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying sinh() function on above given second number",
      gvn_numb2, " = ", math.sinh(gvn_numb2))
# Apply math.sinh() function to the given list element and print it.
print(
    "The result after applying sinh() function on given list element gvnlst[2] = ", math.sinh(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 1 1.5 2.5 3
Enter some random number = 3
The result after applying sinh() function on above given first number 3.0 = 10.017874927409903
Enter some random number = 5.5
The result after applying sinh() function on above given second number 5.5 = 122.34392274639096
The result after applying sinh() function on given list element gvnlst[2] = 6.0502044810397875

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

Python Program for sinh() Function Read More »

Python Program for cosh() Function

In the previous article, we have discussed Python Program for isalpha() Function
cosh() Function in Python:

The math.cosh() method calculates a number’s hyperbolic cosine (equivalent to (exp(number) + exp(-number)) / 2).

Syntax:

math.cosh(x)

Parameters:

x: This is Required. It is a number for which the hyperbolic cosine must be calculated. If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value representing a number’s hyperbolic cosine.

  • The cosh function returns the Hyperbolic Cosine value if the number argument is positive or negative.
  • The cosh function returns TypeError if the number argument is not a number.

Examples:

Example1:

Input:

Given first number = 0.4
Given second number = 1

Output:

The result after applying cosh() function on above given first number 0.4  =  1.081072371838455
The result after applying cosh() function on above given second number 1  =  1.5430806348152437

Example2:

Input:

Given first number = 6.5
Given second number = 2

Output:

The result after applying cosh() function on above given first number 6.5  =  332.5715682417774
The result after applying cosh() function on above given second number 2  =  3.7621956910836314

Program for cosh() Function 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 first number as static input and store it in another variable.
  • Apply math.cosh() function to the given first number to get the hyperbolic cosine value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.cosh() function to the given list element and print it.
  • The Exit of 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 = [0.5, 0.2, 1, 0.8]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 0.4
# Apply math.cosh() function to the given first number to get the hyperbolic
# cosine value of a given number.
# Store it in another variable.
fnl_rslt = math.cosh(gvn_numb1)
# Print the above result
print("The result after applying cosh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 1
print("The result after applying cosh() function on above given second number",
      gvn_numb2, " = ", math.cosh(gvn_numb2))
# Apply math.cosh() function to the given list element and print it.
print(
    "The result after applying cosh() function on given list element gvnlst[1] = ", math.cosh(gvn_lst[1]))

Output:

The result after applying cosh() function on above given first number 0.4  =  1.081072371838455
The result after applying cosh() function on above given second number 1  =  1.5430806348152437
The result after applying cosh() function on given list element gvnlst[1] =  1.020066755619076

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.cosh() function to the given first number to get the hyperbolic cosine value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.cosh() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.cosh() function to the given first number to get the hyperbolic
# cosine value of a given number.
# Store it in another variable.
fnl_rslt = math.cosh(gvn_numb1)
# Print the above result.
print("The result after applying cosh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 =  float(input("Enter some random number = "))
print("The result after applying cosh() function on above given second number",
      gvn_numb2, " = ", math.cosh(gvn_numb2))
# Apply math.cosh() function to the given list element and print it.
print(
    "The result after applying cosh() function on given list element gvnlst[2] = ", math.cosh(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 2 3 4 1 5
Enter some random number = 6.5
The result after applying cosh() function on above given first number 6.5 = 332.5715682417774
Enter some random number = 2
The result after applying cosh() function on above given second number 2.0 = 3.7621956910836314
The result after applying cosh() function on given list element gvnlst[2] = 27.308232836016487

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

Python Program for cosh() Function Read More »

Python Program for acos() Function

In the previous article, we have discussed Python Program for log() Function
acos() Function in Python:

The math.acos() method returns a number’s arc cosine value. Python acos, also known as Arc cosine, is the inverse of a cosine.

Note: It should be noted that the parameter passed to math.acos() must be between -1 and 1.

Tip: The value of PI will be returned by math.acos(-1).

Syntax:

math.acos(x)

Parameters:

x: This is required. It is a number between -1 and 1. If x is not a number, a TypeError is returned.

Return Value:

Returns a float value which represents a number’s arc cosine.

  • The acos() function returns the Arc Cosine value if the number argument is positive or negative.
  • The acos() function returns TypeError if it is not a number.
  • If it is not between -1 and 1, the acos() function returns ValueError.

Examples:

Example1:

Input:

Given first number = 0.5
Given second number = 1

Output:

The result after applying acos() function on above given first number 0.5  =  1.0471975511965979
The result after applying acos() function on above given second number 1  =  0.0

Example2:

Input:

Given first number = 0.3
Given second number = -0.2

Output:

The result after applying acos() function on above given first number 0.3  =  1.2661036727794992
The result after applying acos() function on above given second number -0.2  =  1.7721542475852274

Program for acos() Function 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 first number as static input and store it in another variable.
  • Apply math.acos() function to the given first number to get the arc cosine value (inverse of cosine) of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.acos() function to the given list element and print it.
  • The Exit of 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 = [0.5, 0.2, 1, 0.8]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 0.5
# Apply math.acos() function to the given first number to get arc cosine value
# (inverse of cosine) of a given number.
# Store it in another variable.
fnl_rslt = math.acos(gvn_numb1)
print("The result after applying acos() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 1
print("The result after applying acos() function on above given second number",
      gvn_numb2, " = ", math.acos(gvn_numb2))
# Apply math.acos() function to the given list element and print it.
print(
    "The result after applying acos() function on given list element gvnlst[1] = ", math.acos(gvn_lst[1]))

Output:

The result after applying acos() function on above given first number 0.5  =  1.0471975511965979
The result after applying acos() function on above given second number 1  =  0.0
The result after applying acos() function on given list element gvnlst[1] =  1.369438406004566

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.acos() function to the given first number to get the arc cosine value (inverse of cosine) of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.acos() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.acos() function to the given first number to get arc cosine value
# (inverse of cosine) of a given number.
# Store it in another variable.
fnl_rslt = math.acos(gvn_numb1)
print("The result after applying acos() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying acos() function on above given second number",
      gvn_numb2, " = ", math.acos(gvn_numb2))
# Apply math.acos() function to the given list element and print it.
print(
    "The result after applying acos() function on given list element gvnlst[2] = ", math.acos(gvn_lst[2]))


Output:

Enter some random List Elements separated by spaces = 1 2 0.3 0.5
Enter some random number = 0.38
The result after applying acos() function on above given first number 0.38 = 1.181000030320636
Enter some random number = 0.6
The result after applying acos() function on above given second number 0.6 = 0.9272952180016123
The result after applying acos() function on given list element gvnlst[2] = 1.2661036727794992

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

Python Program for acos() Function Read More »

Python Program for expm1() Function

In the previous article, we have discussed Python Program for exp() Function
expm1() Function in Python:

Ex – 1 is returned by the math.expm1() method.

‘E’ is the natural logarithmic system’s base (approximately 2.718282), and x is the number passed to it.

This function is more precise than using math. exp() and subtracting 1.

Syntax:

math.expm1(x)

Parameters:

x: This is required. It is a number. The exponent is specified.

Return Value:

It returns a value float value which represents  Ex – 1.

Examples:

Example1:

Input:

Given Number = 3

Output:

The result after applying expm1() function on above given number 3  =  19.085536923187668

Example2:

Input:

Given Number = 5.5

Output:

The result after applying expm1() function on above given number 5.5  =  243.69193226422038

Program for expm1() Function 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 first number as static input and store it in another variable.
  • Apply math.expm1() function to the given first number to calculate the value E raised to the power of given number-1.(E =approximately 2.718282)
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.expm1() function to the given list element and print it.
  • The Exit of 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 = [9, 4, 5, 2, 1]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 3
# Apply math.expm1() function to the given first number to calculate the value
# E raised to the power of given number-1.(E =approximately 2.718282)
# Store it in another variable.
fnl_rslt = math.expm1(gvn_numb1)
print("The result after applying expm1() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 5.5
print("The result after applying expm1() function on above given second number",
      gvn_numb2, " = ", math.expm1(gvn_numb2))
# Apply math.expm1() function to the given list element and print it.
print(
    "The result after applying expm1() function on given list element gvnlst[1] = ", math.expm1(gvn_lst[1]))

Output:

The result after applying expm1() function on above given first number 3  =  19.085536923187668
The result after applying expm1() function on above given second number 5.5  =  243.69193226422038
The result after applying expm1() function on given list element gvnlst[1] =  53.598150033144236

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.expm1() function to the given first number to calculate the value E raised to the power of given number-1.(E =approximately 2.718282)
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.expm1() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.expm1() function to the given first number to calculate the value
# E raised to the power of given number-1.(E =approximately 2.718282)
# Store it in another variable.
fnl_rslt = math.expm1(gvn_numb1)
print("The result after applying expm1() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying expm1() function on above given second number",
      gvn_numb2, " = ", math.expm1(gvn_numb2))
# Apply math.expm1() function to the given list element and print it.
print(
    "The result after applying expm1() function on given list element gvnlst[2] = ", math.expm1(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 46 2 1 80 2
Enter some random number = 4
The result after applying expm1() function on above given first number 4.0 = 53.598150033144236
Enter some random number = 3.5
The result after applying expm1() function on above given second number 3.5 = 32.11545195869231
The result after applying expm1() function on given list element gvnlst[2] = 1.718281828459045

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

Python Program for expm1() Function Read More »

Python Program for exp() Function

In the previous article, we have discussed Python Program for modf() Function
exp() Function in Python:

The method math.exp() returns E raised to the power of x (Ex).

‘E’ is the natural logarithmic system’s base (approximately 2.718282), and x is the number passed to it.

Syntax:

math.exp(x)

Parameters:

x: This is required. The exponent is specified by this.

Return Value:

A float value representing ‘E’ raised to the power of x is returned as the return value.

Examples:

Example1:

Input:

Given Number = 5

Output:

The result after applying exp() function on above given number 5  =  148.4131591025766

Example2:

Input:

Given Number = 7.5

Output:

The result after applying exp() function on above given number 7.5  =  1808.0424144560632

Program for exp() Function 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 first number as static input and store it in another variable.
  • Apply math.exp() function to the given first number to calculate the value E raised to the power of given number.(E =approximately 2.718282)
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.exp() function to the given list element and print it.
  • The Exit of 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 = [7, 4, 6, 14, 10]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 5
# Apply math.exp() function to the given first number to calculate the value
# E raised to the power of given number.(E =approximately 2.718282)
# Store it in another variable.
fnl_rslt = math.exp(gvn_numb1)
print("The result after applying exp() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 7.5
print("The result after applying exp() function on above given second number",
      gvn_numb2, " = ", math.exp(gvn_numb2))
# Apply math.exp() function to the given list element and print it.
print(
    "The result after applying exp() function on given list element gvnlst[1] = ", math.exp(gvn_lst[1]))

Output:

The result after applying exp() function on above given first number 5  =  148.4131591025766
The result after applying exp() function on above given second number 7.5  =  1808.0424144560632
The result after applying exp() function on given list element gvnlst[1] =  54.598150033144236

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.exp() function to the given first number to calculate the value E raised to the power of the given number.(E =approximately 2.718282)
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.exp() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.exp() function to the given first number to calculate the value
# E raised to the power of given number.(E =approximately 2.718282)
# Store it in another variable.
fnl_rslt = math.exp(gvn_numb1)
print("The result after applying exp() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying exp() function on above given second number",
      gvn_numb2, " = ", math.exp(gvn_numb2))
# Apply math.exp() function to the given list element and print it.
print(
    "The result after applying exp() function on given list element gvnlst[3] = ", math.exp(gvn_lst[3]))

Output:

Enter some random List Elements separated by spaces = 1 3 5 6 8
Enter some random number = 3.5
The result after applying exp() function on above given first number 3.5 = 33.11545195869231
Enter some random number = 9
The result after applying exp() function on above given second number 9.0 = 8103.083927575384
The result after applying exp() function on given list element gvnlst[3] = 403.4287934927351

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.

Python Program for exp() Function Read More »

Python Program for log() Function

In the previous article, we have discussed Python Program for expm1() Function
log() Function in Python:

The math.log() method returns the natural logarithm of a number or the base logarithm of a number.

Syntax:

math.log(x, base)

Parameters:

x: This is required. It is a number. It 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.

base: This is optional. The logarithmic base that should be used. ‘e’ is the default.

Return Value:

Returns a float value corresponding to the natural logarithm of a number or the logarithm of a number to base.

  • The log function returns the output if the number argument is positive.
  • If the number is negative or zero, the log function throws a ValueError.
  • If it is not a number, the log function throws a TypeError.

Examples:

Example1:

Input:

Given first number = 1
Given second number = 6

Output:

The result after applying log() function on above given first number 1  =  0.0
The result after applying log() function on above given second number 6  =  1.791759469228055

Example2:

Input:

Given first number = 7
Given second number = 9

Output:

The result after applying log() function on above given first number 7  =  1.9459101490553132
The result after applying log() function on above given second number 9  =  2.1972245773362196

Program for log() Function 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 first number as static input and store it in another variable.
  • Apply math.log() function to the given first number to get the natural logarithm or the base logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply the math.log() function to the given list element and print it.
  • The Exit of 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 = [10, 5, 20, 25, 12]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 1
# Apply math.log() function to the given first number to get the natural logarithm
# or the base logarithm of a given number.
# Store it in another variable.
fnl_rslt = math.log(gvn_numb1)
print("The result after applying log() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 6
print("The result after applying log() function on above given second number",
      gvn_numb2, " = ", math.log(gvn_numb2))
# Apply math.log() function to the given list element and print it.
print(
    "The result after applying log() function on given list element gvnlst[1] = ", math.log(gvn_lst[1]))

Output:

The result after applying log() function on above given first number 1  =  0.0
The result after applying log() function on above given second number 6  =  1.791759469228055
The result after applying log() function on given list element gvnlst[1] =  1.6094379124341003

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.log() function to the given first number to get the natural logarithm or the base logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply the math.log() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.log() function to the given first number to get the natural logarithm
# or the base logarithm of a given number.
# Store it in another variable.
fnl_rslt = math.log(gvn_numb1)
print("The result after applying log() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying log() function on above given second number",
      gvn_numb2, " = ", math.log(gvn_numb2))
# Apply math.log() function to the given list element and print it.
print(
    "The result after applying log() function on given list element gvnlst[2] = ", math.log(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 4 5 3 1 2
Enter some random number = 7
The result after applying log() function on above given first number 7.0 = 1.9459101490553132
Enter some random number = 9
The result after applying log() function on above given second number 9.0 = 2.1972245773362196
The result after applying log() function on given list element gvnlst[2] = 1.0986122886681098

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

Python Program for log() Function Read More »

Python Program for modf() Function

In the previous article, we have discussed Python Program for How To Use While Not
modf() Function in Python:

The modf() math function in Python is used to divide a given value into two arguments. Python modf() function takes a fractional part as the first argument and an integer value as the second argument.

Syntax:

math.modf(Number);

Parameters

Number: It could be a number or a valid numerical expression.

Return Value:

  • If the number argument is positive or negative, the modf() function returns Zero.
  • If the number argument is positive or negative decimal, the modf() function returns decimal values for the first argument and an integer value for the second.
  • If it is not a number, the modf() function throws a TypeError.

For example:

Let the number = 3.75

Then the modf() gives 0.75 as the first argument and 3.00 as the second argument.

Therefore , output = (0.75, 3.00)

Examples:

Example1:

Input:

Given Number = 4.75

Output:

The result after applying modf() function on above given number 4.75  =  (0.75, 4.0)

Example2:

Input:

Given Number = 15

Output:

The result after applying modf() function on above given number 15 = (0.0, 15.0)

Program for modf() Function 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 first number as static input and store it in another variable.
  • Apply math.modf() function to the given first number to divide the given value into arguments(fractional part as the first argument and an integer value as the second argument).
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.modf() function to the given list element and print it.
  • The Exit of 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 = [7, 4.65, 6.5, 14, 10]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 4.75
# Apply math.modf() function to the given first number to divide the given value
# into arguments(fractional part as the first argument and an integer value
# as the second argument).
# Store it in another variable.
fnl_rslt = math.modf(gvn_numb1)
print("The result after applying modf() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 15
print("The result after applying modf() function on above given second number",
      gvn_numb2, " = ", math.modf(gvn_numb2))
# Apply math.modf() function to the given list element and print it.
print(
    "The result after applying modf() function on given list element gvnlst[1] = ", math.modf(gvn_lst[1]))

Output:

The result after applying modf() function on above given first number 4.75  =  (0.75, 4.0)
The result after applying modf() function on above given second number 15  =  (0.0, 15.0)
The result after applying modf() function on given list element gvnlst[1] =  (0.6500000000000004, 4.0)

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.modf() function to the given first number to divide the given value into arguments(fractional part as the first argument and an integer value as the second argument).
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.modf() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.modf() function to the given first number to divide the given value
# into arguments(fractional part as the first argument and an integer value
# as the second argument).
# Store it in another variable.
fnl_rslt = math.modf(gvn_numb1)
print("The result after applying modf() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying modf() function on above given second number",
      gvn_numb2, " = ", math.modf(gvn_numb2))
# Apply math.modf() function to the given list element and print it.
print(
    "The result after applying modf() function on given list element gvnlst[3] = ", math.modf(gvn_lst[3]))

Output:

Enter some random List Elements separated by spaces = 12 13 14.674 30.238
Enter some random number = 32.49
The result after applying modf() function on above given first number 32.49 = (0.490000000000002, 32.0)
Enter some random number = 59.759
The result after applying modf() function on above given second number 59.759 = (0.7590000000000003, 59.0)
The result after applying modf() function on given list element gvnlst[3] = (0.23799999999999955, 30.0)

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.

Python Program for modf() Function Read More »

Python Program for sin() Function

In the previous article, we have discussed Python Program for cos() Function
sin() Function in Python:

The sine of a number is returned by the math.sin() method.

The Sine function’s mathematical formula is:

sin(A) = length of opposite side/length of hypotenuse

Syntax:

math.sin(x)

Parameters:

x: This is Required. It is the number to calculate the sine of. If the value is not a number, a TypeError is returned.

Return value:

Returns a float value ranging from -1 to 1 that represents the sine of an angle.

Examples:

Example1:

Input:

Given first number (angle) = 15  
Given second number (angle) = 1

Output:

The result after applying sin() function on above given first number 15  =  0.6502878401571168
The result after applying sin() function on above given second number 1  =  0.8414709848078965

Example2:

Input:

Given first number (angle) = 10
Given second number (angle) = 4

Output:

The result after applying sin() function on above given first number 10  =  -0.5440211108893698
The result after applying sin() function on above given second number 4  =  -0.7568024953079282

Program for sin() Function 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 first number(angle) as static input and store it in another variable.
  • Apply math.sin() function to the given first number to get the sine value of a given number (angle).
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.sin() function to the given list element and print it.
  • The Exit of 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 = [0.92, 2, -0.4, 0.75]
# Give the first number (angle) as static input and store it in another variable.
gvn_numb1 = 15
# Apply math.sin() function to the given first number to get the sine value
# of a given number (angle).
# Store it in another variable.
fnl_rslt = math.sin(gvn_numb1)
print("The result after applying sin() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 1
print("The result after applying sin() function on above given second number",
      gvn_numb2, " = ", math.sin(gvn_numb2))
# Apply math.sin() function to the given list element and print it.
print(
    "The result after applying sin() function on given list element gvnlst[1] = ", math.sin(gvn_lst[1]))

Output:

The result after applying sin() function on above given first number 15  =  0.6502878401571168
The result after applying sin() function on above given second number 1  =  0.8414709848078965
The result after applying sin() function on given list element gvnlst[1] =  0.9092974268256817

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 first number (angle) as user input using the float(input()) function and store it in a variable.
  • Apply math.sin() function to the given first number to get the sine value of a given number (angle).
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.sin() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number(angle) as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.sin() function to the given first number to get the sine value
# of a given number (angle).
# Store it in another variable.
fnl_rslt = math.sin(gvn_numb1)
print("The result after applying sin() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying sin() function on above given second number",
      gvn_numb2, " = ", math.sin(gvn_numb2))
# Apply math.sin() function to the given list element and print it.
print(
    "The result after applying sin() function on given list element gvnlst[2] = ", math.sin(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 3 2 6 2 1
Enter some random number = 8
The result after applying sin() function on above given first number 8.0 = 0.9893582466233818
Enter some random number = -5
The result after applying sin() function on above given second number -5.0 = 0.9589242746631385
The result after applying sin() function on given list element gvnlst[2] = -0.27941549819892586

Note: To find the sine of degrees, first convert it to radians using math.radians() method.

For Example:

Approach:

  • Import math module using the import keyword.
  • Give the first number(angle) as static input and store it in another variable.
  • Get the angle in degree by first converting the given angle into radians using math. radians() method and then apply math.sin() method to it.
  • Print the given angle in degrees.
  • Similarly, do the same for another angle.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the first number(angle) as static input and store it in another variable.
gvn_numb1 = 45
# Get the angle in degree by first converting the given angle into radians using
# the math. radians() method and then apply math.sin() method to it.
angl_in_degre = math.sin(math.radians(gvn_numb1))
# Print the given angle in degrees.
print("The sin value of the given angle",
      gvn_numb1, " in degrees = ", angl_in_degre)
# similarly do the same for other angle
gvn_numb2 = 60
angl_in_degre = math.sin(math.radians(gvn_numb2))
print("The sin value of the given angle",
      gvn_numb2, " in degrees = ", angl_in_degre)

Output:

The sin value of the given angle 45  in degrees =  0.7071067811865475
The sin value of the given angle 60  in degrees =  0.8660254037844386

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.

 

Python Program for sin() Function Read More »