Prime numbers with Python

here we are going to learn how to find if a number is prime or not using Python code and also how to print only prime numbers between 1 to 1000 or more. it's going to be really fun to learn python programming language with questions and trying to find answers.







 import pandas as pd  
 def prime(x):  
   if x<=1:  
     return ('Not')  
   if x==2:  
     return ('Prime')  
   for i in range(2,x//2+1):  
     if x%i==0:  
       return('Not')  
   return('Prime') 
   
   
 prime(10993)  
 
 
 y=range(1,1001)  
 for i in y:  
   print(i,prime(i)) 
   
   
 pri=[]  
 y=range(1,1001)  
 for i in y:  
   pri.append(prime(i))  
   
   
 str(pri.count('Prime'))+' prime',str(pri.count('Not'))+' Not prime'  
 
 
 dt=[range(1,1001),pri]  
 
 
 pn = {'num': range(1,1001),  
     'prime': pri  
     }  
     
     
 primedf = pd.DataFrame(pn, columns = ['num', 'prime'])  
 
 

 for i,j in zip(primedf['prime'],primedf['num']):  
   if i =='Prime':  
     print(j)  

Comments

Popular posts from this blog

Python Data Types Boolean

Python Installation