As with any OOP language, you can use Python to conduct calculations and gather information about numbers. One cool thing you can do with Python is test if a number is prime or not. A prime number, as you may remember from math class way back when, is any whole number (it must be greater than 1), whose only factors are 1 and itself, meaning it can't evenly be divided by any number (apart from 1 and itself, of course). Prime numbers include 2, 3, 5, 7, 11, 13, and so on until infinity.

In Python, we can test for prime numbers pretty easily using the snippet below.

if num > 1:

for i in range(2,num):
 if (num % i) == 0:
 print(num,"is not a prime number")
 print(i,"times",num//i,"is",num)
 break
 else:
 print(num,"is a prime number")
 
else:
 print(num,"is not a prime number")

First, the code checks to make sure that the number is greater than 1 (anything less than one can't be a prime number because it isn't whole). Then it checks to see if the number is divisible by any number between 2 and the number you're checking. If it is divisible, then you'll see from the output that the number is not prime. If it isn't divisible by any of those numbers, then the message on the output will read "[num] is not a prime number."

About The Author