How to Swap Two Numbers in Python Using Function?

Recently, while working on a data-cleaning script, I needed to swap two numbers in Python inside a function. At first, it seemed like a simple task, but I soon realized that many beginners struggle with writing reusable functions for this.

I have often seen new learners overcomplicate this problem. The good news is, Python makes swapping values very easy.

In this tutorial, I’ll show you different ways to swap two numbers in Python using a function. I’ll explain each method step by step, so you can easily apply them in your projects.

Swap Two Numbers in Python Using a Function

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

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.

This example demonstrates how to swap two numbers in Python using function 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

I executed the above example code and added the screenshot below.

swap two numbers in python using function

By returning swapped values from the function, the variables update successfully with their exchanged values.

Example 2: Swap two Numbers in Python with Arguments and Without a Return Value.

This example shows how to swap two numbers in Python using arguments without returning values from the function.

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

I executed the above example code and added the screenshot below.

swap two numbers using function in python

The values are swapped and displayed directly inside the function without needing to return them.

Example 3: Swap two Numbers in Python Without Arguments and with a Return Value.

This example demonstrates swapping two numbers in Python without using arguments but returning the swapped values.

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

I executed the above example code and added the screenshot below.

write a program to swap two numbers using functions in python

The function swaps the input values internally and returns them, which are printed after swapping.

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

This example illustrates swapping two numbers in Python without using arguments or return values.

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

I executed the above example code and added the screenshot below.

swapping of two numbers in python using function

The swapping is performed and displayed entirely within the function without passing or returning values.

Conclusion

I hope you understood the information in the above Python article on swapping 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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.