Swap Two Numbers in Python Without Using a Third Variable | Beginner Python Tutorial Exercise #11
Aug 7, 2025
Welcome to PyBeginners ā your go-to place to learn Python programming the easy way! š
Whether you're starting from scratch or revisiting the basics, this channel is built just for YOU. We break down Python concepts into bite-sized, beginner-friendly videos using real-life examples, everyday language, and fun visuals.
Think of PyBeginners as your coding buddy ā here to help you:
ā
Understand the why behind every concept
ā
Build confidence, one small step at a time
ā
Learn coding without feeling overwhelmed
š Check out more learning resources:
š Blogs: https://www.pybeginners.com/
š¼ LinkedIn: https://www.linkedin.com/newsletters/pybeginners-7267881715929415681/
š Free eBooks & PDFs: https://www.usandopy.com/livros/
š New videos every week ā covering Python basics, hands-on exercises, projects, and career tips!
š Subscribe, join the PyBeginners family, and let's make Python simple together š»š¬
#LearnPython #PyBeginners #PythonForBeginners #PythonMadeEasy
Show More Show Less View Video Transcript
0:00
Hey friends, welcome back to Python
0:02
series. This is the space where we make
0:04
Python fun and easy for everyone. So in
0:07
today's Python exercise, we are going to
0:09
learn something that seems like a magic
0:12
trick at first, which is swapping two
0:14
numbers without using a third variable.
0:17
So in the previous video, we worked with
0:20
swapping two numbers using a third
0:23
variable, which is temporary variable.
0:25
But now we are going to do that without
0:28
using the temporary variable. If you
0:30
have if you guys have missed that
0:32
episode, I'm going to keep that in the
0:34
description. You can just go there and
0:38
go through that exercise and just come
0:39
back here and you can continue from here
0:42
to learn to start with this one.
0:46
So here is the goal. We'll ask the user
0:49
to enter two numbers and let's say user
0:52
enters five and even you know
0:55
8 and without using any temporary
0:58
variable we will switch their values. So
1:00
at the end a becomes 8 and b becomes
1:04
five. So here is the logic and we will
1:07
use this simple math for it. No magic
1:10
involved just addition and subtraction.
1:12
So here is the flowchart. We'll start
1:15
with reading the values number one and
1:17
number two which is it could be a or b.
1:21
And then we are going to do this math.
1:24
A= to A + B, B= A minus B and A= to A
1:29
minus B. And then we will display the
1:31
result which is the swapped numbers.
1:35
So it looks simple. So let's write it in
1:38
Python.
1:42
So here is the previous code. So we are
1:44
going to take two numbers or I can
1:47
rewrite it once again if you guys are
1:49
okay.
1:51
So ask
1:54
the user to enter the values.
2:01
So a equal to input
2:08
enter the value of a
2:16
and b equal to input
2:23
enter the value of b.
2:30
Mhm. Okay.
2:34
And now we are going to display the
2:36
numbers before swap.
2:43
So print I'm using f strings here.
2:47
So a = a
2:54
b = b
3:05
and then we will write logic to swap the
3:09
numbers.
3:14
But if you observe, if you guys are, you
3:18
know, if you guys have come up with the
3:20
previous exercises and you're still here
3:22
with me, you might notice that there's a
3:25
mistake that I'm doing here. Like even
3:27
in all the exercises, I was highlighting
3:30
that just one thing where we have to
3:33
convert the type of a variable
3:36
especially when we are dealing with
3:37
mathematical operations. If you observe
3:40
the previous exercise, we didn't do any
3:43
type conversion like we didn't convert
3:45
the value of a to integer and value of b
3:48
to integer because there is no chance to
3:51
work on mathematical operations in that
3:54
particular situation. But here we have
3:57
to swap the numbers. So in order to do
4:00
that we should not use the third
4:03
variable and then we have to do the
4:05
mathematical calculation here. So can we
4:07
calculate using the strings? Like can we
4:10
add two strings? Yes, you can do that.
4:12
But that's a different story.
4:16
Can you add two strings to get a number?
4:20
No. Right? We need that input to be
4:23
integers in order to calculate them. So
4:27
always use type conversions
4:30
if you want to calculate anything.
4:37
So I you know change the type from
4:42
string to integer. And now let's work on
4:46
the calculation. We already have
4:48
algorithm. So we are just going to work
4:50
on this one. A equal to a + b b= a minus
4:53
b and a equal to a minus b. Don't think
4:56
that what's happening here. Why are we
4:58
doing that? Because once we trace it,
5:00
we'll understand why we are writing this
5:02
logic.
5:04
So just do it. A = a + b
5:10
and b = a minus b
5:16
and a equal to
5:20
a minus b.
5:23
So now we are going to display the
5:25
results after swap. So display
5:31
after
5:33
swap.
5:36
So, print
5:41
after the exchange or after swapping
5:43
anything will work.
5:52
And you're just going to write print
5:56
f
5:59
a = a
6:06
comma b = b.
6:10
Let's break this down. So if you enter a
6:14
= 5. So here a = 5 and
6:21
b = maybe 8. Okay, a = 5 and b = 8.
6:30
And here is what happens behind the
6:32
scenes. If you see what is a + b, a 8 +
6:36
5 13, right? And
6:41
here we are doing b equal to a minus b.
6:44
Now what is the value of a is 13.
6:48
So 13 minus
6:51
what is b? 8. So 13 - 8 equal to five.
6:58
So what is the value of b now? 5. Right?
7:02
I hope you are getting my point. And now
7:06
what is the value of a? 13 itself. 13
7:10
minus
7:13
what is the value of B now it's five
7:16
okay always try to trace the logic
7:19
keeping the pen and paper is the most
7:22
important thing at this point I'm trying
7:24
to break it down in the comments but I
7:26
would suggest you to keep the pen and
7:28
paper and try to trace the logic
7:32
and what is the value of B now it's five
7:35
right so 13 - 5 is nothing but 8. So
7:40
this is what happens. If you see in the
7:43
beginning A is 5 and B is 8. But now B
7:47
became five and A became 8. It just
7:49
swapped just like that by just using
7:51
this logic. At first we added two
7:54
numbers and we stored it in A and then A
7:58
minus B which is nothing but five and
8:01
then again A minus B so which is 8.
8:06
So after swapping a is 8 and b is five.
8:09
So this is how we can swap any two
8:11
numbers without any extra variable.
8:15
And why this is important? Why this
8:18
exercise is important? Well, it teaches
8:21
you how memory exactly works like how
8:24
variable stores data and how operations
8:28
can be optimized. And this trick is
8:30
often asked in interviews too because so
8:34
it's a good one to remember. And always
8:38
always remember to convert the input to
8:40
integer because Python takes input as a
8:43
string by default. I've told you
8:46
multiple times because this is where
8:47
beginners make mistakes. So I don't mind
8:50
though it is repetitive because it this
8:53
is really important for you guys to
8:55
remember.
8:58
So yeah, that's it for this one. If this
9:01
made sense and you enjoyed it, do me a
9:04
small favor. Just like it and comment
9:06
what values you tried. And also if you
9:10
have any errors, then you know try
9:12
commenting it. And also subscribe if
9:14
you're learning Python with me and let
9:16
me know in the comments should we try
9:18
swapping with multiplication and
9:20
division next. Can we do that?
9:24
So in the next exercise we will work on
9:27
another Python basics. So this is the
9:30
last exercise of this Python series. For
9:32
these you know I think all these
9:35
exercises have covered the basics that
9:37
we have gone through in the previous
9:39
episodes. So just to get you guys a grip
9:42
on the basics. We just made this many
9:46
exercises. So though it takes more time
9:48
for you to I would suggest you to go
9:50
through each and every exercise and try
9:52
to solve it. I don't want you to see the
9:54
entire video. I don't want you to do
9:57
that. I just want you to get the point
9:59
that these things are really important.
10:02
I want you to keep these things in mind
10:04
because if you like it is common in
10:07
almost all programming languages.
10:10
Like if you learn these common basics
10:13
then these are actually implemented in
10:16
all programming languages. So if you
10:18
learn these things then other
10:20
programming languages also will be easy
10:22
for you to understand and it'll be
10:24
you'll be very fast in learning them
10:26
because you already know these basics.
10:28
So there will be just syntax difference.
10:32
So this is the last exercise and in the
10:35
upcoming videos we are going to learn
10:37
some more important concepts of Python
10:39
programming.
10:40
And then from this point I could say
10:43
you're not a beginner anymore. You you
10:45
came to an intermediate level if you
10:48
have solved it by yourself.
10:50
So yeah see you in the next one and all
10:54
the best for your learning.

