Function to Determine Prime Status - Python Programming Exercise

In this exercise, you will develop a Python program with a function called "is_prime" that takes an integer as input and returns True if the number is prime, or False if it is not. This exercise is perfect for practicing function definition, conditional statements, and loops in Python. By implementing this function, you will gain hands-on experience in handling function definitions, conditional statements, and loops in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Determine Prime Status

 Objective

Develop a Python program with a function called "is_prime" that takes an integer as input and returns True if the number is prime, or False if it is not.

For example, if is_prime(127) returns True, then the number 127 is prime.

 Example Python Exercise

 Copy Python Code
# Define the function to check if a number is prime
def is_prime(number):
    # Check for numbers less than 2 (they are not prime)
    if number < 2:
        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:  # If a factor is found, number is not prime
            return False
    
    # If no factors are found, number is prime
    return True

# Main function to test the is_prime function
def main():
    # Test the function with a prime number
    print(is_prime(127))  # Expected output: True
    
    # Test the function with a non-prime number
    print(is_prime(100))  # Expected output: False

# Call the main function to execute the program
main()

 Output

True
False

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Main Function Parameters and Summation

    In this exercise, you will develop a Python program named "sum" that takes two integer numbers from the command line and prints their sum. This exercise is per...

  •  Function to Calculate Sum of Digits

    In this exercise, you will develop a Python program with a function named "sum_digits" that takes a number as input and returns the sum of its digits. This exercis...

  •  Function to Compute Factorial

    In this exercise, you will develop a Python program with a recursive function to compute the factorial of a given number. The factorial of a number is defined as: 𝑛...

  •  Main Function Parameters and Reversal

    In this exercise, you will develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. This exercise...

  •  Function to Obtain Integer Value

    In this exercise, you will develop a Python program with a function named "get_int" that displays the text received as a parameter, prompts the user for an integer, a...

  •  Function to Manage Task Database

    In this exercise, you will develop a Python program to enhance the "tasks database" by dividing it into multiple functions for better organization and readability. Th...