0:00
Hey everyone, welcome back to this
0:02
channel. If you're new, then welcome to
0:04
this channel. Well, today we are going
0:06
to work on our very first Python
0:08
exercise and it's super important one
0:11
because that will help you to understand
0:14
how to get user input and how to work
0:17
with numbers and print the results. And
0:19
it is the first exercise from our
0:22
beginners flowchart series and we are
0:24
going to build today together step by
0:27
step. Ready? Let's go.
0:32
All right. Before we write any code,
0:34
let's quickly look at what this program
0:37
is supposed to do. Like according to the
0:39
flowchart here, we start the program and
0:42
we need two values a and b. And then we
0:45
calculate the sum which is sum equal to
0:48
a + b. And then we print the result and
0:52
stop. That's it. So here so our goal is
0:56
to take two numbers from the user add
0:58
them and show the result.
1:01
So now let's start with getting user
1:04
input. So now how do we read values from
1:07
the user? As I said before in the
1:10
previous sessions in Python we use
1:12
built-in function called input which is
1:14
something like this. So a equal to input
1:21
and you can ask the user and one more
1:24
thing input will not just take the
1:27
values from the user instead it will
1:29
also print what user can see like it can
1:32
also work something like print because
1:35
we even user must understand right what
1:38
he is giving to the computer. So for
1:40
that we can give something like enter
1:44
here itself. You don't have to print for
1:58
and enter the second number.
2:08
And this is really important here
2:10
because input always returns a string.
2:13
Even if the user types 10, Python reads
2:16
it like text 10, not the number 10. Like
2:20
for example, if you run this and you can
2:24
and you can just print type of a.
2:33
And now user will ask me the input. it
2:36
will ask the user the input. Okay, like
2:40
for example, if I give 10 and enter the
2:42
second number three and if you see the
2:44
print type of a which is actually
2:48
So it will actually take it as a string
2:51
not the number. So we have to convert
2:53
the number. So what I'll do is I'll keep
3:00
because float this little guy helps us
3:03
to convert a string into a decimal
3:05
number. Wait, why not integers?
3:09
That's a good question. Because int only
3:12
works for whole numbers, but float works
3:15
for both whole numbers and decimals. So
3:18
it's safer and more flexible, right?
3:20
Compared to int. So that's why I took
3:22
float. So let's also change it to float
3:27
and remember to keep the end bracket as
3:29
well. And now whatever the user types
3:32
Python will treat it as a number we can
3:34
work with. So like if if I rerun this
3:44
see now it's taking the in input as
3:49
All right, now time for the fun part and
3:52
let's add the two numbers. So for that
3:54
I'm going to create one more variable
4:01
plus b. It's as simple as that. And now
4:04
let's print the result. So here instead
4:07
of this we can just print
4:12
This gives you a nice clean output. But
4:27
something like this. So here comes the
4:29
complete program all together.
4:32
Now when you run this
4:37
like for example first number maybe 12.5
4:40
because we use float and second number
4:48
boom you have just written your very
4:50
first program that actually accepts
4:53
input and does calculations and gives
4:56
you the output. I'm proud of you truly.
4:59
So let's do a super quick recap here. We
5:02
have worked with input which will
5:04
actually get the text from the user and
5:07
float actually converts that text into
5:10
numbers and then here we are working on
5:13
math. So plus operator is working on
5:16
math and print we actually showing the
5:21
result using print. So the pro tip for
5:25
beginners always keep that in mind that
5:27
always convert your input if you're
5:29
doing math. Don't forget that you can
5:31
use float over int as well if there's a
5:34
chance of decimal input. So don't
5:37
overthink it. Keep it simple and build
5:42
I hope you got my point. And that's it
5:44
for exercise one. And you know how to
5:46
read two numbers from the user and find
5:48
their sum in Python. And this might seem
5:51
small, but trust me, it's one of the
5:53
most important building blocks that
5:54
you'll use again and again. If you found
5:57
this helpful, don't forget to like,
5:58
subscribe, and hit the bell icon so you
6:00
never miss the next exercise in the
6:02
Python's beginner series. And thanks for
6:05
coding with me, and I'll see you in the