Python Program to Swapping two numbers | Learn Easily with 3 steps | (With & without temporary variable) | Latest Technology Updates

Swapping two numbers using temporary variable and without temporary variable from this tutorial you can learn easily within 3 easy steps without tension.In this program we are using a temporary variable temp to swap the two numbers. We are storing the value of num1 in temp so that when the value of num1 is overwritten by num2 we have the backup of the num1 value, which we later assign to the num2. From this tutorial we are going to see two Python programs to swap two numbers.

Swapping Methods in Python Programming 

  1. Program to swapping the numbers using temporary variable
  2. Program to swapping without using temporary variable.

Python_Swap_Latest_Technology_Updates

 Program to swapping the numbers using temporary variable

# Program published by- https://ramrw7.blogspot.com
# Python program to swap two variables

num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')

print("Value of num1 before swapping: ", num1)
print("Value of num2 before swapping: ", num2)

# swapping two numbers using temporary variable
temp = num1
num1 = num2
num2 = temp

print("Value of num1 after swapping: ", num1)
print("Value of num2 after swapping: ", num2)

Output

Enter First Number: 123
Enter Second Number: 456
Value of num1 before swapping:  123
Value of num2 before swapping:  456
Value of num1 after swapping:  456
Value of num2 after swapping:  123


Program to swapping without using temporary variable.

# Program published by- https://ramrw7.blogspot.com

# Python program to swap two variables

num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')

print("Value of num1 before swapping: ", num1)
print("Value of num2 before swapping: ", num2)

# swapping two numbers without using temporary variable
num1, num2 = num2, num1

print("Value of num1 after swapping: ", num1)
print("Value of num2 after swapping: ", num2)

Output

Enter First Number: 123
Enter Second Number: 456
Value of num1 before swapping:  123
Value of num2 before swapping:  456
Value of num1 after swapping:  456
Value of num2 after swapping:  123

Real Time Output 

Python_Swap_Latest_Technology_UpdatesPython_Swap_Latest_Technology_UpdatesPython_Swap_Latest_Technology_Updates

Post a Comment

If you any doubts, Please let me know

Previous Post Next Post