Python Program for copysign() Function

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

The math.copysign() method returns a float containing the first parameter’s value and the sign (+/-) of the second parameter.

Syntax:

math.copysign(x, y)

Parameter Values:

x: This is required. It is a number. It returns the value of this parameter.

y: This is required. It is a number. It returns the sign(+/-) of this parameter.

For example:

let x=10, y=-20.5

The math.copysign() method returns the value of x=10, and the sign of y i.e,negative sign(-).

so, the result number = -10

Examples:

Example1:

Input:

Given tuple = (1, -10, 20, 35, -12.5)
Given List = [6.7, 80, -10, -26, 13.5]

Output:

The copysign value of given numbers 12 and -10 =  -12.0
The copysign value of given numbers -9.5 and 30 =  9.5
The copysign value of given tuple element gvn_tupl[1] and 50 =  10.0
The copysign value of given list element gvn_lst[2] and -65 =  -10.0

Example2:

Input:

Given tuple = (10, -20, 30, -40, 50)
Given List = [1, -2, -3, 4]

Output:

The copysign value of given numbers 45 and -70 =  -45.0
The copysign value of given numbers -80 and 100 =  80.0
The copysign value of given tuple element gvn_tupl[0] and -25 =  -10.0
The copysign value of given list element gvn_lst[3] and 54 =  4.0

Program for copysign() Function in python

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

Approach:

  • Import math module using the import keyword.
  • Give the tuple as static input and store it in a variable.
  • Give the list as static input and store it in another variable.
  • Apply math. copysign() function to any positive and negative numbers respectively and print it.
  • Apply math. copysign() function to any negative and positive numbers respectively and print it.
  • Apply math. copysign() function to the given tuple element, any other number and print it.
  • Apply math. copysign() function to the given list element, any other number and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the tuple as static input and store it in a variable.
gvn_tupl = (1, -10, 20, 35, -12.5)
# Give the list as static input and store it in another variable.
gvn_lst = [6.7, 80, -10, -26, 13.5]
# Apply math. copysign() function to any positive and negative numbers
# respectively and print it.
print('The copysign value of given numbers 12 and -10 = ', math.copysign(12, -10))
# Apply math. copysign() function to any negative and positive numbers
# respectively and print it.
print('The copysign value of given numbers -9.5 and 30 = ', math.copysign(-9.5, 30))
# Apply math. copysign() function to the given tuple element, any other number
# and print it.
print('The copysign value of given tuple element gvn_tupl[1] and 50 = ', math.copysign(
    gvn_tupl[1], 50))
# Apply math. copysign() function to the given list element, any other
# number and print it.
print('The copysign value of given list element gvn_lst[2] and -65 = ', math.copysign(
    gvn_lst[2], -65))

Output:

The copysign value of given numbers 12 and -10 =  -12.0
The copysign value of given numbers -9.5 and 30 =  9.5
The copysign value of given tuple element gvn_tupl[1] and 50 =  10.0
The copysign value of given list element gvn_lst[2] and -65 =  -10.0

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

Approach:

  • Import math module using the import keyword.
  • Give the tuple as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give positive and negative numbers respectively as user input using float(), map(), input(), and split() functions and store them in two separate variables.
  • Apply math. copysign() function to the given positive and negative numbers respectively and print it.
  • Give negative and positive numbers respectively as user input using float(), map(), input(), and split() functions and store them in two separate variables.
  • Apply math. copysign() function to the given negative and positive numbers respectively and print it.
  • Give the number as user input using the float(input()) function and store it in another variable.
  • Apply math. copysign() function to the given tuple element, and above-given number and print it.
  • Give the number as user input using the float(input()) function and store it in another variable.
  • Apply math. copysign() 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 tuple as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(float, input(
   'Enter some random tuple Elements separated by spaces = ').split()))
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_lst = list(map(float, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give positive and negative numbers respectively as user input using float(),map(),input(),and split()
# functions and store them in two separate variables.
a,b=map(float,input("Enter some random positive and negative numbers separated by spaces = ").split())
# Apply math. copysign() function to the given positive and negative numbers respectively and print it.
print("The copysign value of given numbers",a,"and",b," = ", math.copysign(a, b))
# Give negative and positive numbers respectively as user input using float(),map(),input(),and split()
# functions and store them in two separate variables.
p,q=map(float,input("Enter some random negative and positive numbers separated by spaces = ").split())
# Apply math. copysign() function to the given negative and positive numbers respectively and print it.
print("The copysign value of given numbers",p,"and",q," = ", math.copysign(p, q))
#Give the number as user input using the float(input()) function and store it in another variable.
gvn_numb1= float(input("Enter some random number = "))
#Apply math. copysign() function to the given tuple element, and above-given number and print it.
print('The copysign value of given tuple element gvn_tupl[0] and',gvn_numb1,' = ', math.copysign(
    gvn_tupl[0],gvn_numb1 ))
#Give the number as user input using the float(input()) function and store it in another variable.
gvn_numb2= float(input("Enter some random number = "))
#Apply math. copysign() function to the given list element, and above-given number and print it.
print('The copysign value of given list element gvn_lst[2] and',gvn_numb2,' = ', math.copysign(
    gvn_lst[2],gvn_numb2 ))

Output:

Enter some random tuple Elements separated by spaces = 20 -34 45 -64
Enter some random List Elements separated by spaces = 16 -31 -78 42
Enter some random positive and negative numbers separated by spaces = 25 -15
The copysign value of given numbers 25.0 and -15.0 = -25.0
Enter some random negative and positive numbers separated by spaces = -18 46
The copysign value of given numbers -18.0 and 46.0 = 18.0
Enter some random number = 65
The copysign value of given tuple element gvn_tupl[0] and 65.0 = 20.0
Enter some random number = -17
The copysign value of given list element gvn_lst[2] and -17.0 = -78.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.