0:00
Hey guys, welcome back to this channel.
0:02
If you're new, then welcome to this
0:03
channel. And if you're just joining, you
0:05
are at the right time because today's
0:07
video is short, simple, and super useful
0:10
because we are writing a Python program
0:11
to calculate the area of a circle using
0:14
the radius entered by the user. Sounds
0:18
easy. That's because it is. But there
0:21
are a couple of small things we need to
0:23
talk about like how to use pi and how to
0:26
convert input to numbers and how to
0:29
avoid common beginner mistakes. And also
0:32
I've made a few more exercises before
0:35
this episode as well. If you want to go
0:37
through that as well, I would love to
0:39
encourage you to go there and check the
0:42
basics as well. I have videos on the
0:44
basics as well. And these exercises are
0:46
actually as part of those particular
0:48
series because once you're handy with
0:50
these basics then we can move on to you
0:53
know more complex stuff yet that can be
0:56
easy for you to understand.
0:59
So let's start with the flowchart and
1:02
here you will start always and uh here
1:04
we need radius. We have to take radius
1:07
as input from the user and then we will
1:09
use this formula which is pi into r into
1:12
r and then we have to show the output
1:14
area and then we'll stop.
1:17
So in simple terms the user types the
1:20
radius and python calculates the area
1:23
using the formula and then we'll display
1:25
the result. But before we jump into code
1:28
let's talk about a few key things. So
1:31
the formula to calculate the area of
1:36
So that means you take the radius and
1:39
multiply it by itself. So that's you
1:43
know it's something like a squared part.
1:46
So multiply that result by pi which is
1:50
this one. Now in Python we can write r
1:54
into r to get the square of a number or
1:58
you can also use exponentiation that we
2:01
have used which is where you'll have
2:03
double multiplications at the same time.
2:07
So both will work but wait where does
2:10
you know pi come from? How do how can we
2:16
So you have got two options. One is
2:19
hardcode it manually something like by
2:22
giving pi equal to 3.14159
2:25
or you can let Python give you a super
2:28
accurate version for the math module
2:31
like it will just work for mathematical
2:33
calculations. So we also have that
2:36
Python version. So all you have to do is
2:39
you have to you have to import this
2:42
math. You don't have to worry from where
2:44
it's coming. You just have to write this
2:46
line called import math and then you can
2:50
just give pi equal to math dot pi
2:55
and then it'll it'll get the exact value
2:58
of pi or you can just give something
3:06
which is you know the actual number pi
3:11
Okay. So personally I like using math.pi
3:16
because it's cleaner, more precise and
3:18
easy to remember. We'll go with that. We
3:20
don't remember this 3.14159 every time,
3:23
right? So that's why I prefer this one.
3:27
All right, let's get input input from
3:29
the user. So all you have to do is
3:32
create a variable radius
3:44
Enter the radius of the circle.
3:55
But remember this gives a string right
3:58
like a string five. We can't do math
4:01
with string. So let's convert it to
4:02
number. So I'm using float.
4:10
Mhm. And now we are ready for math.
4:14
So once we have the radius in pi, the
4:16
formula is super simple. You have to do
4:19
area equal to math dopi
4:23
or you can just give pi
4:35
Right? We don't have r because we wrote
4:39
it as radius that is fine. So pi into
4:43
radius into radius or you can also do
5:03
into two. So this is same as this one no
5:08
change but here we are using the power
5:09
operator. I I hope you have completed
5:12
the operators video and you're here. I
5:16
think you know what this exponentiation
5:18
is all about. So both of them works but
5:21
let's use this one and let's show the
5:24
result using a nice print statement. So
5:27
all you have to do is print
5:51
This is the basic version
5:54
but let's try the better version with
5:56
formatting which is f strings.
6:00
So f the area of the circle is
6:05
or you can write the area of the circle
6:31
and we need this in two decimals.
6:42
and that's it. This makes sure your
6:44
result is rounded to two decimals and
6:50
So now let's try running this one.
6:58
Like for example, enter the radius maybe
7:01
4 cm. The area of the circle with four
7:05
radius is 50.27 which is into r²
7:11
All right, here is the complete program.
7:15
All right, here is the complete program
7:17
which is nice and clean, right? Like you
7:19
just have to import math and you can
7:21
just give radius equal to if you want
7:24
you can also ignore this one.
7:27
Just do this and you can directly give
7:30
math py pi. That also will work
7:36
something like this. And then you can
7:38
just print. Let me remove these
7:40
additional lines so it'll be easy for
7:43
see. So this is how you'll optimize the
7:46
code. You can either create a variable
7:48
and just add math.pi in that in that or
7:51
you can directly use that here.
7:55
So try it out. You can try again. I'll
7:58
show you once again. If you enter the
7:59
radius five like for example.
8:02
So the area of the circle with the
8:07
which is simple and done.
8:11
So let's recap what we have learned. So
8:14
here we worked with input. It is used to
8:17
take the values from the user. And
8:19
always always convert input to float
8:22
when dealing with decimals and use
8:26
when you're working with pi and when you
8:29
deal with more accurate calculations.
8:31
And always format the result with 2F to
8:36
get the clean output especially when
8:38
you're dealing with when you need output
8:41
with two decimals. So if you don't want
8:44
to import the whole math module, you can
8:47
also do this. You can just give this
8:52
and here you can just
8:55
uh do something like from math
9:00
import you can just import pi you don't
9:03
need you know whole math for that and uh
9:08
and then pi will directly deal with this
9:13
so and that's it so this is exercise
9:16
three and we have officially written in
9:18
a program that calculates the area of a
9:20
circle like a pro. If you have enjoyed
9:22
this, hit the like button, subscribe,
9:23
and tap the notification bell. And we
9:26
have got a ton more fun and, you know,
9:29
practical Python exercises coming your
9:31
way. Next up, we'll take this one step
9:34
further with a little twist. So, don't
9:36
miss it and thanks for learning with me
9:38
and I'll see you in the next one.