How to Swap Two Numbers Without Using Temp Variable in Python – GATE, Interview & Placement Practice
22 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 python
0:04
program which is asked in various
0:06
placement coding examinations
0:08
competitive exams such as gate so this
0:12
is actually how to swap two variables
0:14
without using the third variable so this
0:18
is actually the program that i written
0:20
swapping means that we need to exchange
0:22
both the numbers or both the variables
0:25
so you actually run the program first of
0:27
all let me run this
0:30
so as you run the script here you need
0:32
to you need to be entering two numbers
0:35
so enter the first number i write five
0:38
enter the second number and i write
0:40
eight so after executing this program
0:43
now the both these numbers will be
0:45
swapped 8 will become the first number
0:47
five will become the second number so if
0:49
i enter it you will see before swapping
0:51
it was 58 after swapping it is 85 so now
0:56
the how to do this without using the
0:58
third variable this is actually the
1:00
program so let me paste this program and
1:04
show you step by step using this
1:07
software
1:09
pythonutor.com which will actually
1:11
visualize the execution of the program
1:13
step by step so first of all we are on
1:15
this step it is asking you to enter the
1:18
number so i will say five then it moves
1:21
to the second step
1:23
this variable has been registered x is
1:26
stored inside five now we enter the y
1:30
value which is
1:31
10 so now both these variables have been
1:34
assigned in global variables x is
1:36
holding five y is holding 10 now before
1:40
swapping you will see it is 5 10 now we
1:44
come to the logic how we do this without
1:46
using the third variable first of all uh
1:49
the first step here is to calculate the
1:52
sum of both these numbers which is 5 +
1:56
10 so we calculate the sum the sum comes
1:59
out to be 15 and we store this 15
2:03
variable 15 value in the x variable so
2:07
the first step is this x is equal to x +
2:10
y if you do this now you will see the
2:13
value is replaced here of x we add both
2:17
these numbers and then store this into x
2:19
so x is now 15 then the next step is we
2:23
actually calculate the we subtract both
2:27
these numbers so now x is
2:30
15 y is 10 so 15 minus 10 comes out to
2:34
be five and we store this value inside
2:38
your y variable so now as we execute
2:41
this step now y will become five
2:46
so as you can see now y has become
2:50
five and now in the next step we again
2:53
need to calculate the we need to
2:55
subtract both these numbers which is 15
2:58
minus 5 which comes out to be
3:01
10 so now the value 10 will be replaced
3:06
here of
3:08
x so in this easy way you can see both
3:11
the numbers have been swapped and uh
3:14
using these arithmetic operations and we
3:17
haven't used the third variable as you
3:19
can
3:20
see now in the next step after swapping
3:23
it is 10 five so just make see uh both
3:27
these operation first of all we add both
3:29
these numbers 10 + 5 we store it inside
3:35
x and then we do the subtraction 15 - 10
3:40
comes out to be five which
3:43
we so here you can see the value is
3:46
swapped of x it stored inside y and then
3:49
again we calculate the subtract comes
3:52
out to be five
3:54
sorry 10 so we replace
3:57
it so in this easy way
4:03
uh you can do this so you can take any
4:06
example for example if you take uh any
4:10
number it can swap here also if i run
4:14
this if i say 100 and then
4:18
200 can see so this is a very simple
4:21
logic very simple program ask inside
4:24
interviews and placements how to swap
4:27
two numbers or variables without using
4:29
the third variable so in this way you
4:31
can do this uh please hit that like
4:33
button subscribe the channel and also
4:36
check out my website freemediatools.com
4:39
uh which contains thousands of