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.