In this python tutorial, you will learn about the **python program to print prime numbers **also we will check**:**

- Python program to print prime numbers
- Python program to print prime or not
- Simple prime number program in python
- Python program to print prime numbers upto n
- Python program to print prime numbers from 1 to 100
- Python find prime numbers in a range
- Python program to print prime numbers using while loop
- Python program to print prime numbers using for loop
- Python program to print prime numbers from 1 to 100 using while loop
- Python program to print prime numbers from 1 to 20
- Python program for prime number using if-else
- Python program for prime number using list comprehension
- Python program for prime number in interval
- Python find the sum of prime numbers in a range
- First n prime numbers Python

**Table of Contents**show

## Python program to print prime numbers

Let see **python program to print prime numbers**.

- Firstly, we will take
**two inputs**from the user. **for loop**is used to iterate from**lower**to**upper**values- Another
**for loop**is used, we are dividing the input number by all the numbers in the range of 2 to number. It checks whether there are any positive divisors other than 1 and the number itself. - The break statement is used to come out of the loop as soon we get any positive divisor then no further check is required.
- At last, it
**prints**the number which is prime.

You may also like Check if a number is a prime Python and How to print factorial of a number in Python.

**Example:**

```
lower = int(input("Enter the lower value:"))
upper = int(input("Enter the upper value:"))
for number in range(lower,upper+1):
if number>1:
for i in range(2,number):
if (number%i)==0:
break
else:
print(number)
```

To get the output, I have used** print(number)**. You can refer to the below screenshot for the output.

The above code, we can use to **print prime numbers in Python**.

Read, Python program to print pattern and How to calculate simple interest in Python?

## Python program to print prime or not

Now, we will see **python program to print prime or not**.

- Firstly, we will take
**one input**from the user. - A
**prime number**is always**positive**so, we will check at the beginning of the program - Another
**for loop**is used, we are dividing the input number by all the numbers in the range of 2 to number. It checks whether there are any positive divisors other than 1 and the number itself. - If any divisor is found then we display that the
**“number is not prime number”**else we display that the**“number is a prime number”**. - The break statement is used to come out of the loop as soon we get any positive divisor then no further check is required.
- At last, it
**prints**the number which is prime.

**Example:**

```
number = int(input("Enter any number:"))
if number>1:
for i in range(2,number):
if (number%i)==0:
print(number, "is not prime number")
break
else:
print(number, "is prime number")
```

To get the output, I have used** print(number, “is prime number”).** You can refer to the below screenshot for the output.

The above code we can use to **print prime or not in Python**.

## Simple prime number program in python

Here, we will see **simple prime number program in python**.

- Firstly, we will take two inputs
**(low and high)**from the user - Here, we will use
**for loop**to iterate through the given range - Now, we will check whether the values within the given range are divisible by 1 and itself.
- The
**break statement**is used to come out of the loop as soon we get any positive divisor then no further check is required. **else**, print the number

**Example:**

```
low = int(input("Enter the lower value:"))
high = int(input("Enter the higher value:"))
for num in range(low, high+1):
if(num>1):
for i in range(2,num):
if(num%i)==0:
break
else:
print(num)
```

To get the output, I have used** print(num).** You can refer to the below screenshot for the output.

Hope, you liked the **simple prime number program in Python**.

## Python program to print prime numbers upto n

Let’s see **python program to print prime numbers upto n**

- Firstly, we will set the initial value with 1
- Now, we will take input from the user which is stored in variable n.
**for loop**is used for dividing the input number by all the numbers in the range of 2 to number. It checks whether there are any positive divisors other than 1 and the number itself.- If any divisor is found then we display that the
**“number is not prime number”**else we display that the**“number is a prime number”**. - The break statement is used to come out of the loop as soon we get any positive divisor then no further check is required.
- At last, it
**prints**the number which is prime

**Example:**

```
start_val = 1
n = int(input("Enter the n number:"))
for num in range(start_val, n+1):
if(num>1):
for i in range(2,num):
if(num%i)==0:
break
else:
print(num)
```

To get the output, I have used** print(num).** You can refer to the below screenshot for the output.

This Python code, we can use to **print prime numbers upto n in Python**.

## Python program to print prime numbers from 1 to 100

Now, we will see a **python program to print prime numbers from 1 to 100**.

- I have used for loop to iterate a loop from 1 to 100.
- Another
**for loop**is used to check whether the number is divisible or not. - At last, it
**prints**the number in the given range.

**Example:**

```
for num in range(1, 101):
if(num>1):
for i in range(2,num):
if(num%i)==0:
break
else:
print(num, end = ' ')
```

To get the output, I have used** print(num, end = ‘ ‘).** You can refer to the below screenshot for the output.

This Python code, we can use to **print prime numbers from 1 to 100 in Python**.

## Python find prime numbers in a range

Now we will see **python find prime numbers in a range**.

- I have stored the value in the
**lower**and**upper**, and we will find prime numbers in that range. **for loop**is used to iterate from**lower**to**upper**values- Another
**for loop**is used, we are dividing the input number by all the numbers in the range of 2 to number. It checks whether there are any positive divisors other than 1 and the number itself. - At last, it
**prints**the number in the given range

**Example:**

```
lower = 100
upper = 200
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num, end = " ")
```

To get the output, I have used** print(num, end = ” “).** You can refer to the below screenshot for the output.

This code we can use to **find prime numbers in a range in Python**.

## Python program to print prime numbers using while loop

Let’s see **python program to print prime numbers using while loop**.

- Firstly, we will initialize
**num**as**1** - Here, we will use a
**while loop**to calculate the prime number **i = 2**is used for checking the factor of the number- We are dividing the number by all the numbers using
**f(num % i == 0)**. - At last
**print(” %d” %num, end = ‘ ‘)**is used for printing the prime numbers.

**Example:**

```
num = 1
while(num <= 30):
count = 0
i = 2
while(i <= num//2):
if(num % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and num!= 1):
print(" %d" %num, end = ' ')
num = num + 1
```

To get the output, I have used** print(” %d” %num, end = ‘ ‘).** You can refer to the below screenshot for the output.

This is how to **print prime numbers using while loop in Python**.

## Python program to print prime numbers using for loop

Now, we will see **python program to print prime numbers using for loop**.

- Firstly, I have taken two variable and the value is initialized
- I have used for loop to iterate in the given range
- Another
**for loop**is used to check whether the number is divisible or not. - At last, it
**prints**the prime number

**Example:**

```
l = 20
h = 50
for num in range(l, h+1):
if(num>1):
for i in range(2,num):
if(num%i)==0:
break
else:
print(num, end = ' ')
```

To get the output, I have used** print(num, end = ” “).** You can refer to the below screenshot for the output.

This is how to **print prime numbers using for loop in Python**.

## Python program to print prime numbers from 1 to 100 using while loop

Now, we will see **python program to print prime numbers from 1 to 100 using while loop**

- Firstly, we will initialize
**num**as**1** - Here, we will use a
**while loop**to calculate the prime number**from 1 to 100** **i = 2**is used for checking the factor of the number- We are dividing the number by all the numbers using
**f(num % i == 0)**. - At last
**print(” %d” %num, end = ‘ ‘)**is used for printing the prime numbers.

**Example:**

```
num = 1
while(num <= 100):
count = 0
i = 2
while(i <= num//2):
if(num % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and num!= 1):
print(" %d" %num, end = ' ')
num = num + 1
```

To get the output, I have used** print(” %d” %num, end = ‘ ‘).** You can refer to the below screenshot for the output.

This code, we can use to **print prime numbers from 1 to 100 using while loop in Python**.

## Python program to print prime numbers from 1 to 20

Here, we will see** python program to print prime numbers from 1 to 20**.

- Firstly, I have taken two variables
**start**and**end**with the value initialized. **for loop**is used to iterate the value from start to end- Another
**for loop**is used to check whether the number is divisible or not. - At last, it
**prints**the prime number from 1 to 20

**Example:**

```
start = 1
end = 20
for num in range(start, end+1):
if num>1:
for j in range(2,num):
if(num % j==0):
break
else:
print(num, end=" ")
```

To get the output, I have used** print(num, end = ” “).** You can refer to the below screenshot for the output.

This Python code, we can use to **print prime numbers from 1 to 20 in Python**.

## Python program for prime number using if-else

Let see **python program for prime number using if-else**

- Firstly, I have initialized the two variable
- I have used for loop to iterate in the given range
- Another
**for loop**is used to check whether the number is divisible or not. **if condition**checks whether the number is equal to zero or not**else**print the prime number

**Example:**

```
S = 50
E = 80
for num in range(S, E+1):
if num>1:
for i in range(2,num):
if(num % i==0):
break
else:
print(num, end=" ")
```

** print(num, end = ” “).** You can refer to the below screenshot for the output.

This is how check a prime number in if-else in Python.

## Python program for prime number using list comprehension

Let see **python program for prime number using list comprehension**.

In this example, the **list comprehension** is used to find the prime numbers in the range 2 to n+1 where **n=50** is initialized at the starting.

**Example:**

```
n = 50
prime = [i for i in range(2, n + 1) if all(i%j != 0 for j in range(2, int(i ** 0.5) + 1))]
print(prime)
```

To get the output, I have used** print(prime).** You can refer to the below screenshot for the output.

## Python program for prime number in interval

Now, we will see** python program for prime number in interval**

- I have stored the value in the
**start**and**end**, and we will find prime numbers in that range. **for loop**is used to iterate from**start**to**end**values- Another
**for loop**is used, we are dividing the input number by all the numbers in the range of**2 to number**. It checks whether there are any positive divisors other than 1 and the number itself. - At last, it
**prints**the number in the given range

**Example:**

```
start = 500
end = 600
print("Prime numbers in interval", start, "and", end, "are:")
for num in range(start, end + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num, end=" ")
```

** print(num, end = ” “).** You can refer to the below screenshot for the output.

This is how to get **prime number in interval in Python**.

## Python find sum of prime numbers in a range

Lets see **how to find the sum of prime numbers in a range**.

- Firstly, we will take
**one input**from the user. - Then we will define a function and the loop is used to iterate from the given range.
- Another
**for loop**is used, we are dividing the input number by all the numbers in the range of 2 to maxint. It checks whether there are any positive divisors other than 1 and the number itself. - The
**break**statement is used to come out of the loop as soon we get any positive divisor then no further check is required. - The generator function
**yield a**is used in the loop for iterable to call the sum. - At last, the
**print**will return the sum of prime numbers in the range.

**Example:**

```
num = int(input("Enter a number: "))
def sum_range(num):
for a in range(2, num + 1):
maxint = int(a **.5) + 1
for i in range(2, maxint):
if a % i == 0:
break
else:
yield a
print(sum(sum_range(num)))
```

To get the output, I have used** print(sum(sum_range(num))).** You can refer to the below screenshot for the output.

This is how to find **sum of prime numbers in a range in Python**.

## First n prime numbers Python

Here, we will see** first n prime numbers Python**.

- Firstly, we will take
**one input**from the user. **for n in range(2,num)**is used to iterate in the given range.- Another
**for loop**is used, we are dividing the input number by all the numbers in the range of 2 to n. It checks whether there are any positive divisors other than 1 and the number itself. - At last, it
**prints**the number which is prime.

**Example:**

```
num=int(input("Enter range:"))
print("Prime numbers:", end=' ')
for n in range(2,num):
for i in range(2,n):
if(n%i==0):
break
else:
print(n,end=' ')
```

To get the output, I have used** print(n, end = ‘ ‘).** You can refer to the below screenshot for the output.

This is how to find **first n prime numbers in Python**.

You may like the following Python tutorials:

- Python format number with commas
- Python generate random number and string
- Python square a number
- Python program to print element in an array
- Python string formatting with examples

In this Python tutorial, we have learned about the ** Python program to print prime numbers**. Also, we covered these below topics:

- Python program to print prime numbers
- Python program to print prime or not
- Simple prime number program in python
- Python program to print prime numbers upto n
- Python program to print prime numbers from 1 to 100
- Python find prime numbers in a range
- Python program to print prime numbers using while loop
- Python program to print prime numbers using for loop
- Python program to print prime numbers from 1 to 100 using while loop
- Python program to print prime numbers from 1 to 20
- Python program for prime number using if-else
- Python program for prime number using list comprehension
- Python program for prime number in interval
- Python find the sum of prime numbers in a range
- First n prime numbers Python

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.