Prime no checker in python

Prime number program in python | python basic programs | learn python | Codigence
codigence- prime number checker in python

What is a prime number?


A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 4 is composite because it is a product in which both numbers are smaller than 4. Primes are central in number theory because of the fundamental theorem of arithmetic: every natural number greater than 1 is either a prime itself or can be factorized as a product of primes that is unique up to their order. The property of being prime is called primality. A simple but slow method of checking the primality of a given number n, called trial division, tests whether n is a multiple of any integer between 2 and √(n).
So lets see how to check prime numbers in python..

Code 1(Using For): 

  
  '''checking weather the no is prime or not'''
def isprime(no):
    #If the number is smaller than 2 than return false
    if(no < 2):
       return False
      
    prime = True
    #if the number is 2 then it will return true
    for i in range(2,int(no**0.5)+1,2):
        '''checking if it gives remainder '0' dividing by 
          numbers less than it'''
        if (no % i == 0):
            prime = False
        
        return prime

print(isprime(97))
print(isprime(1))
  
  

Output: 


  
True
False
  
  

Code 2(Using math function): 


  
import math
def isprime(num):
    i=2
    while i<=math.sqrt(num):
        if num%i<1: false="" i="i+1" num="" return="">1
print(isprime(0))
print(isprime(7))
  
  

Output: 


  
False
True