How to Swap Two Variables Using Temp Variable in Python – GATE, Interview & Placement Practice
181 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a very
0:04
common problem which is asked inside
0:06
coding placements and competitive exams
0:09
such as GATE which is how to swap two
0:11
numbers using a third temporary variable
0:16
so this is really easy program so if you
0:19
want this is actually the Python program
0:21
that I written so if I execute this
0:24
program it will ask you to enter two
0:26
values enter the value of a so let's
0:29
suppose I write 15 then we write the
0:32
enter the value of B so if I write 25 so
0:35
as I execute this program both the
0:37
values will be swapped uh 25 will become
0:41
15 and 15 be will come 25 so as you can
0:45
see now the value of a after swapping is
0:48
25 and b is 15 so we are simply
0:52
exchanging both the variables value
0:55
using the third variable so we how we do
0:58
this it's very simple logic let me show
1:01
you this
1:03
program inside this software which is
1:06
Python
1:07
tutor which will show you step by step
1:10
how the code executes so first of all we
1:13
take the input here we take both the
1:15
values so if I write 15 and then I
1:19
write 10 for B so as you can see both
1:24
the variables have been assigned here
1:27
are stored as global variables a is 15 B
1:31
is 10 now we come to the logic so we
1:34
declare here this third variable it is
1:37
it can be called as anything so I'm just
1:39
calling it as temp so temporary variable
1:43
so inside this we store the value of
1:46
whatever it's stored inside a which is
1:49
15 here so we store it inside your new
1:52
variable which is
1:54
temp so we store it 15 so after we
1:57
execute this you will see it will create
1:59
a new variable temp which is storing 15
2:02
right
2:03
here so we create a third variable after
2:06
that whatever is the value present
2:09
inside B which is 10 we are replacing it
2:13
in the A variable so we are swapping it
2:17
simply equaling A is equal to B so
2:20
whatever is present in B is transferring
2:23
it in A so now after we execute this you
2:26
will see A value will change to 10 and
2:30
now whatever is present in your
2:32
temporary variable we just need to
2:34
transfer this to B so very simple logic
2:37
as you can see using the third variable
2:39
it becomes very easy to swap two numbers
2:42
so if I execute now B value will be 15
2:46
so you can see both the numbers have
2:47
been
2:48
swapped and using this third temporary
2:51
variable so now we can print out the
2:54
value of A after swapping is 10
2:58
similarly the value of B after swapping
3:01
is 15 so it's a very simple program in
3:05
which we do how to swap two numbers
3:08
without using the third variable which
3:10
is
3:12
temp so this is the program here it is
3:17
commonly asked inside interviews
3:19
whenever you go for programming
3:21
interviews competitive exams as well and
3:24
like it so this is actually the logic
3:29
so please hit that like button subscribe
3:31
the channel and also check out my
3:33
website freemediatetools.com
3:35
uh which contains thousands of tools
#Programming
#Scripting Languages