0:00
Hey everyone, welcome to Simplified by
0:02
Singum. Welcome back to our Python for
0:04
beginners series. Today we are going to
0:07
do something super fun and practical. We
0:09
are going to take numbers from the user
0:11
and print their squares.
0:14
Squaring numbers is a great way to
0:16
practice loops, user input, and
0:18
calculations in Python. Let's jump right
0:21
in. So our flowchart breaks down the
0:24
program like this. First we ask the user
0:27
how many numbers they want to square and
0:31
let's call this n and then we ask the
0:35
user to input n numbers
0:39
first how many numbers and then you ask
0:42
all the numbers n numbers one by one and
0:45
for each number user entered we
0:48
calculate its square by just using this
0:52
formula which is i into i number into
0:56
and we print the square in a neat
1:02
It's like telling Python, hey Python, I
1:04
have some numbers for you. Show me their
1:08
Simple and straightforward.
1:11
Let's turn that flowchart into a real
1:25
and equal to integer. We need input and
1:28
integers. Input enter
1:42
Okay. And then you will create a list
1:55
for every loop you ask the user enter
2:14
you will create one more variable called
2:17
num and you just ask the user input.
2:23
I'm using a strings here. enter number
2:37
you will add that into the list by using
2:40
numbers dotappend. We already learned
2:42
about list. We know how to use this
2:44
append. So I'm just using it.
2:49
And then you print the squares of enter
3:10
You don't need space here.
3:17
So here we loop to print the squares.
4:20
and yeah that's it as simple as that.
4:23
So let's break it down line by line.
4:25
Here we are taking user input. So this
4:29
asks the user how many numbers they want
4:32
to enter and we created an empty list to
4:35
store the user's numbers here and the
4:38
first step is to loop for in range of 1
4:41
comma n + one why I am starting with one
4:44
here because usually range method starts
4:46
with zero to end right but here you are
4:50
showing the user enter number one enter
4:53
number two you cannot say that enter
4:55
number zero right user will be like why
4:57
is Why it is starting from zero? Of
5:00
course, we are the programmers. We know
5:02
that it starts from zero. But why user
5:04
want to know? So to make things easier
5:07
for user, we make it one. So the range
5:10
actually starts from 1 to n + one.
5:14
That's it. Obviously here you are
5:16
incrementing by one. Then even you have
5:18
to do the same thing so that it can
5:21
maintain the number of loops. Balance
5:24
the number of loops. So here you are
5:29
asking number enter first number enter
5:31
second number for every loop. So this is
5:34
what it is all about. And now every time
5:37
you're taking that input you're adding
5:39
that into this empty list by using this
5:41
append method. I hope you guys already
5:44
have learned about list. If not I have
5:47
already made a video on lists as well.
5:49
You can go through list and its methods.
5:52
have a good practice and then you can
5:53
come back and start working on this
5:59
So for every loop you are adding this
6:03
Okay, let's see until this one. For
6:07
example, you want two squares of a
6:10
number for i in range of
6:18
Okay, 1 comma n + 1. So 1 comma 2 + 1
6:23
that means this will be 3 not 2. So 1
6:26
comma 3 for in range of 1a 3 the range
6:31
starts from 1 till 3. So initially i
6:34
value is 1. Okay let's keep one here and
6:38
number input enter number one. So this
6:42
is this will be the output and then user
6:45
will add some number here will be for
6:48
example two. Now the list will get
6:50
updated with two at first using this
6:53
append method. Okay, that means
6:59
the list will get updated with two and
7:02
again now the loop goes here. Now I
7:07
and again now it will ask enter number
7:11
I. What is the value of I? Two. So enter
7:14
number two. For example, maybe three
7:17
this time. and numbers dot append of
7:21
num. So what is this num is nothing but
7:24
this value that user gave. So now the
7:26
list will be updated to three. Okay. And
7:30
now again I value goes here and now I
7:33
value becomes three for i in range of 1a
7:40
So is i in range of three? No. It should
7:43
be less than three. Right? So it will
7:45
come out of the loop and it will take
7:48
two numbers from the user and now it
7:51
shows this output the squares of entered
7:54
numbers entered numbers.
7:58
And now let's go through this logic. So
8:00
now the numbers list have two and three
8:05
for num in numbers. Num is nothing but
8:10
first it will point to two and then it
8:12
will point to three in the next loop in
8:14
the upcoming loop. So initially the num
8:16
value is two and you're just showing it
8:19
to user that the name square equal to
8:22
the simple logic num into num 1 into 1 2
8:27
into 2 3 into 3 for squares that is what
8:29
we do. So that is the logic I wrote.
8:32
Okay. So here the output will be first
8:38
this value will be 2 into 2 is nothing
8:42
but 4 and then now the loop jumps to the
8:46
next number which is nothing but three
8:48
from the numbers and 3Β² equal to 3 into
8:52
3 is nothing but 9. So this is what it
8:54
will show you as output in every line.
8:58
So this is what's happening. This line
9:00
is asking the user how many numbers they
9:02
want to enter. And this line it creates
9:05
an empty list to store the user's
9:08
numbers. And here the first loop for in
9:13
range of 1, n + 1, it will ask the user
9:16
to input each number one by one. And
9:20
this numbers do.append it stores each
9:26
And here we are printing the squares of
9:29
entered numbers. And it will just prints
9:33
a heading for a clarity. And the second
9:36
loop goes through each number in the
9:38
list and prints its square using num
9:42
into num. And that's all. Now Python
9:45
will calculate and display the square of
9:47
each number automatically.
9:59
For example, I will give two numbers.
10:02
One is two and the other is three. So
10:06
the squares of enter numbers I got some
10:09
something wrong because while explaining
10:11
I think I made some mess. Observe this
10:14
error. You see can't multiply sequence
10:16
by non-int of type string. That means
10:19
somewhere we didn't convert the input to
10:22
integer. So here you have to give it int
10:27
whenever you're getting the input from
10:29
the user so that it will do mathematical
10:32
calculations. It is the Python is like
10:34
ar you gave me string input and how you
10:38
can expect me to do multiplication with
10:43
So in order to do multiplication you
10:45
have to convert it to integer. So that
10:47
is what it is expecting from us. So this
10:49
is why when you do it when you trace it
10:52
when you're getting errors then only you
10:53
will understand you'll learn a lot just
10:55
by you know going through errors don't
10:58
feel bad when you get errors. So when
11:01
you get errors then only you will learn
11:03
something from there. So in the process
11:06
of understanding why it is happening
11:09
that is where you will get more
11:10
knowledge of how the program is working.
11:13
So people who don't get errors
11:16
are the most dumbest people according to
11:18
me. The people who make more errors in
11:21
their lives or even in programming are
11:23
the people who will learn a lot in the
11:25
future who are spending their time to
11:27
solve a particular error are the one who
11:29
will learn better than the one who
11:31
didn't make any error at all in the
11:34
first program itself.
11:36
So don't worry about the errors. Go
11:38
through it. When you go through it, you
11:40
will understand. So here I made one
11:42
error and I don't feel bad because by
11:45
looking at it I understood that
11:46
somewhere I didn't give I didn't convert
11:49
the data type of the input. So I figured
11:52
it out and I solved it. Now I
11:56
So take each and every error as a lesson
12:12
Okay. So let's give 2 again 2 and 3.
12:17
So 2 square is 4 and 3 square is 9. So
12:20
the squares of entered numbers 2 square
12:22
is 4 and 3 square is 9.
12:25
And yeah, that's all. And let's give for
12:28
example four numbers.
12:30
And let's give 3 5 7 9 3 square 9 5
12:37
square 25 7 square 49 9 square 81. Look
12:40
at that. Python did all the calculations
12:43
for us in a clean easy to read format.
12:46
Amazing right? You are the one who did
12:49
it. So you are amazing.
12:51
And yeah, fantastic. You just learned
12:54
how to take multiple inputs from a user
12:57
and you learned how to store them in a
12:59
list and also you know how to use loops
13:02
to perform calculations on each number
13:05
and print the results neatly. And here
13:08
is a fun challenge for you. Try
13:10
modifying this program to print cubes
13:15
Comment below if you try it. And if you
13:17
found this video helpful, make sure to
13:19
like, subscribe, and hit the bell icon
13:21
so you never miss a Python lesson. And
13:23
also remember, coding is all about
13:25
experimenting, learning, and having fun.
13:28
Keep practicing and keep growing. See
13:30
you in the next one.