Python

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 »

Python Program for How To Use While Not

In the previous article, we have discussed Python Program for trunc() Function
Iteration is the process of repeatedly running the same block of code, potentially many times. Loops are built using Python’s while statement.

In Python, a while loop is used to iterate over a block of code as long as the test condition) is True. The While Loop executes the set of statements as long as the condition is True. The while loop instructs a computer to perform an action as long as the condition is met or holds True.

Python’s While Not loop:

In Python, a while not loop executes the loop’s body until the condition for loop termination is met. If the condition evaluates to False, use the syntax while not condition with the condition as a boolean expression to execute the loop’s body.

Program for How To Use While Not in Python

Method #1: Using While Not Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Iterate till the given number is not equal to 0 using the while not loop.
  • Inside the loop, print the given number.
  • Decrement the given number by 1 and store it in the same variable.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 8
# Iterate till the given number is not equal to 0 using the while not loop.
while not (gvn_numb == 0):
    # Inside the loop, print the given number.
    print(gvn_numb)
    # Decrement the given number by 1 and store it in the same variable.
    gvn_numb = gvn_numb - 1

Output:

8
7
6
5
4
3
2
1

Method #2: Using While Not Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Iterate till the given number is not equal to 0 using the while not loop.
  • Inside the loop, print the given number.
  • Decrement the given number by 1 and store it in the same variable.
  • The Exit of Program.

Below is the implementation:

# 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 = '))
# Iterate till the given number is not equal to 0 using the while not loop.
while not (gvn_numb == 0):
    # Inside the loop, print the given number.
    print(gvn_numb)
    # Decrement the given number by 1 and store it in the same variable.
    gvn_numb = gvn_numb - 1

Output:

Enter some random number = 4
4
3
2
1

If the variable is not iterable, use the syntax “while variable not in” iterable to execute the loop’s body.

gvn_lst = [2, 6, 7, 3, 4]
while 8 not in gvn_lst:
    gvn_lst.append(len(gvn_lst) + 1)
print(gvn_lst)

output:

[2, 6, 7, 3, 4, 6, 7, 8]

break and continue statements in Python:

The Python break statement terminates a loop completely.

The Python continue statement terminates the current loop iteration immediately.

While else clause in Python:

We can use the else statement to run a block of code only once when the condition is no longer true.

Syntax:

while <expr>: 
    <statement(s)> 
else: 
    <additional_statement(s)>

When the while loop ends, the additional statement(s)> specified in the else clause will be executed.

Method #3: Using While else Clause (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Iterate till the given number is less than 8 using the while loop.
  • Inside the loop print the given number.
  • Increment the given number by 1 and store it in the same variable.
  • Else, print “The given number is greater than 8”.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 3
# Iterate till the given number is less than 8 using the while loop.
while gvn_numb < 8:
    # Inside the loop print the given number.
    print(gvn_numb)
    # Increment the given number by 1 and store it in the same variable.
    gvn_numb += 1
else:
    # Else, print "The given number is greater than 8".
    print("The given number is greater than 8")

Output:

3
4
5
6
7
The given number is greater than 8

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python Program for How To Use While Not Read More »

Python Program for Set sorted() Method

In the previous article, we have discussed Python Program for Set remove() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}

Output:

The given set is :
{'is', 'hello', 'btechgeeks', 'this'}
The given set after sorting the items in ascending order :
['btechgeeks', 'hello', 'is', 'this']

Explanation:

Here the items of the set are sorted in ascending order based upon the ASCII values.

Example2:

Input:

 Given set = {2, 3, 5, 10, 6, 12, 50, 1}

Output:

The given set is :
{1, 2, 3, 5, 6, 10, 12, 50}
The given set after sorting the items in descending order :
[50, 12, 10, 6, 5, 3, 2, 1]

Explanation:

Here the items of the set are sorted in descending order using the reverse= True condition
(based upon the ASCII values)

Program for Set sorted() Method in Python

set sorted() Method:

One of the set methods in Python is the set sorted() function, which is used to sort the given set items in ascending or descending order.

Syntax:

sorted(iterable,key,reverse)  

ex: sorted(setname) - for ascending order
     sorted(setname, reverse=True)- for descending order.

Parameter Values:

sorted() accepts three parameters, two of which are optional.

Iterable: a sequence (list, tuple, or string) or a collection (dictionary, set, or frozen set), or any other iterator that must be sorted.
Key(optional): A function that can be used as a key or as the basis for sort comparison.
Reverse(optional): If set to true, the iterable is sorted in reverse (descending) order; otherwise, it is set to false.

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

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply sorted() method to the given set to sort the items of the given set either in ascending or descending order.
  • Store it in another variable.
  • Print the given set after sorting all the items in ascending order.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply sorted() method to the given set to sort the items of the given set either in
# ascending or descending order.
# Store it in another variable.
sortd_set = sorted(gven_set)
# Print the given set after sorting all the items in ascending order.
print("The given set after sorting the items in ascending order :")
print(sortd_set)

Output:

The given set is :
{'this', 'hello', 'is', 'btechgeeks'}
The given set after sorting the items:
['btechgeeks', 'hello', 'is', 'this']

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

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply sorted() method to the given set to sort the items of the given set either in ascending or descending order.
  • Store it in another variable.
  • Print the given set after sorting all the items in descending order.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply sorted() method to the given set to sort the items of the given set either in
# ascending or descending order.
# Store it in another variable.
sortd_set = sorted(gven_set, reverse=True)
# Print the given set after sorting all the items in descending order.
print("The given set after sorting the items in descending order :")
print(sortd_set)

Output:

Enter some random values separated by spaces = 2 3 5 10 6 12 50 1
The given set is :
{1, 2, 3, 5, 6, 10, 12, 50}
The given set after sorting the items in descending order :
[50, 12, 10, 6, 5, 3, 2, 1]

Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples tutorial will definitely aid programmers to learn easily & efficiently.

Python Program for Set sorted() Method Read More »