Author name: Vikram Chiluka

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 »

Python Program for atan() Function

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

The arc tangent of a number (x) is returned as a numeric value between -PI/2 and PI/2 radians by the math.atan() method.

Arc tangent is also defined as an inverse tangent function of x, where x is the value to be calculated for the arc tangent.

Syntax:

math.atan(x)

Parameters:

x: This is Required. It is a number that is either positive or negative. If x is not a number, TypeError is returned.

Return Value:

It returns a float value ranging from -PI/2 to PI/2 representing a number’s arc tangent.

The Arc Tangent value is returned if the number argument is positive or negative.
The atan() function throws a TypeError if the number argument is not a number.

Examples:

Example1:

Input:

Given first number = 0.7
Given second number = 1

Output:

The result after applying atan() function on above given first number 0.7  =  0.6107259643892086
The result after applying atan() function on above given second number 1  =  0.7853981633974483

Example2:

Input:

Given first number = -1
Given second number = 0.3

Output:

The result after applying atan() function on above given first number -1  =  -0.7853981633974483
The result after applying atan() function on above given second number 0.3  =  0.2914567944778671

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

Output:

The result after applying atan() function on above given first number 0.7  =  0.6107259643892086
The result after applying atan() function on above given second number 1  =  0.7853981633974483
The result after applying atan() function on given list element gvnlst[1] =  -0.2914567944778671

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.atan() function to the given first number to get the arc tangent value (inverse of tangent ) of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.atan() 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.atan() function to the given first number to get arc tangent value
# (inverse of tangent) of a given number.
# Store it in another variable.
fnl_rslt = math.atan(gvn_numb1)
print("The result after applying atan() 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 atan() function on above given second number",
      gvn_numb2, " = ", math.atan(gvn_numb2))
# Apply math.atan() function to the given list element and print it.
print(
    "The result after applying atan() function on given list element gvnlst[2] = ", math.atan(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 0 2 0.9 1 -3
Enter some random number = -1
The result after applying atan() function on above given first number -1.0 = -0.7853981633974483
Enter some random number = 0.3
The result after applying atan() function on above given second number 0.3 = 0.2914567944778671
The result after applying atan() function on given list element gvnlst[2] = 0.7328151017865066

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

Python Program for atan() Function Read More »

Python Program for asin() Function

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

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

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

Tip: math.asin(1) returns the value PI/2, while math.asin(-1) returns the value -PI/2. (where PI= 22/7)

Syntax:

math.asin(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 sine.

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

Examples:

Example1:

Input:

Given first number =  0.5
Given second number = 1

Output:

The result after applying asin() function on above given first number 0.5  =  0.5235987755982989
The result after applying asin() function on above given second number 1  =  1.5707963267948966

Example2:

Input:

Given first number = -1
Given second number =  0.6

Output:

The result after applying asin() function on above given first number -1  =  -1.5707963267948966
The result after applying asin() function on above given second number 0.6  =  0.6435011087932844

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

Output:

The result after applying asin() function on above given first number 0.5  =  0.5235987755982989
The result after applying asin() function on above given second number 1  =  1.5707963267948966
The result after applying asin() function on given list element gvnlst[1] =  0.1001674211615598

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.asin() function to the given first number to get the arc sine value (inverse of sine) of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.asin() 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.asin() function to the given first number to get arc sine value
# (inverse of sine) of a given number.
# Store it in another variable.
fnl_rslt = math.asin(gvn_numb1)
print("The result after applying asin() 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 asin() function on above given second number",
      gvn_numb2, " = ", math.asin(gvn_numb2))
# Apply math.asin() function to the given list element and print it.
print(
    "The result after applying asin() function on given list element gvnlst[2] = ", math.asin(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 0 3 -0.5 3 -1
Enter some random number = -1
The result after applying asin() function on above given first number -1.0 = -1.5707963267948966
Enter some random number = 0.6
The result after applying asin() function on above given second number 0.6 = 0.6435011087932844
The result after applying asin() function on given list element gvnlst[2] = -0.5235987755982989

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

Python Program for asin() Function Read More »

Python Program for cos() Function

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

The cosine of a number is returned by the math.cos() method.

The Trigonometry Cosine function’s mathematical formula is

cos(A)= adjacent side of A/ Hypotenuse

Syntax:

math.cos(x)

Parameters:

x: This is required. It is a number. A number for which the cosine must be calculated. 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 cosine of an angle.

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

Examples:

Example1:

Input:

Given first number = 1
Given second number = 45

Output:

The result after applying cos() function on above given first number 1  =  0.5403023058681398
The result after applying cos() function on above given second number 45  =  0.5253219888177297

Example2:

Input:

Given first number = 60
Given second number = 10

Output:

The result after applying cos() function on above given first number 60  =  -0.9524129804151563
The result after applying cos() function on above given second number 10  =  -0.8390715290764524

Program for cos() 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.cos() function to the given first number to get the 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.cos() 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, 15, 20, 30, 35]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 1
# Apply math.cos() function to the given first number to get the cosine value
# of a given number.
# Store it in another variable.
fnl_rslt = math.cos(gvn_numb1)
print("The result after applying cos() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 45
print("The result after applying cos() function on above given second number",
      gvn_numb2, " = ", math.cos(gvn_numb2))
# Apply math.cos() function to the given list element and print it.
print(
    "The result after applying cos() function on given list element gvnlst[1] = ", math.cos(gvn_lst[1]))

Output:

The result after applying cos() function on above given first number 1  =  0.5403023058681398
The result after applying cos() function on above given second number 45  =  0.5253219888177297
The result after applying cos() function on given list element gvnlst[1] =  -0.7596879128588213

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.cos() function to the given first number to get the 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.cos() 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.cos() function to the given first number to get the cosine value
# of a given number.
# Store it in another variable.
fnl_rslt = math.cos(gvn_numb1)
print("The result after applying cos() 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 cos() function on above given second number",
      gvn_numb2, " = ", math.cos(gvn_numb2))
# Apply math.cos() function to the given list element and print it.
print(
    "The result after applying cos() function on given list element gvnlst[2] = ", math.cos(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 10 15 5 6 0
Enter some random number = 60
The result after applying cos() function on above given first number 60.0 = -0.9524129804151563
Enter some random number = 20.5
The result after applying cos() function on above given second number 20.5 = -0.07956356727854007
The result after applying cos() function on given list element gvnlst[2] = 0.28366218546322625

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

Python Program for cos() Function Read More »

Python Program for atan2() Function

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

The arc tangent of y/x in radians is returned by the math.atan2() method. In this equation, x and y are the coordinates of a point (x,y).

The value returned is between PI and -PI. (where PI=22/7)

Syntax:

math.atan2(y, x)

Parameters:

y: This is required. It is a number (cartesian y coordinate). Indicates whether the number is positive or negative.

x: This is required. It is a number (cartesian x coordinate). Indicates whether the number is positive or negative.

Return Value:

Returns a float representing the arc tangent of y/x in radians between PI and -PI.

Note: If we pass non-numeric values to the atan2 function, it will return TypeError as output.

Examples:

Example1:

Input:

Given y coordinate = 2     
Given x coordinate = 4

Output:

The result after applying atan2() function on above given y and x coordinates { 2 , 4 } =  0.4636476090008061

Example2:

Input:

Given y coordinate = 1
Given x coordinate = 3

Output:

The result after applying atan2() function on above given y and x coordinates { 1 , 3 } =  0.3217505543966422

Program for atan2() 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 y-coordinate as static input and store it in another variable.
  • Give the x-coordinate as static input and store it in another variable.
  • Apply math.atan2() function to the given y-coordinate and the x-coordinate to get the value of arc tangent of given y-coordinate/x-coordinate in radians.
  • Store it in another variable.
  • Print the above result which is the result after applying math.atan2() function.
  • Give the number as static input and store it in another variable.
  • Apply math.atan2() function to the given list element and above-given number 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, -2, 4, 0]
# Give the y-coordinate as static input and store it in another variable.
gvn_ycoordinte = 2
# Give the x-coordinate as static input and store it in another variable.
gvn_xcoordinte = 4
# Apply math.atan2() function to the given y-coordinate and the x-coordinate
# to get the value of arc tangent of given the y-coordinate/x-coordinate in radians.
# Store it in another variable.
fnl_rslt = math.atan2(gvn_ycoordinte, gvn_xcoordinte)
# Print the above result which is the result after applying math.atan2() function.
print("The result after applying atan2() function on above given y and x coordinates {",
      gvn_ycoordinte, ",", gvn_xcoordinte, "} = ", fnl_rslt)
# Give the number as static input and store it in another variable.
gvn_numb3 = 2
# Apply math.atan2() function to the given list element and above given number
# and print it.
print("The result after applying atan2() function on the given list element gvn_lst[1] and above given number = ", math.atan2(
    gvn_lst[1], gvn_numb3))

 

Output:

The result after applying atan2() function on above given y and x coordinates { 2 , 4 } =  0.4636476090008061
The result after applying atan2() function on the given list element gvn_lst[1] and above given number =  -0.7853981633974483

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 y-coordinate and x-coordinate respectively as user input using int(), map(), input(), and split() functions.
  • Store them in two separate variables.
  • Apply math.atan2() function to the given y-coordinate and the x-coordinate to get the value of arc tangent of given y-coordinate/x-coordinate in radians.
  • Store it in another variable.
  • Print the above result which is the result after applying math.atan2() function.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Apply math.atan2() function to the given list element and above-given number 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(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give y-coordinate and x-coordinate respectively as user input using 
# int(), map(), input(), and split() functions.
gvn_ycoordinte, gvn_xcoordinte = map(int, input(
    "Enter two random numbers separated by spaces = ").split())
# Apply math.atan2() function to the given y-coordinate and the x-coordinate
# to get the value of arc tangent of given the y-coordinate/x-coordinate in radians.
# Store it in another variable.
fnl_rslt = math.atan2(gvn_ycoordinte, gvn_xcoordinte)
# Print the above result which is the result after applying math.atan2() function.
print("The result after applying atan2() function on above given y and x coordinates {",
      gvn_ycoordinte, ",", gvn_xcoordinte, "} = ", fnl_rslt)
# Give the number as user input using the int(input()) function and 
# store it in another variable.
gvn_numb3 = int(input("Enter some random number = "))
# Apply math.atan2() function to the given list element and above given number
# and print it.
print("The result after applying atan2() function on the given list element gvn_lst[2] and above given number = ", math.atan2(
    gvn_lst[2], gvn_numb3))

Output:

Enter some random List Elements separated by spaces = 4 5 2 1 0
Enter two random numbers separated by spaces = 1 3
The result after applying atan2() function on above given y and x coordinates { 1 , 3 } = 0.3217505543966422
Enter some random number = 2
The result after applying atan2() function on the given list element gvn_lst[2] and above given number = 0.7853981633974483

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

Python Program for atan2() Function Read More »

Python Program for ldexp() Function

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

The inverse of math.frexp() is returned by the math.ldexp() method, which returns x * (2**i) of the given numbers x and i. ().

For example:

Let x= 3, i=4

Then math.ldexp(x, i) = 3*(2**4) = 48

Syntax:

math.ldexp(x, i)

Parameters:

x: This is required. A number that is either positive or negative. It returns TypeError if the value is not a number.

i: This is required. A number that is either positive or negative. It returns TypeError if the value is not a number.

Return value: It returns the value of x * (2**i)

Examples:

Example1:

Input:

Given x value = 3
Given i value = 4

Output:

The value of given  x * (2**i) = 48.0

Example2:

Input:

Given x value = 5
Given i value =  6

Output:

The value of given x * (2**i) = 320.0

Program for ldexp() 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.
  • Give the second number as static input and store it in another variable.
  • Apply math.ldexp() function to the given first and the second number to get the value of the given first number*(2**second number) from the above-given formula x * (2**i)
  • Store it in another variable.
  • Print the above result which is the result after applying math.ldexp() function.
  • Give the number as static input and store it in another variable.
  • Apply math.ldexp() function to the given list element and above-given number 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, 4, 2, 0]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 3
# Give the second number as static input and store it in another variable.
gvn_numb2 = 4
# Apply math.ldexp() function to the given first and the second number to get
# the value of the given first number*(2**second number) from the above given
# formula x * (2**i)
# Store it in another variable.
rslt = math.ldexp(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying math.ldexp() function.
print(
    "The value of given first number*(2**second number) = ", rslt)
# Give the number as static input and store it in another variable.
gvn_numb3 = 2
# Apply math.ldexp() function to the given list element and above given number
# and print it.
print(
    "The value of given list element *(2** above given number ) = ", math.ldexp(
        gvn_lst[2], gvn_numb3))

Output:

The value of given first number*(2**second number) =  48.0
The value of given list element *(2** above given number ) =  8.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 first and second numbers respectively as user input using int(), map(), input(), and split()
    functions.
  • Store them in two separate variables.
  • Apply math.ldexp() function to the given first and the second number to get the value of the given first number*(2**second number) from the above-given formula x * (2**i)
  • Store it in another variable.
  • Print the above result which is the result after applying math.ldexp() function.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Apply math.ldexp() function to the given list element and above-given number 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(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give first and second numbers respectively as user input using int(),map(),input(),and split()
# functions and store them in two separate variables.
gvn_numb1, gvn_numb2 = map(int, input(
    "Enter two random numbers separated by spaces = ").split())
# Apply math.ldexp() function to the given first and the second number to get
# the value of the given first number*(2**second number) from the above given
# formula x * (2**i)
# Store it in another variable.
rslt = math.ldexp(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying math.ldexp() function.
print(
    "The value of given first number*(2**second number) = ", rslt)
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb3 = int(input("Enter some Random Number = "))
# Apply math.ldexp() function to the given list element and above given number
# and print it.
print(
    "The value of given list element *(2** above given number ) = ", math.ldexp(
        gvn_lst[3], gvn_numb3))

Output:

Enter some random List Elements separated by spaces = 3 0 2 1
Enter two random numbers separated by spaces = 5 6
The value of given first number*(2**second number) = 320.0
Enter some Random Number = 7
The value of given list element *(2** above given number ) = 128.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 ldexp() Function Read More »

Python Program for isnan() Function

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

The math.isnan() method determines whether a value is NaN (Not a Number).

If the specified value is a NaN, this method returns true; otherwise, it returns False.

Syntax:

math.isnan(x)

parameters:

x: This is required. It is a value to be checked.

Return Value: It returns a boolean value that is True if the value is NaN and False otherwise.

Examples:

Example1:

Input:

Given Value = -200.02
Given Value = 500
Given Value = math.nan
Given Value = math.inf
Given Value = NaN

Output:

False
False
True
False
True

Example2:

Input:

Given Value = -850.07
Given Value =  nan

Output:

False
True

Program for isnan() Function 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.
  • Apply math.isnan() function to the given number to check if the given number is NaN(Not a Number) or not.
  • Store it in another variable.
  • Print the above result.
  • Similarly, check for the other values and print the result.
  • The Exit of 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 = -200.02
# Apply math.isnan() function to the given number to check if the given number
# is NaN(Not a Number) or not.
# Store it in another variable.
rslt = math.isnan(gvn_numb)
# print the above result.
print("Checking if the above given number",
      gvn_numb, "is NaN or not :", rslt)
# similarly check for the other values and print the result.
print(math.isnan(500))
print(math.isnan(math.nan))
print(math.isnan(math.inf))
print(math.isnan(float('NaN')))

Output:

Checking if the above given number -200.02 is NaN or not : False
False
True
False
True

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.
  • Apply math.isnan() function to the given number to check if the given number is NaN(Not a Number) or not.
  • Store it in another variable.
  • Print the above result.
  • Similarly, check for the other values and print the result.
  • The Exit of 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 = '))
# Apply math.isnan() function to the given number to check if the given number
# is NaN(Not a Number) or not.
# Store it in another variable.
rslt = math.isnan(gvn_numb)
# print the above result.
print("Checking if the above given number",
      gvn_numb, "is NaN or not :", rslt)
# similarly check for the other values
b = input('Enter some random value = ')
print(math.isnan(float(b)))

Output:

Enter some random number = 380.0006
Checking if the above given number 380.0006 is NaN or not : False
Enter some random value = nan
True

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 isnan() Function Read More »

Python Program for isinf() Function

In the previous article, we have discussed Python Program for How To Find Cube Root
isinf() Function in Python:

The math.isinf() method determines whether or not a number is infinite.

If the specified number is positive or negative infinity, this method returns true; otherwise, it returns False.

Syntax:

math.isinf(x)

Parameters:

x: This is required. It is a number to check.

Return Value: It returns a boolean value. If x is positive or negative infinity, this statement is true. Otherwise, false.

Examples:

Example1:

Input:

Given value = 1000
Given value = -100
Given value =  math.pi
Given value = nan
Given value = inf
Given value = -inf

Output:

False
False
False
False
True
True

Example2:

Input:

Given value = -70000
Given value = NaN

Output:

False
False

Program for isinf() Function 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.
  • Apply math.isinf() function to the given number to check if the given number is infinite or not.
  • Store it in another variable.
  • Print the above result.
  • Similarly, check for the other values and print the result.
  • The Exit of 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 = 1000
# Apply math.isinf() function to the given number to check if the given number
# is infinite or not.
# Store it in another variable.
rslt = math.isinf(gvn_numb)
# print the above result.
print("Checking if the above given number",
      gvn_numb, "is infinite or not :", rslt)
# similarly check for the other values and print the result.
print(math.isinf(-100))
print(math.isinf(math.pi))
print(math.isinf(float('nan')))
print(math.isinf(float('inf')))
print(math.isinf(float('-inf')))

Output:

Checking if the above given number 1000 is infinite or not : False
False
False
False
True
True

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

Approach:

  • Import math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Apply math.isinf() function to the given number to check if the given number is infinite or not.
  • Store it in another variable.
  • Print the above result.
  • Similarly, check for the other values and print the result.
  • The Exit of 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 = '))
# Apply math.isinf() function to the given number to check if the given number
# is infinite or not.
# Store it in another variable.
rslt = math.isinf(gvn_numb)
# print the above result.
print("Checking if the above given number",
      gvn_numb, "is infinite or not :", rslt)
# similarly check for the other values
b = input('Enter some random value = ')
print(math.isinf(float(b)))

Output:

Enter some random number = -9999
Checking if the above given number -9999 is infinite or not : False
Enter some random value = inf
True

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 isinf() Function Read More »

Python Program for trunc() Function

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

The math.trunc() method returns the integer part of a number that has been truncated.

Note: Please keep in mind that this method will NOT round the number up or down to the nearest integer, but will simply remove the decimals.

Syntax:

math.trunc(x)

Parameters:

x: This is required. It is a number. The number from which you want to remove the decimal point(truncate). If the value is not a number, a TypeError is returned.

Return Value: Returns an int value that represents a number’s truncated integer parts.

Examples:

Example1:

Input:

Given first number = 870.26
Given second number = -11.56

Output:

The truncated value of the given first number  870.26  =  870
The truncated value of the given second number  -11.56  =  -11

Example2:

Input:

Given first number = 80.432
Given second number = 22.4

Output:

The truncated value of the given first number  80.432  =  80
The truncated value of the given second number  22.4  =  22

Program for trunc() 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.trunc() function to the given first number to remove the decimal part and give only the integer part of the given number. (truncate)
  • Store it in another variable.
  • Print the truncated value of the given first number.
  • Similarly, do the same for the other number.
  • Apply math.trunc() 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 = [12.005, 11.89, 30.224]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 870.26
# Apply math.trunc() function to the given first number to remove the decimal
# part and give only the integer part of the given number. (truncate) 
# Store it in another variable.
trnc_numb = math.trunc(gvn_numb1)
# Print the truncated value of the given first number.
print("The truncated value of the given first number ",
      gvn_numb1, " = ", trnc_numb)
# similarly do the same for the other number.
gvn_numb2 = -11.56
trnc_numb2 = math.trunc(gvn_numb2)
print("The truncated value of the given second number ",
      gvn_numb2, " = ", trnc_numb2)
# Apply math.trunc() function to the given list element and print it.
print(
    "The truncated value of the given list element ", gvn_lst[2], " = ", math.trunc(gvn_lst[2]))

Output:

The truncated value of the given first number  870.26  =  870
The truncated value of the given second number  -11.56  =  -11
The truncated value of the given list element  30.224  =  30

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.trunc() function to the given first number to remove the decimal part and give only the integer part of the given number. (truncate)
  • Store it in another variable.
  • Print the truncated value of the given first number.
  • Similarly, do the same for the other number.
  • Apply math.trunc() 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.trunc() function to the given first number to remove the decimal
# part and give only the integer part of the given number. (truncate) 
# Store it in another variable.
trnc_numb = math.trunc(gvn_numb1)
# Print the truncated value of the given first number.
print("The truncated value of the given first number ",
      gvn_numb1, " = ", trnc_numb)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
trnc_numb2 = math.trunc(gvn_numb2)
print("The truncated value of the given second number ",
      gvn_numb2, " = ", trnc_numb2)
# Apply math.trunc() function to the given list element and print it.
print(
    "The truncated value of the given list element ", gvn_lst[1], " = ", math.trunc(gvn_lst[1]))

Output:

Enter some random List Elements separated by spaces = 3 45.66 20.01 54
Enter some random number = 80.432
The truncated value of the given first number 80.432 = 80
Enter some random number = 22.4
The truncated value of the given second number 22.4 = 22
The truncated value of the given list element 45.66 = 45

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 trunc() Function Read More »