0:00
Hey everyone, welcome back to another
0:02
Python exercise and this is exercise 10
0:05
and today we are going to learn
0:07
something super simple but very
0:09
important which is how to swap two
0:11
numbers using a temporary variable. And
0:14
trust me this is one of those basic
0:16
concepts that can help you understand
0:18
how memory and variables work in
0:20
programming. And also keep this in mind
0:22
that these this question is the most
0:25
repetative question when it comes to
0:26
interviews. So if you learn this one
0:29
then maybe this can help you in you know
0:32
cracking your interview as well
0:36
and let's first take a quick look at the
0:39
flowchart before jumping into the code.
0:41
So here is what happens step by step. So
0:44
we start by reading two numbers and
0:46
let's call them num one and num two and
0:49
we introduce a third variable called
0:52
temp which is here to temporarily hold
0:57
the value of num one. So then we assign
1:01
the value of num two to num one. I hope
1:05
you are getting my point. And finally we
1:08
put the value stored in temp into num
1:13
So it's just like that the values are
1:15
actually swapped. So like for example
1:17
num 1 equal to 5 and num 2 equal to 8
1:21
and there is a temporary variable there
1:23
is nothing in that like for example
1:26
this five is assigned in temp
1:29
this 8 is assigned in one and then this
1:34
five is assigned to num two. So we are
1:38
just swapping the numbers and we are
1:40
just changing the numbers through
1:42
containers so that we can swap two
1:44
numbers. That's it. Just using a
1:46
temporary variable to swap and then we
1:49
are displaying the number one and number
1:51
two after switching it. So finally we
1:55
put that value stored in the temp and
1:57
then into the num two. So just like that
2:00
the values are actually swapped and
2:02
let's move to the code now. But before
2:06
that I want you to explain it a bit more
2:07
clear. Like for example num 1 equal to 5
2:10
and num 2 equal to 8. There is a
2:13
temporary variable box and you moved
2:15
five to temp and 8 to num one and five
2:21
and that temp to num two. It's just that
2:28
So let's write program or if you guys
2:31
are really interested and if you guys
2:32
have knowledge in this just keep this
2:35
flowchart in front of you just pause
2:36
this at this moment and you can start
2:38
coding. If you still have any doubts or
2:42
if you're a beginner you can just start
2:43
coding with me that's fine.
2:48
So just give num num one equal to input.
2:59
Enter the value of num one.
3:23
So here we are actually
3:30
the inputs from the user.
4:08
or you can just skip equal to or
4:39
a temporary variable.
4:46
So temp equal to num one
4:50
and num one = to num two
4:55
and then num two will be the temporary.
5:00
So temp equal to num one and num one
5:03
will be num two and num two will be
5:05
temporary. So now we just have to
5:19
So you can just print
5:30
print. You can just you know just copy
5:33
paste these two things.
5:41
So this is what the swapping is all
5:46
I hope you are getting my point. Like
5:58
So here we are actually showing them.
6:00
And now temp equal to num one which
6:06
here and here the num two will be the
6:10
num one will be the num two. So what is
6:13
the value of num2 which is five. So five
6:16
will be inserted here and n2 equal to
6:19
temporary. So what is the temporary
6:24
and what's the value of it? H right.
6:27
So if you see here initially num 1 8 and
6:31
num 25 but now the number one five and
6:35
28 I want you to take a pen and paper I
6:39
would suggest you to write and you know
6:41
try to trace it line by line that is how
6:45
you learn how programming exactly works
6:47
how each line will be executed that's
6:51
the beauty of it if you write on you
6:54
know with pen and paper
6:57
That's the beauty of it. You will
6:58
understand the beauty of it. So this is
7:01
what we are doing in order to swap two
7:03
numbers using the third variable.
7:09
So if you see here we are actually
7:12
taking the input from the user and let's
7:14
say you know five and eight.
7:19
So before the swap we display the
7:21
original values and then here we are
7:24
actually swapping and then after
7:26
swapping we are displaying the values.
7:30
So that's pretty straightforward right?
7:33
But even though it's simple the swapping
7:35
method is used in so many real world
7:39
Try this out yourself. Play with
7:44
and see the magic happen. If you want, I
7:47
can also explain how to swap two numbers
7:50
without using a temporary variable. And
7:52
trust me, that one's even more fun. So,
7:55
just comment down below if you need that
7:57
logic or if you need the explanation of
8:00
that logic. So, so don't forget to hit
8:03
like, subscribe, and comment below if
8:04
you understood the logic or if you want
8:06
me to explain any part again. Just let
8:09
me know and catch you in the next one.