Skip to content
Home » Python Programs » Python Program to Swap Two Variables Using Function

Python Program to Swap Two Variables Using Function

Basic python program to swap two variables using function. In this program we can check the python program to swap the two variables using function for beginners.

Function Definition in Python

A function in computer programming is a named segment of code that performs a defined task. This usually entails accepting some input, altering it, and returning an output.

Variables Definition in Python

A Python variable is a memory area designated for storing values. A variable in a Python program, in other words, provides data to the computer for processing. In Python, every value has a datatype. Numbers, Lists, Tuples, Strings, Dictionary, and other data types are available in Python. Variables are highly used in this python program to swap two variables using function.

Python Program to Swap Two Variables Using Function

def swap(x,y): 
    print('Before Swapping a :',x)
    print('Before Swapping b :',y)
    x,y=y,x
    return x,y 
a=int(input('Enter value : '))
b=int(input('Enter value : '))    
a,b=swap(a,b) 
print('After Swapping a becomes :',a)
print('After Swapping b becomes :',b)

Output:

Enter value : 34
Enter value : 56
Before Swapping a : 34
Before Swapping b : 56
After Swapping a becomes : 56
After Swapping b becomes : 34