All Python Programs

All Programs

Basic Python Programs

Simple Python programs

If Condition Programs

If condition based Python Programs 

Looping based programs

program based on for loop, while loop, range

Searching and Sorting

Python Searching Programs
Python Sorting Programs

Python program to swap two numbers

Solutions Posted By : Aditya Saxena, CSE, IPS Gwalior

Solution without using functions and using third variable
Solution without using functions and without using third variable
Solution without using functions and using only a single line
Solution using functions without passing arguments
Solution using functions and by passing arguments
Solution using functions and by passing arguments and returning multiple values


Solution : Without using function
Program/Source Code

Python program to swap two numbers

#using third variable
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
print("1st value ",a," 2nd value ",b)
t=a
a=b
b=t
print("1st value ",a," 2nd value ",b)

Output:

Enter 1st no 10
Enter 2nd no 20
1st value 10 2nd value 20
1st value 20 2nd value 10
>>>



Solution without using functions and without using third variable
Program/Source Code

#without using third variable
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
print("1st value ",a," 2nd value ",b)
a=a+b
b=a-b
a=a-b
print("1st value ",a," 2nd value ",b)

Output:

Enter 1st no 10
Enter 2nd no 20
1st value 10 2nd value 20
1st value 20 2nd value 10
>>>



Solution without using functions and using only a single line
Program/Source Code

#using only a single line
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
print("1st value ",a," 2nd value ",b)
a,b=b,a
print("1st value ",a," 2nd value ",b)

Output:

Enter 1st no 100
Enter 2nd no 200
1st value 100 2nd value 200
1st value 200 2nd value 100
>>>



Solution using functions without passing arguments
Program/Source Code

#function definition
def swap():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    print("1st value ",a," 2nd value ",b)
    a,b=b,a
    print("1st value ",a," 2nd value ",b)

#function calling
swap()

Output:

Enter 1st no 3
Enter 2nd no 6
1st value 3 2nd value 6
1st value 6 2nd value 3
>>>



Solution using functions and by passing arguments
Program/Source Code

#function definition
def swap(a,b):
    print("1st value ",a," 2nd value ",b)
    a,b=b,a
    print("1st value ",a," 2nd value ",b)

#function calling
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
swap(a,b)

Output:

Enter 1st no 25
Enter 2nd no 96
1st value 25 2nd value 96
1st value 96 2nd value 25
>>>



Solution using functions and by passing arguments and returning multiple values
Program/Source Code

#function definition
def swap(p,q):
    p,q=q,p
    return p,q

#function calling
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
print("Before swap 1st value ",a," 2nd value ",b)
a,b=swap(a,b)
print("After swap 1st value ",a," 2nd value ",b)

Output:

Enter 1st no 100
Enter 2nd no 200
Before swap 1st value 100 2nd value 200
After swap 1st value 200 2nd value 100
>>>

Previous :
Next :

You May Also Like:

Python Program to print table of a number
C Language Program to Find Factorial of Number Using Loop
Python Program to Print Fibonacci Series
Python Program to check character is a vowel not
C Language Program for Prime Number upto 100

Tutorials

Technical Questions and Important programs

Interview Questions

C Programming
C++ Programming
Basic Python Tutorial
Advanced Python Tutorial
C Language
C++ Programming
Python programming
C Important Programs
C Interview Questions
C++ Interview Questions
Python Interview Questions
HTML Interview Questions