Python Program to Swap two Float Numbers With Examples

In this article, we will learn about how to write python program to swap two float numbers.

Getting Started

The task is to swap two float numbers using python programming. For example,

Let’s say a = 80.1 and b = 90.4, then output should be a = 90.4 and b = 80.1.

There are multiple ways to achieve above tasks.

  1. Using a Temporary Variable
  2. Using Arithmetic Operations
  3. Using Tuple Packing and Unpacking
  4. Using XOR bitwise operation
  5. Using map Function

1. Using a Temporary Variable

Using third variable, we can write python program to swap two float numbers as shown below –

def swap_numbers(a, b):
    temp = a  # store value of a in temp.
    a = b     # store value of b in a.
    b = temp  # store value of temp in b.
    return a, b

# Example usage
a = 5.4
b = 10.6
print("Before swap: a =", a, "b =", b)
a, b = swap_numbers(a, b)
print("After swap: a =", a, "b =", b)

Here,

  • There are two float variables a and b which we want to swap or interchange.
  • temp is temporary variable which is being used to store value of a temporarily.

Output:

Before swap: a = 5.4 b = 10.6
After swap: a = 10.6 b = 5.4

2. Using Arithmetic Operations

We are going to use subtract i.e. and addition i.e. + operations to swap two float numbers in python programming.

Pseudo code to such tasks –

  • Define two float variables a and b.
  • Then, get sum of a and b. Then, store sum in a.
  • After that, subtract b from a. Then, store the difference in b.
  • Then, subtract b from a and store in a.
  • Now, value of a and b are interchanged value.

Program –

def swap_numbers(a, b):
    a = a + b  # Take sum of a and b and store in a.
    b = a - b  # Subtract b from a and store in b.
    a = a - b  # Subtract b from a and store in a.
    return a, b

# Example usage
num1 = 5.04
num2 = 6.01
print("Before swap: num1 =", num1, "num2 =", num2)
num1, num2 = swap_numbers(num1, num2)
print("After swap: num1 =", num1, "num2 =", num2)

Output:

Before swap: num1 = 5.04 num2 = 6.01
After swap: num1 = 6.01 num2 = 5.04

3. Using Tuple Packing and Unpacking

We can also use techniques of tuple packing and unpacking to write python program to swap two float numbers with examples as shown below –

This is simplest way to interchange two float numbers in python.

def swap_numbers(a, b):
    return b, a

# Example usage
num1 = 5.32
num2 = 6.43
print("Before swap: num1 =", num1, "num2 =", num2)
num1, num2 = swap_numbers(num1, num2)
print("After swap: num1 =", num1, "num2 =", num2)

In above code, we are just interchanging the position and return the values.

Output

Before swap: num1 = 5.32 num2 = 6.43
After swap: num1 = 6.43 num2 = 5.32

4. Using XOR Bitwise Operation

We can also use XOR bitwise operator i.e. ^ to interchange or swap two float numbers in python as shown below –

def swap_numbers(a, b):
    a = a ^ b
    b = a ^ b
    a = a ^ b
    return a, b

# Example usage
num1 = 5.12
num2 = 10.43
print("Before swap: num1 =", num1, "num2 =", num2)
num1, num2 = swap_numbers(num1, num2)
print("After swap: num1 =", num1, "num2 =", num2)

Output:

Before swap: num1 = 5.12 num2 = 10.43
After swap: num1 = 10.43 num2 = 5.12

5. Using map Function

We can also use map function in python to interchange or swap two float numbers in python as shown below –

num1 = 65.34
num2 = 1.32
print("Before swap: num1 =", num1, "num2 =", num2)
num1, num2 = map(lambda x: num2 if x == num1 else num1, (num1, num2))
print("After swap: num1 =", num1, "num2 =", num2)

Here,
We use the map function with a lambda function. Then, we compare each number with num1 and replaces it with num2 if it matches, and vice versa.

Output:

Before swap: num1 = 65.43 num2 = 1.32
After swap: num1 = 1.32 num2 = 65.43

That’s how we can swap two float numbers in python with examples. Learn more about python at official documentation

Leave a Reply