Python 3 Program to Add Two Numbers Using While Loop in Terminal Tutorial For Complete Beginners
41 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 how to add
0:04
two numbers inside Python uh using while
0:07
loop loops are very much important if
0:10
you are going for a job this question
0:13
might be asked for you it's a
0:14
competitive
0:16
question
0:18
so specifically we are talking about
0:20
while loop so let me show you the
0:23
program here so we actually declare the
0:26
variables
0:29
let me take a is equal to 10 b is equal
0:32
to 5 we have declared two variables
0:35
which we will be adding we will not be
0:38
adding directly 10 + 5 we will not be
0:41
using
0:42
this directly instead we'll be using the
0:45
while loop for this we need two separate
0:47
variables first variable where we'll be
0:50
storing the sum so this will be zero sum
0:52
is equal to zero and also specifically
0:56
for this process we need one more
0:58
variable which is for keep on checking
1:01
that uh whether the value is increased
1:05
or decreased so for this we need this
1:07
counter variable so for this approach we
1:12
need two more extra variables here one
1:15
more extra variable which is counter so
1:17
now let me show you how this process
1:19
works so the way by which we use while
1:22
loop is we use the while keyword and
1:25
here we simply compare the value of
1:28
counter if the counter variable is
1:30
smaller than the value of
1:34
b and what we will do here we will
1:36
increment the value of we can also write
1:40
like this sum plus equal to 1 so this
1:42
simply
1:43
means the shorthand notation for this so
1:47
we're incrementing the value
1:53
of by one every
1:55
time and also we will do this for the
1:59
same for counter as
2:01
well and after that if the counter value
2:05
is
2:07
uh greater than the value of B when this
2:11
condition evaluates to
2:13
true then we will break out of this loop
2:17
and whatever is the value which is
2:20
stored inside
2:21
sum we will try to print this out so now
2:25
how the program will work right here
2:27
initial value of a is 10 b is equal to 5
2:32
so when this condition hits counter is
2:34
zero so 0 is smaller than five so this
2:38
condition will evaluate to true so now
2:41
sum will become one counter will also
2:43
become one and in the next iteration
2:46
then it again will check one is not
2:50
greater than five so again it will come
2:52
so sum value was 1 so 1 + 1 comes out to
2:56
be 2 and counter value will also be
2:59
increased so 1 + 1 is equal to 2 so 2
3:03
again is not greater than 5 so again 2 +
3:09
1 3 3 so this will keep on repeating
3:14
itself when the condition will hit which
3:17
will be the value of counter will become
3:20
six so here it will say six is not
3:24
smaller than five so this will break
3:26
out and now the sum value will be five
3:31
so if you print if you run this so now
3:35
the value of sum is equal to
3:37
5 so 10 + 5 is not equal to 5
3:41
so what we have done using this while
3:44
loop we have captured everything which
3:46
is stored inside B and stored this
3:48
inside this sum variable so now in the
3:52
next step what we need to do sum is
3:54
already is equal to 5 so what we need to
3:56
do we need to just increment whatever is
4:00
there inside the a so 10 + 5 is equal to
4:04
15
4:05
so so here we simply
4:09
say we do this step and then we print
4:12
out
4:15
sum so this is 15 so essentially we do
4:20
use this plus operator but uh this is
4:23
actually the way by which you can use a
4:25
while loop and still add two numbers so
4:29
this may sound something repetitive
4:31
because we are repeating the it is
4:34
taking way long we can directly write a
4:37
+ b which will do the addition for us
4:40
but this is actually the way by which
4:43
you can use the while loop so
4:46
essentially this while loop is
4:48
transferring everything which is there
4:50
inside B into sum so sum becomes five
4:54
and then in the next step we are
4:57
uh incrementing what is there in the a
5:00
so a a plus
5:03
sum right here and then we print out the
5:05
result so in this way uh hopefully you
5:08
have understood how we use the while
5:10
loop for addition of two
5:13
numbers and also check out my website
5:16
freemediattools.com
5:18
uh which contains thousands of tools