Learn Python With Dhawal -11 Chapter - Program to Find A Number is Prime or Not


This time I wanted to share the fun and joy of using python. I had mentioned earlier that python is an advanced language and it has many libraries and modules to do things which we used code for a few decades back. Using basic math library functions isn't as much fun as what I am going to share with you today.

Python has a vast amount of libraries that you can import and make your work extremely easy as long as you understand how they work and what are the things that go behind the scenes when you use a particular method.

Here I have used a different library to show how much of your work can become easy when you start working with different modules and get familiar with them. Python is mostly used for Machine Learning as well as Data Analysis. So you can find a ton of things that you can do with this fun language as long as you know how to play with it.

Here I have shown a way to code a program that will help you find out if a particular number is prime or not. I have used very basic methodology to find it out and then I have shown another way to do the same time. I want you to observe the lines of code and how it works.

Input :

  1. def checkPrime(n):
  2.     #to check if the number is prime or not we will use the basic test of divisibility from mathematics.
  3.     if(n==2 or n==3 or n== 5 or n==7 ):
  4.         return True
  5.     elif(n%2==0):
  6.         return False
  7.     elif(n%3==0):
  8.         return False
  9.     elif(n%5==0):
  10.         return False
  11.     elif(n%7==0):
  12.         return False
  13.     else:
  14.         return True

  16. n=int(input("Enter Any Integer from 2-100\n"))

  17. result=checkPrime(n)

  18. if(result==True):
  19.     print(n," is a prime number.")
  20. else:
  21.     print(n," is not a prime number.")
  1. Enter Any Integer from 2-100
  2. 45
  3. 45  is not a prime number.
Input :

  1. import sympy

  2. n=int(input("Enter Any Integer from 2-100 to check if the number is prime or not.\n"))

  3. print (sympy.isprime(n))
  1. Enter Any Integer from 2-100 to check if the number is prime or not.
  2. 45
  3. False

  1.  In the first program, I have created a little function that I call upon to check if a particular number is prime or not.
  2. In that I have made a loop to first pick out the basic prime numbers which we all know, following which I have made a little else-if ladder to find out if the number passes the test of divisibilities or not.
  3. If the number is not divisible by any of those then it is a prime number else it isn't a prime number.
  4. I have used the restriction for the number 1 as number one is a topic of debate when it comes to say it is prime or not. Some argue that it is a prime number while some say it is not, and it is something new. I forgot the term that was used for it and can't find in articles but if I do, I will surely update it here.
  5. Secondly, I have used only positive integers here for the sake of simplicity of the programming. You can include negative numbers as well as floating point numbers.
  6. If you can see, I haven't added any protection against invalid inputs here as I wanted to keep the code simple as possible. Since I wanted to show the difference in number of lines between using a library function by importing it and coding it manually.
  7. In the second code, I have imported a library and used it. Just like your math library which you can use to easily perform addition and other such basic math functions. Here this library holds a lot of special functions related to prime numbers. You can search on the web for other functions that this library has or use the help command to find out.
  8. This code is very simple hence I expect you to understand it, even though if you find anything difficult, please comment down below and I'd be happy to clear the doubts for you.

I hope you enjoyed as much as I did writing this chapter. Here you learnt something more about loops, variables as well as functions in a gist and how to go about using them and build your own logic for various things.



This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.


Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.


Post a Comment

Do comment and share your thoughts.