How to Exchange Two Input Values in Python


This example will explain how to exchange two variable values in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# Enter a variable from the terminal and cast it to an integer type.
x = int(input('Please input  x : '))
y = int(input('Please input y : '))

# Create temporary variables and exchange
temp = x
x = y
y = temp

print('The value of x after exchange: {}'.format(x))
print('The value of y after exchange: {}'.format(y))

Output:

Please input  x : 3
Please input y : 9
The value of x after exchange: 9
The value of y after exchange: 3
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments