Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Check Prime Number
Photo Credit to CodeToFun
π Introduction
In the landscape of programming, dealing with prime numbers is a fundamental and interesting task. Prime numbers, those divisible only by 1 and themselves, play a crucial role in various mathematical and computational applications.
In this tutorial, we will unravel a concise yet potent Python program to determine whether a given number is a prime number.
π Example
Let's immerse ourselves in the Python code that executes the prime number check.
# Function to check if a number is prime
def is_prime(number):
# 0 and 1 are not prime numbers
if number <= 1:
return False
# Check for factors from 2 to the square root of the number
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False # Found a factor, not a prime number
return True # No factors found, it's a prime number
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
test_number = 17
# Call the function to check if the number is prime
if is_prime(test_number):
print(f"{test_number} is a prime number.")
else:
print(f"{test_number} is not a prime number.")
π» Testing the Program
To test the program with a different number, replace the value of test_number in the if __name__ == "__main__": block.
17 is a prime number.
Run the script to see whether the number is a prime number.
π§ How the Program Works
- The program defines a function is_prime that takes an integer as input and returns a boolean indicating whether the number is prime.
- Inside the function, it checks if the number is less than or equal to 1; if so, it's not prime.
- It then iterates through potential factors from 2 to the square root of the number.
- If it finds any factor, the number is not prime; otherwise, it is prime.
- The main block tests the program with a specific number (in this case, 17).
π Between the Given Range
Let's dive into the python code that checks and lists prime numbers in the specified range.
# Function to check if a number is prime
def is_prime(num):
if num > 1:
# Check for factors
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
else:
return True
else:
return False
# Define the range
lower_limit = 1
upper_limit = 20
# Print prime numbers in the specified range
prime_numbers = [num for num in range(lower_limit, upper_limit + 1) if is_prime(num)]
print(f"Prime Numbers in the Range {lower_limit} to {upper_limit}:", end=" ")
print(*prime_numbers)
π» Testing the Program
The program is set to check and list prime numbers in the range from 1 to 20. No additional input is required.
Prime Numbers in the Range 1 to 20: 2 3 5 7 11 13 17 19
Run the program to see the prime numbers in the specified range.
π§ How the Program Works
- The program defines a function is_prime to check if a number is prime.
- Inside the function, it iterates through the range from 2 to the square root of the number and checks for factors.
- The main section defines the range (1 to 20) and prints the prime numbers in that range using list comprehension.
π§ Understanding the Concept of Prime Numbers
Before diving into the code, let's briefly understand prime numbers.
A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
π’ Optimizing the Program
While the provided program is effective, you can explore optimizations such as using the square root of the number as the loop limit for improved efficiency.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Prime Number), please comment here. I will help you immediately.