How to Create Check Prime Number in Python

In this tutorial, we will create a "How to Check Prime Number in Python". The purpose of this tutorial is to teach you how to determine whether a number is a prime number or not. We will try to understand the process of creating this program. I will provide a sample program to demonstrate the actual coding of this tutorial.

This tutorial is simple and easy to understand; just follow the instructions I provide, and you can do it without a problem. You can use this program for any system or application that requires you to find prime numbers. I will do my best to provide you with the most simplified method of creating this program, called "Prime Number Checker". So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display the Prime number Checker Program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. print("\n\n=========== Prime Number Checker ===========")
  2.  
  3.  
  4. while True:
  5. ret=False
  6. num = int(input("Enter a number: "))
  7.  
  8. if num == 1:
  9.  
  10. print(num, "is not a prime number")
  11. elif num > 1:
  12.  
  13. for i in range(2,num):
  14. if (num % i) == 0:
  15. print(num,"this is not a prime number")
  16. print(i,"times",num//i,"is",num)
  17. break
  18. else:
  19. print(num,"this is a prime number")
  20.  
  21.  
  22. else:
  23. print(num,"this is not a prime number")
  24.  
  25. user_input=input("\n\nTry another number? (Yes/No):")
  26. print("\n\n")
  27. if user_input.lower()=='no':
  28. print("Exit program.")
  29. break
  30.  
  31. elif user_input.lower()=='yes':
  32. ret=True
  33.  
  34. else:
  35. print("Wrong input.")
  36. ret=True
  37.  
  38.  
  39. if ret:
  40. continue

In the provided code, we first created the perfect title for the application and then proceeded to enclose the entire code with a while loop. Next, we set a variable that will take the input value of a number. Following that, we will create a code that checks if the number is a prime number. If it is not a prime number, a certain condition will activate and show you the process for it.

Output:

The How to Create Check Prime Number in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Check Prime Number in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment