write a python programto swap two numbers...
Answers
Answer:
# Python program to swap two variables
x = 5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
Python program and the output for the above question is listed below:
Output:
If the user input as 4 and 5, then the output is 5 and 4.
If the user input as 6 and 7, then the output is 7 and 6.
Explanation:
first_number =int(input("Enter the first number for swapping: "))#Take the first number.
Second_number =int(input("Enter the Second number for swapping: "))#Take the second number.
#Swap the number.
Third_number=first_number
first_number=Second_number
Second_number=Third_number
print("The first number and the second number after swapping is:",first_number,"and ",Second_number)#Print the number after swapping.
Code Explanation :
- The above code is in python language, in which the first and the second line is used to instruct the user and take the input from the user.
- Then the three expressions are used to swap the number with the help of the third variable.
- Then the number is printed after swapping with the help of print function.
Learn More :
- Python : https://brainly.in/question/14689905