0:00
Hey everyone, welcome back to our Python
0:02
exercise series for beginners. In the
0:05
previous videos, we have worked on if
0:07
else and if statements and how
0:09
everything exactly works. So today we
0:12
are going to start implementing that if
0:16
else concept by creating a simple
0:18
calculator in Python and we are going to
0:22
actually build uh you know something
0:24
super simple but very useful like there
0:27
is no complicated logic no conditions no
0:30
fancy formatting just plain arithmetic.
0:33
So here's what we are aiming for. We
0:36
want a program that takes two numbers
0:38
from the users and then shows addition,
0:43
subtraction, multiplication, and
0:45
division. Pretty simple, right? And this
0:48
is a perfect first step if you're just
0:50
learning how to take input and do
0:53
calculations in Python. So before we
0:56
start coding, let's understand what
1:01
So here we will use input the function
1:05
to take user input and then we'll
1:08
convert those inputs into numbers using
1:11
float because remember input always
1:15
gives us a string in all of the
1:17
exercises we have learned it and now I
1:20
hope you know it and finally we'll do
1:23
all the calculations using basic
1:25
arithmetic operators addition
1:27
subtraction multiplication and division
1:31
So no if statements here, no loops, just
1:35
clean and straightforward code.
1:38
Okay, in the next one we are going to
1:41
work on using if statements. Don't make
1:43
it complex. Don't think it as a complex
1:46
one. It is pretty simple, really very
1:48
simple if you do it step by step. So
1:51
let's take input from the user.
1:58
I'm converting it right away.
2:05
enter the first number.
2:26
And then two equal to
2:38
enter the second number
2:48
now. So this is all about
3:03
and here we will perform
3:14
and for addition I'm creating a variable
3:19
and just add those num one and num two.
3:24
Right? So I created a variable called
3:26
addition and I'm adding two numbers and
3:28
I'm storing the result in addition.
3:32
And in the same way subtraction
3:36
is nothing but num one minus
4:16
And now it's time to display the
4:18
results. So we took two numbers and we
4:20
did addition, subtraction,
4:21
multiplication, division. And now it's
4:23
time to display the results.
4:32
So just have to print
5:29
So let's break this down line by line.
5:33
So first we ask the user to enter two
5:36
numbers. Since the input gives a string,
5:40
we wrap it with float. So we can do math
5:43
with them. And next we do all arithmetic
5:47
operations. Nothing fancy you know just
5:50
using the basic operators. Finally we
5:53
print the result. And if you notice,
5:57
we didn't format the output or do any
6:00
checks. We just printed the raw result
6:03
as Python gives it. And that's it. Your
6:06
very first calculator is ready. And
6:10
yeah, let's run the program.
6:16
And enter the first number 10. Enter the
6:19
second number five. And addition 15.0.
6:23
Subtraction 5.0. 0 multiplication 50.0
6:30
It looks good, right? Now, here is
6:33
something extra like if you enter a zero
6:37
as the second number,
6:44
I want you to pause at this moment and I
6:46
want you to take a breathe and try it
6:51
What if you give the second number zero?
6:57
for example the first number 10 second
6:59
number zero and it gave me a very big
7:03
error saying that zero division error.
7:07
So if you give second number zero your
7:10
division will break with the zero
7:12
division error. But don't worry, we are
7:15
going to handle those kinds of issues as
7:18
well in future exercises where we learn
7:21
all about conditions and error handling.
7:24
You don't have to worry about that. It's
7:27
okay. We'll go step by step and we'll
7:33
So for now you have learned how to take
7:38
and you converted input to float and
7:41
then you performed four operations using
7:44
arithmetic operators and you displayed
7:46
the result using print
7:49
and yeah that's your very first Python
7:51
program Python calculator which is
7:54
simple clean and beginner friendly.
7:58
If you coded along with me, give
8:00
yourself a pat on the back. And also let
8:02
me know in the comments if it worked for
8:04
you or if you ran into any errors. I'm
8:07
here to help. And don't forget to like
8:10
this video if you learned something
8:12
today. And subscribe if you're new here.
8:14
And hit the bell icon so you don't miss
8:17
another exercise where we'll start
8:19
working with conditional statements all
8:21
about if, l, and else. And that's where
8:25
the real fun begins. Thanks for watching
8:27
and I'll see you in the next one.