{"id":24093,"date":"2021-10-14T09:10:20","date_gmt":"2021-10-14T03:40:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=24093"},"modified":"2021-11-05T20:56:52","modified_gmt":"2021-11-05T15:26:52","slug":"python-program-for-copysign-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-copysign-function\/","title":{"rendered":"Python Program for copysign() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for ceil() Function<\/a> The math.copysign() method returns a float containing the first parameter’s value and the sign (+\/-) of the second parameter.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n x:<\/strong> This is required. It is a number. It returns the value of this parameter.<\/p>\n y:<\/strong> This is required. It is a number. It returns the sign(+\/-) of this parameter.<\/p>\n For example:<\/p>\n let x=10, y=-20.5<\/p>\n The math.copysign() method returns the value of x=10, and the sign of y i.e,negative sign(-).<\/p>\n so, the result number = -10<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a>.<\/p>\n 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. …<\/p>\n
\ncopysign() Function in Python:<\/strong><\/p>\nmath.copysign(x, y)<\/pre>\n
Given tuple = (1, -10, 20, 35, -12.5)\r\nGiven List = [6.7, 80, -10, -26, 13.5]<\/pre>\n
The copysign value of given numbers 12 and -10 = -12.0\r\nThe copysign value of given numbers -9.5 and 30 = 9.5\r\nThe copysign value of given tuple element gvn_tupl[1] and 50 = 10.0\r\nThe copysign value of given list element gvn_lst[2] and -65 = -10.0<\/pre>\n
Given tuple = (10, -20, 30, -40, 50)\r\nGiven List = [1, -2, -3, 4]<\/pre>\n
The copysign value of given numbers 45 and -70 = -45.0\r\nThe copysign value of given numbers -80 and 100 = 80.0\r\nThe copysign value of given tuple element gvn_tupl[0] and -25 = -10.0\r\nThe copysign value of given list element gvn_lst[3] and 54 = 4.0<\/pre>\n
Program for copysign() Function in python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the tuple as static input and store it in a variable.\r\ngvn_tupl = (1, -10, 20, 35, -12.5)\r\n# Give the list as static input and store it in another variable.\r\ngvn_lst = [6.7, 80, -10, -26, 13.5]\r\n# Apply math. copysign() function to any positive and negative numbers\r\n# respectively and print it.\r\nprint('The copysign value of given numbers 12 and -10 = ', math.copysign(12, -10))\r\n# Apply math. copysign() function to any negative and positive numbers\r\n# respectively and print it.\r\nprint('The copysign value of given numbers -9.5 and 30 = ', math.copysign(-9.5, 30))\r\n# Apply math. copysign() function to the given tuple element, any other number\r\n# and print it.\r\nprint('The copysign value of given tuple element gvn_tupl[1] and 50 = ', math.copysign(\r\n gvn_tupl[1], 50))\r\n# Apply math. copysign() function to the given list\u00a0element, any other\r\n# number and print it.\r\nprint('The copysign value of given list element gvn_lst[2] and -65 = ', math.copysign(\r\n gvn_lst[2], -65))\r\n<\/pre>\n
The copysign value of given numbers 12 and -10 = -12.0\r\nThe copysign value of given numbers -9.5 and 30 = 9.5\r\nThe copysign value of given tuple element gvn_tupl[1] and 50 = 10.0\r\nThe copysign value of given list element gvn_lst[2] and -65 = -10.0<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the tuple as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_tupl = tuple(map(float, input(\r\n 'Enter some random tuple Elements separated by spaces = ').split()))\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\ngvn_lst = list(map(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give positive and negative numbers respectively as user input using float(),map(),input(),and split()\r\n# functions and store them in two separate variables.\r\na,b=map(float,input(\"Enter some random positive and negative numbers separated by spaces = \").split())\r\n# Apply math. copysign() function to the given positive and negative numbers respectively and print it.\r\nprint(\"The copysign value of given numbers\",a,\"and\",b,\" = \", math.copysign(a, b))\r\n# Give negative and positive numbers respectively as user input using float(),map(),input(),and split()\r\n# functions and store them in two separate variables.\r\np,q=map(float,input(\"Enter some random negative and positive numbers separated by spaces = \").split())\r\n# Apply math. copysign() function to the given negative and positive numbers respectively and print it.\r\nprint(\"The copysign value of given numbers\",p,\"and\",q,\" = \", math.copysign(p, q))\r\n#Give the number as user input using the float(input()) function and store it in another variable.\r\ngvn_numb1= float(input(\"Enter some random number = \"))\r\n#Apply math. copysign() function to the given tuple element, and above-given number and print it.\r\nprint('The copysign value of given tuple element gvn_tupl[0] and',gvn_numb1,' = ', math.copysign(\r\n gvn_tupl[0],gvn_numb1 ))\r\n#Give the number as user input using the float(input()) function and store it in another variable.\r\ngvn_numb2= float(input(\"Enter some random number = \"))\r\n#Apply math. copysign() function to the given list element, and above-given number and print it.\r\nprint('The copysign value of given list element gvn_lst[2] and',gvn_numb2,' = ', math.copysign(\r\n gvn_lst[2],gvn_numb2 ))\r\n<\/pre>\n
Enter some random tuple Elements separated by spaces = 20 -34 45 -64\r\nEnter some random List Elements separated by spaces = 16 -31 -78 42\r\nEnter some random positive and negative numbers separated by spaces = 25 -15\r\nThe copysign value of given numbers 25.0 and -15.0 = -25.0\r\nEnter some random negative and positive numbers separated by spaces = -18 46\r\nThe copysign value of given numbers -18.0 and 46.0 = 18.0\r\nEnter some random number = 65\r\nThe copysign value of given tuple element gvn_tupl[0] and 65.0 = 20.0\r\nEnter some random number = -17\r\nThe copysign value of given list element gvn_lst[2] and -17.0 = -78.0<\/pre>\n
\n