How to Swap Two Numbers in Python Using Function [4 Examples]

If you want to learn how to work with user-defined functions in Python to swap two numbers, you have come to the right place.

Swapping two variables using function in Python is a common task, and that can be done by passing our variables to the user-defined function as arguments and returning or printing the swapped data.

In this Python tutorial, I will explain the various methods to swap two numbers in Python using function, i.e., with arguments and return value, with arguments and without return value, without arguments and with return value, and without arguments and return value.

How to Swap Two Numbers in Python Using Function

Here, I will create a function in Python that will swap two variables storing numbers in it.

I will use the comma separator in the Python program to swap the given variable. It is a Python expression that performs simultaneous assignment or swapping of values between two variables.

var1, var2 = var2, var1

Here are all the different ways to do this:

Example 1: Swap two numbers in Python with arguments and return value.

Here is the complete Python code to swap two numbers with arguments and return values.

def swap_USA_states(State1,State2): 
    print("Top 1 :",'California')
    print("Top 2 :",'Texas')

    State1,State2 = State2,State1
    return State1,State2 

California=str(input("Enter value : "))
Texas=str(input("Enter value : "))    
California,Texas=swap_USA_states(California,Texas)

print("Top 1 :","California)
print("Top 2 :",Texas)

Output:

Enter value : California
Enter value : Texas
Top 1 : California
Top 2 : Texas
Top 1 : Texas
Top 2 : California

You can see the below image to get the result of the Python program to swap two numbers with arguments and return values.

swap two numbers in python using function

Example 2: Swap two numbers in Python with arguments and without a return value.

We have provided the full code of Python to swap two numbers with arguments and without return values.

def swap_name(first_name, last_name):
    print("first_name:", first_name)
    print("last_name:", last_name)
    
    first_name, last_name = last_name, first_name
    print("After swapping first_name becomes:", first_name)
    print("After swapping last_name becomes:", last_name)

firstname = str(input("Enter value for first name: "))
lastname = str(input("Enter value for last name: "))  
swap_name(firstname, lastname)

Output:

Enter value for first name: John
Enter value for last name: Gilbert
first_name: John
last_name: Gilbert
After swapping first_name becomes: Gilbert
After swapping last_name becomes: John

Refer to the screenshot below. You can see the result of the Python program once the program gets executed.

swap two numbers using function in python

Example 3: Swap two numbers in Python without arguments and with return value.

The Python source code of Python to swap two numbers without arguments and with return value is given below.

def swap_price(): 
    price1 =int(input("Enter price one value : "))
    price2 =int(input("Enter price two value : "))  
    print("Before swapping price1 :",price1)
    print("Before swapping price2 :",price2)
 
    price1,price2=price2,price1
    return price1,price2
    
price_one,price_two =swap_price()
print("After swapping price1 becomes :",price_one) 
print("After swapping price2 becomes :",price_two)

Output:

Enter price one value : 243
Enter price two value : 465
Before swapping price1 : 243
Before swapping price2 : 465
After swapping price1 becomes : 465
After swapping price2 becomes : 243

The below screenshot shows the result of the Python program to swap two numbers without arguments and with a return value.

write a program to swap two numbers using functions in python

Example 4: Swap two numbers in Python without arguments and return value.

Python full source code to swap two numbers in Python without arguments and return value.

def swap_cities_population(): 
    city_rank1=int(input("Enter value : "))
    city_rank2=int(input("Enter value : "))  
    print("Before swapping city one population are :",city_rank1)
    print("Before swapping city two population are :",city_rank2)
  
    city_rank1, city_rank2 = city_rank2, city_rank1
    print("After swapping city one population becomes  :",city_rank1) 
    print("After swapping city two population becomes :",city_rank2)  
   
swap_cities_population()

Output:

Enter value : 1337912
Enter value : 1561189
Before swapping city one population are : 1337912
Before swapping city two population are : 1561189
After swapping city one population becomes  : 1561189
After swapping city two population becomes : 1337912

Refer to the below image to see the result of the Python code to swap two numbers without arguments and a return value.

swapping of two numbers in python using function

Conclusion

I hope you like the information in the above Python article to swap two numbers in Python using a function.

In this article, I have explained with four examples how to swap two numbers in Python using functions such as with arguments and return value, with arguments and without return value, without arguments and return value, and without arguments and return value.

You may also like to read: