1 Video View·Apr 21, 2025
In this video I show you how to check if a specific number is a prime number, meaning it is only divisible by itself and one. This becomes tedious to check for at higher values and programming code for checking automatically becomes very useful! We do this function using a for loop and the built in python modulo operator for checking remainders in division. This short easy to follow tutorial is great for beginners looking to expand their knowledge.
Be sure to check out the channel for over 100 videos of code tutorials across multiple platforms and different software! New content comes out every week so make sure to subscribe to stay up to date, and if you found the video useful please consider leaving a like or let me know what could be done better in the comments!
Thanks for watching and good luck with your code!
what's going on you guys welcome back to
the channel and today's video is how to
check if a specific number is a prime
value inside of python
[Music]
okay so a prime number specifically is
any number that is only divisible by
itself and one no other values and so
we're going to create a program to
actually check for it so let's start by
getting the number that we want to check
and let's get it as an input from the
user so that makes this function a
little bit that this program a little
bit quicker to use
enter the number you want to check now
we don't have to hard code the value
that we're checking against we can just
use that every time so what we're going
to do what we need to do to make sure
something is prime is check and see if
any of the numbers below that value are
able to uh be divisible by it so let's
say for i in range
and we're gonna use a for loop because
what this is going to do is it's going
to start at two basically because every
number is divisible by one but we're
gonna start at two
and we're just going to go forward and
say if if you're divisible by 2 all the
way up to
this number minus 1 then you're not
prime but if you get through that whole
list and we haven't finished yet then it
it could be a prime value so what we'll
do is we'll say for i in range and we'll
start with two because we don't wanna
start with one everything is divisible
by one
so um we'll do two and then we'll say
all the way up to my number and the
reason this isn't my number minus one
is the way python for loops work is they
do include the start value of your range
but they don't include the end value so
actually this is going to check if our
number was 10 it's gonna check if you're
divisible by two three four five six
seven eight and nine but not ten that's
just how for loops in python work
and what do we actually want check we
wanna check if my number
divided by that value basically has no
remainder right like it's it's hold the
vision so there's no remainder and
python has this really useful built-in
module
operator called the modulo operator
which is uh basically just saying the
remainder after division so if we're
going to divide by i
as we go through this for loop i is the
variable that keeps updating so it's two
it's three it's four it's five
and if we divide
my number by i
and there's
no
remainder so that's that's what this
modulo is getting is the remainder that
means the remainder is equal to zero
then that means our number is not prime
this
number
is not
prime and so we'll print that out on the
screen so they know and then we'll also
just exit so that's how you get it to
leave the for loop right
if you're in the for loop
and you get certain conditions that you
want to end the check because the thing
is if you're checking 400 you don't need
to get all the way up to 40 to know that
it's not prime because as soon as you're
at 2 you know it's not prime so then you
get four and it's not prime 10 it's not
prime so just exit the for loop as soon
as something's divisible by it
um and then what we'll print onto the
screen
is
if so now we're thinking
if you get all the way through the for
loop and we haven't exited the code yet
that means that the for loop finished
and we can say this number is prime so
this right here is all you need to do to
check if it's prime so go ahead and run
that and let's check one that we know is
prime like uh 13.
this number is prime sweet now let's
check like i was saying let's go ahead
and check 400
and this number is not prime okay and
you can check that with as many as you
want um i don't know all the prime
numbers i guess 177s not one
but try some high numbers look them up
online and and see what primes there are
out there and just make sure it's
working but this is all you need so
super quick super easy python concept
super useful to know i hope you found
that useful if you did please consider
liking the video subscribe to the
channel it helps me out a ton
be sure to check out all the other great
content over there let me know if you
have any questions or want to see
something in a future video in the
comments below and as always good luck
with your code and thanks for watching
thanks bye