Python – Check if a number is prime

In this post, we will see how we can check if a number is prime or not


A number is prime, if it has NO divisors other than 1 or itself [in other words, it should not be divisible by any number in between 1 and itself]


5 => is a prime number [5 is not divisible by any number in between 1 and 5, i.e not divisible by 2/3/4]

9 => is NOT a prime number [9 is divisible by 3]


num // 2 => divide with 2 (discard remainder)

Used num // 2 => we are checking divisibility half way of the number [no need to check full range]

If interested the implementation in java, refer here

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *