0:00
Hey everyone, welcome back to simplified
0:02
by Singom. Welcome to our Python for
0:04
beginners series. Today we are going to
0:07
explore something really fun which is
0:09
finding cubes of numbers. In the
0:11
previous exercise we worked with squares
0:12
of numbers. And now you might be
0:15
wondering what's a cube? Well, just like
0:17
a square is actually a number multiplied
0:20
by itself. In the same way, a cube is a
0:22
number multiplied by itself three times.
0:26
Like for example 2 cube is nothing but 2
0:31
is multiplied by 3 * 2 into 2 into 2 is
0:35
8 2 into 2 4 4 into 2 8
0:39
and simply 4 cq 4 into 46 16 into 4 64
0:44
so that is nothing but 4 cube pretty
0:46
cool right now let's see how to write a
0:49
python program that does this
0:54
and first here is the idea idea we have
0:57
to ask the user how many numbers they
1:00
want to find cubes for and then we ask
1:03
them to type in each number one by one
1:06
using the loop and finally for each
1:09
number Python will calculate the cube
1:12
using simple multiplication and then we
1:14
print the result in a nice format. So
1:19
basically we are teaching Python how to
1:21
be our personal cube calculator.
1:25
All right, let's score this step by step
1:32
and n equal to int of input.
1:51
Okay. And then in the same way we create
1:55
numbers in the it is almost same as the
1:58
previous exercise. So we have already
2:01
done this before. And now we create a
2:14
And for i in range of
2:26
And here let's create a variable called
2:28
m equal to int of input.
2:42
And we'll add those numbers into our
2:46
numbers list using append.
2:53
So here we took how many numbers the
2:55
user want to enter and then we created
2:57
an empty list called numbers and for i
3:02
in range of 1 comma n + one like for
3:05
example user gave two numbers to add. So
3:08
we created an empty list and for i in
3:15
what is the 2 + 1 which is nothing but
3:18
three. So for I in range of 1, 3.
3:22
So then it will create a variable
3:26
called num. And then it is asking the
3:29
user enter number one, enter number two,
3:32
enter number three, something like that.
3:34
We are actually taking the user input
3:36
for every loop for every time. Okay? And
3:45
numbers so that you will see how this
4:01
when I give two numbers I want to enter
4:04
enter number one. See it is asking me
4:06
the first number maybe two and second
4:09
number maybe three. But let me give a
4:12
space here so that you can understand
4:21
So two numbers number one two number two
4:25
maybe four. See it is creating a list
4:28
called numbers and it is adding every
4:30
time the user gave the value. So here we
4:34
are taking the input from the user and
4:36
at the same time in the next line itself
4:38
we are adding that particular number
4:42
whatever the number that user gives into
4:45
this numbers list. How? By using this
4:50
So this is how we get user input for
4:53
every loop using for loop.
4:57
So this is just to understand and now
4:59
let's create a headline to create
5:04
this slash in is nothing but it will
5:06
show us the output in the next line.
5:08
That's the only thing we use slash for.
5:12
So just create a headline cubes
5:16
of the entered numbers
5:24
and then we'll write logic
5:34
You calculate and print
5:49
So the logic will be for again in order
5:52
to grab each and every number from the
5:56
list per num in numbers.
6:23
simple just multiply the number by three
6:25
times to get the cube num into num into
6:29
num and yeah that's it and this will
6:33
this logic will give you the cube of a
6:35
number as simple as that.
6:38
So like for example user gave two
6:41
numbers and those two numbers are added
6:44
here. So now the numbers value is
6:49
for example let's take the same example
6:51
2a 4. So these are the numbers. So now
6:55
it will print the cubes of the enter
6:57
numbers and for num in numbers
7:01
that means it will go to each and every
7:04
item in the loop. We are instantly
7:07
creating a variable at that particular
7:09
time when you're looping when you're
7:11
doing looping in a list. So at that time
7:14
you are instantly creating a variable
7:16
called num so that it can grab a number.
7:19
It can grab a particular thing from the
7:22
list at one particular time. So
7:25
initially it starts with 2 and it will
7:29
print 2 cube is equal to 2 into 2 into
7:33
2. So it will give you the calculation
7:35
of 2 into 2 into 2 which is nothing but
7:40
the number once it is done now the
7:43
interpreter goes again here. Now the
7:45
number value changes from 2 to 4. Okay.
7:49
And now it will print 4 cube = 4 into 4
7:52
into 4 which is nothing but 64.
7:56
So overall what's happening here we are
7:59
taking the input from the user to ask
8:02
how many numbers the user want to
8:04
calculate and then we are creating an
8:06
empty list where we will store the
8:09
numbers and in the first loop for i in
8:12
range of 1 n + 1 it takes each number
8:15
from the user and saves it inside that
8:19
list which is numbers
8:21
and then we print a heading called cubes
8:24
of the entered numbers.
8:26
And finally, the second loop goes
8:28
through each number in the list,
8:30
multiplies it by itself for about three
8:33
times, and prints the cube in a need
8:37
Simple and clear. Okay, now let's run
8:49
I want to give three numbers
8:52
2 4 5. So the cubes of entered numbers
8:57
is 2 cube, 4 cube and 5 cube. If you
9:01
want you can give you can remove the
9:05
space and you can do it again.
9:13
You can give whatever the output that
9:26
And yeah that's all. So today you
9:30
learned how to take multiple inputs from
9:32
the user and show them inside a list. We
9:35
have done it even in the squares of a
9:37
number in the previous exercise as well.
9:39
We also learned how to use loops to
9:41
perform calculations and also print out
9:44
cubes of numbers in a good way using
9:48
And here is a little challenge for you.
9:50
Try modifying this program to also print
9:53
squares and cubes together. Like for
9:55
example, 2 square is 4 and 2 cube is 8.
10:00
And that will be a fun extra step to
10:02
practice with. And if you enjoyed the
10:06
tutorial, don't forget to like this
10:07
video, subscribe to the channel, and hit
10:09
that notification bell so you'll always
10:12
always stay ahead in our Python learning
10:14
journey. And remember, coding is like
10:17
building blocks. Once you understand the
10:19
basics, you can create anything.
10:22
See you in the next video and until
10:24
then, keep coding and keep smiling.