---
title: "How do you check if a given number is prime in Python?"  
description: "How do you check if a given number is prime in Python?"  
author: "Utpal Vishwas"  
published: 2023-06-26  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/158862/how-do-you-check-if-a-given-number-is-prime-in-python  
category: "python"  
tags: ["python", "programs"]  
reading_time: 2 minutes  

---

# How do you check if a given number is prime in Python?

How do you [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a given number is prime in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Aryan Kumar

```plaintext
def is_prime(number):
  if number < 2:
    return False

  for i in range(2, int(number ** 0.5) + 1):
    if number % i == 0:
      return False

  return True

if __name__ == "__main__":
  number = 11
  print(is_prime(number))
```

This code works by first checking if the number is less than 2. If it is, the function returns False, because no number less than 2 is prime. Otherwise, the function iterates through all the numbers from 2 to the square root of the number, and checks if any of them are factors of the number. If any of them are factors, the function returns False. Otherwise, the function returns True.

Here is an explanation of the code:

- The `is_prime()` function takes a number as its input.
- The `if number < 2:` statement checks if the number is less than 2. If it is, the function returns False.
- The `for i in range(2, int(number ** 0.5) + 1):` statement iterates through all the numbers from 2 to the square root of the number.
- The `if number % i == 0:` statement checks if any of the numbers in the range are factors of the number. If any of them are factors, the function returns False.
- The `return True` statement returns True if the number is not divisible by any of the numbers in the range.


---

Original Source: https://www.mindstick.com/forum/158862/how-do-you-check-if-a-given-number-is-prime-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
