{"id":7305,"date":"2023-11-01T10:19:16","date_gmt":"2023-11-01T04:49:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=7305"},"modified":"2023-11-10T12:11:04","modified_gmt":"2023-11-10T06:41:04","slug":"python-program-to-swap-two-variables","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-swap-two-variables\/","title":{"rendered":"Python Program to Swap Two Variables"},"content":{"rendered":"
Swapping:<\/strong><\/p>\n Swapping two variables in computer programming means that the variables values are swapped.<\/p>\n Given two variables x , y the task is to swap the variable values.<\/p>\n Examples:<\/strong><\/p>\n Example1:(Integer Values Swapping)<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:(Boolean Values Swapping)<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:(Decimal Values Swapping)<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example4:(String Values Swapping)<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to swap two variables in Python some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n A temp variable is the easiest way to change the values of two variables. The temp variables will save the value of the fist variable (temp = a), allow the two variables to swap (a = b), and assign the them to the second variable. The temp variables will then be saved.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The tuple packaging and sequence unpackaging can also be used to change the values of two variables without a temporary variable. There are a number of methods in which tuples can be created, including by dividing tuples using commas. In addition, Python evaluates the right half of a task on its left side. Thus the variables are packed up and unpacked with the same amount of commas separating the target variables on the left hand side by selecting the comma on the right hand hand side of the sentence.<\/p>\n It may be used for more than two variables, provided that both sides of the state have the same amount of variables<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Swapping: Swapping two variables in computer programming means that the variables values are swapped. Given two variables x , y the task is to swap the variable values. Examples: Example1:(Integer Values Swapping) Input: p=1372 q=4129 Output: printing the values of integer variables p and q before swapping p = 1372 q = 4129 printing the …<\/p>\np=1372 q=4129<\/pre>\n
printing the values of integer variables p and q before swapping\r\np = 1372\r\nq = 4129\r\nprinting the values of integer variables p and q after swapping\r\np = 4129\r\nq = 1372<\/pre>\n
p=True q=False<\/pre>\n
printing the values of boolean variables p and q before swapping\r\np = True\r\nq = False\r\nprinting the values of boolean variables p and q after swapping\r\np = False\r\nq = True<\/pre>\n
p = 2738.321 q = 83472.421<\/pre>\n
printing the values of decimal variables p and q before swapping\r\np = 2738.321\r\nq = 83472.421\r\nprinting the values of decimal variables p and q after swapping\r\np = 83472.421\r\nq = 2738.321<\/pre>\n
p=\"Vicky\" q=\"Tara\"<\/pre>\n
printing the values of string variables p and q before swapping\r\np = vicky\r\nq = Tara\r\nprinting the values of string variables p and q after swapping\r\np = Tara\r\nq = vicky<\/pre>\n
Swapping two Variables in Python<\/h2>\n
\n
Method #1: Using temporary Variable<\/h3>\n
# given variables of different types\r\n# given two integer variables\r\np = 1372\r\nq = 4129\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of integer variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# using temp variable to swap the two integers\r\n# swapping two variables\r\ntempo = p\r\np = q\r\nq = tempo\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of integer variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# given two decimal variables\r\np = 2738.321\r\nq = 83472.421\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of decimal variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# using temp variable to swap the two decimal variables\r\n# swapping two variables\r\ntempo = p\r\np = q\r\nq = tempo\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of decimal variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# given two boolean variables\r\np = True\r\nq = False\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of boolean variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# using temp variable to swap the two boolean values\r\n# swapping two variables\r\ntempo = p\r\np = q\r\nq = tempo\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of boolean variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# given two string variables\r\np = \"vicky\"\r\nq = \"Tara\"\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of string variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# using temp variable to swap the two string\r\n# swapping two variables\r\ntempo = p\r\np = q\r\nq = tempo\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of string variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n<\/pre>\n
printing the values of integer variables p and q before swapping\r\np = 1372\r\nq = 4129\r\nprinting the values of integer variables p and q after swapping\r\np = 4129\r\nq = 1372\r\nprinting the values of decimal variables p and q before swapping\r\np = 2738.321\r\nq = 83472.421\r\nprinting the values of decimal variables p and q after swapping\r\np = 83472.421\r\nq = 2738.321\r\nprinting the values of boolean variables p and q before swapping\r\np = True\r\nq = False\r\nprinting the values of boolean variables p and q after swapping\r\np = False\r\nq = True\r\nprinting the values of string variables p and q before swapping\r\np = vicky\r\nq = Tara\r\nprinting the values of string variables p and q after swapping\r\np = Tara\r\nq = vicky<\/pre>\n
Method #2:Using comma operator in Python without temporary variable(Tuple Swap)<\/h3>\n
# given variables of different types\r\n# given two integer variables\r\np = 1372\r\nq = 4129\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of integer variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# swapping two variables\r\np, q = q, p\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of integer variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# given two decimal variables\r\np = 2738.321\r\nq = 83472.421\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of decimal variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# swapping two variables\r\np, q = q, p\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of decimal variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# given two boolean variables\r\np = True\r\nq = False\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of boolean variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# swapping two variables\r\np, q = q, p\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of boolean variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# given two string variables\r\np = \"vicky\"\r\nq = \"Tara\"\r\n# printing the values of p and q before swapping\r\nprint(\"printing the values of string variables p and q before swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n# swapping two variables\r\np, q = q, p\r\n# printing the values of p and q after swapping\r\nprint(\"printing the values of string variables p and q after swapping\")\r\nprint(\"p =\", p)\r\nprint(\"q =\", q)\r\n<\/pre>\n
printing the values of integer variables p and q before swapping\r\np = 1372\r\nq = 4129\r\nprinting the values of integer variables p and q after swapping\r\np = 4129\r\nq = 1372\r\nprinting the values of decimal variables p and q before swapping\r\np = 2738.321\r\nq = 83472.421\r\nprinting the values of decimal variables p and q after swapping\r\np = 83472.421\r\nq = 2738.321\r\nprinting the values of boolean variables p and q before swapping\r\np = True\r\nq = False\r\nprinting the values of boolean variables p and q after swapping\r\np = False\r\nq = True\r\nprinting the values of string variables p and q before swapping\r\np = vicky\r\nq = Tara\r\nprinting the values of string variables p and q after swapping\r\np = Tara\r\nq = vicky<\/pre>\n
\n