Welcome to PyBeginners – your go-to place to learn Python programming the easy way! 🚀
In this video, you’ll learn how to find factorials in Python in the easiest way possible. 🚀
This beginner-friendly Python exercise will help you practice loops, recursion, and math operations step by step.
👉 What you’ll learn:
What factorial means in mathematics
How to write a Python program to calculate factorial
Using for loop, while loop, and recursion for factorials
Building problem-solving skills through coding exercises
This tutorial is perfect for beginners who want to improve their Python basics while solving fun exercises. 💡
Whether you're starting from scratch or revisiting the basics, this channel is built just for YOU. We break down Python concepts into bite-sized, beginner-friendly videos using real-life examples, everyday language, and fun visuals.
Think of PyBeginners as your coding buddy — here to help you:
Show More Show Less View Video Transcript
0:00
Hey everyone, welcome to simplified by
0:02
Singum. Welcome back to our Python for
0:04
beginner series. So today we are going
0:06
to solve a very popular math problem
0:09
which is finding the factorial of a
0:11
number. Now don't worry if the word
0:13
factorial sounds complicated.
0:16
I promise by the end of this video
0:18
you'll not only understand it but also
0:20
write your very own Python program to
0:22
calculate it. Let's go step by step. All
0:26
right. So what does a factorial mean?
0:30
Factorial of a number
0:33
written as n factorial.
0:40
So which is written something like this
0:42
n factorial
0:45
which simply means multiplying all the
0:48
numbers from 1 to 10. So 1
0:54
into 2 into 3 into till
1:03
n.
1:05
Like for example, if we have
1:09
pi factorial
1:11
that means it'll be like 1
1:15
1 into 2 into 3
1:20
into 4 into 5
1:24
and 4 factorial is nothing but
1:26
multiplication of 1 2 3 4 3 factorial
1:29
multiplication of 1 2 3.
1:32
So it's it's pretty simple. It's just a
1:35
repeated multiplication till the limit
1:38
of the number that you gave. So here is
1:40
how the logic works according to the
1:42
flowchart. So it starts with the number
1:45
n and here we are setting up a value
1:48
called fact and we are starting it with
1:50
one because multiplying by one doesn't
1:53
change anything. So that is why we are
1:55
starting it with one. We are also using
1:57
a counter here which is i and we are
2:00
also giving it one. And then here keep
2:05
multiplying the fact by i. Fact into i
2:09
while increasing the i value by one one
2:11
step at a time inside the loop until i
2:15
reaches n.
2:17
So you will stop when i becomes greater
2:21
than n. So finally we printed the
2:25
factorial result. So what's happening
2:27
here? Here we are getting the input from
2:29
the user. Like for example number five
2:33
and here we created two variables. One
2:35
is used as a counter which is i and the
2:38
other is a factorial variable called
2:39
fact and we are starting both of them
2:42
with one.
2:44
And here we are checking whether i is
2:46
less than or equal to n so that we can
2:49
loop it until i reaches the number five.
2:54
What you're doing inside the loop? We
2:56
are multiplying the number fact with i.
3:00
Number I. So I equal to i + 1. Don't
3:03
worry about the logic. We will write it.
3:05
We will trace it. So this is the actual
3:07
logic.
3:10
And then once i reaches n once i is
3:14
greater than n, it will be so this
3:18
condition is false. So then it comes out
3:20
of the loop and then it will print the
3:22
factorial of the number and then we will
3:25
end the program.
3:27
So it's like telling the Python multiply
3:30
numbers from 1 to n step by step.
3:34
Let's write our Python program together.
3:38
So always get user input.
3:46
Let's just get int
3:50
enter a number
3:56
and as usual create two variables one is
3:58
fact and one is a counter
4:02
and while i
4:05
less than or equal to n.
4:11
So you created the fact right fact =
4:14
fact into I
4:21
and I =
4:24
I + 1.
4:35
You can also do the same thing with fact
4:38
into equal to I
4:46
same as
4:54
okay.
4:56
So you will come out of the loop and
4:58
then you will print
5:03
the factorial
5:14
of
5:16
n.
5:25
Let's break this down. So here we are
5:27
getting the user input and you have to
5:30
give input here. My bad.
5:34
int of input of number. Enter a number
5:38
and then you gave two variables. This is
5:41
the starting point because factorial
5:43
usually starts with one because if you
5:46
multiply you'll be thinking like why not
5:48
zero? Why just one? Because if you
5:50
multiply any number with zero obviously
5:52
the outcome will be zero. So in order to
5:55
initiate this multiplication
6:01
so we need to start the number with one.
6:04
So that is what we are doing
6:06
and we are using i = 1 which is our
6:09
initial counter and while i less than or
6:13
equal to n. The loop runs until I
6:16
becomes bigger than N. And inside the
6:19
loop, the fact equal to fact into I
6:22
keeps multiplying and it will increase
6:26
the value of I for every loop and
6:28
finally it will come out of the loop
6:30
once I greater than N and then we print
6:33
the result using an F string for a neat
6:36
formatting.
6:37
Okay, let's trace it. For example, user
6:41
gave
6:43
three number three. Initially the fact
6:46
value is one and I value is 1. Okay.
6:49
While
6:52
I what is I value 1
6:55
less than or equal to three? Is this
6:59
condition is true? True. So now what
7:03
happens with the fact?
7:06
So fact equal to fact into I right. So
7:09
initially the fact value is
7:13
1.
7:14
So 1 into what is the value of i?
7:21
What is the value of i? 1. So 1 into 1
7:24
is nothing but 1.
7:28
Okay. And now i value becomes 2.
7:34
I value becomes two. Let's keep these
7:38
things aside for a while so that you can
7:40
understand the logic.
7:44
So I value is 2 is 2 less than or equal
7:49
to 3. True. So now fact equal to fact
7:53
into I. So what is the value of I here?
7:58
2. So what is the value of 1 into 2? two
8:03
and now I value again increments by one.
8:06
So this will be three is three less than
8:09
or equal to what is the value of n three
8:12
less than or equal to three true. So now
8:15
fact equal to fact into i right. So fact
8:18
value becomes.
8:20
So this becomes 2 and what is the value
8:23
of i now three. So now the fact value
8:26
changes to six.
8:30
And now the i value becomes four. And is
8:34
4 less than or equal to 3? Listen to me.
8:37
Is four less than or equal to three? No.
8:41
So this comes out of the loop. And the
8:44
final factorial of n is what is the
8:46
final value is six. So that is what you
8:50
are going to see as output. Let's check.
9:01
Enter a number three. The factorial of
9:04
three is six. So this is what we got
9:07
when we calculated when we traced the
9:09
logic.
9:11
And when you give five, this will be
9:14
120.
9:19
That's really good job my friends. So
9:22
you just learned how to understand
9:24
factorials in math, how to use a while
9:27
loop in Python, and also how to perform
9:30
repeated multiplication step by step.
9:33
And here is a little challenge for you.
9:35
Try running this program with numbers
9:37
like seven or 10. And can you find 10
9:40
factorial?
9:42
It's a really big number, but Python
9:45
will handle it easily.
9:47
So just try, give it a try. And if you
9:50
enjoyed this tutorial, make sure to like
9:52
this video, subscribe to my channel, and
9:55
tap the notification bell so you never
9:57
miss the next Python exercise.
10:00
Remember, coding is like solving
10:02
puzzles. Every program you write makes
10:04
you sharper and more confident. Keep
10:07
practicing, keep experimenting, and soon
10:10
you'll be building amazing things.

