0
0
DSA Pythonprogramming~15 mins

Check if Number is Prime in DSA Python - Build from Scratch

Choose your learning style9 modes available
Check if Number is Prime
📖 Scenario: You are helping a friend who wants to check if a number is prime. A prime number is a number greater than 1 that has no divisors other than 1 and itself.Imagine you have a list of numbers and want to find out which ones are prime.
🎯 Goal: Build a simple program that checks if a given number is prime by testing divisibility from 2 up to the number minus one.
📋 What You'll Learn
Create a variable called number with the exact value 29.
Create a variable called is_prime and set it to True.
Use a for loop with variable i to check divisors from 2 to number - 1.
Inside the loop, if number is divisible by i, set is_prime to False and break the loop.
Print "Prime" if is_prime is True, otherwise print "Not Prime".
💡 Why This Matters
🌍 Real World
Prime numbers are important in cryptography, computer security, and coding theory.
💼 Career
Understanding prime checking helps in algorithm design and problem solving in software development.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to the exact value 29.
DSA Python
Hint

Use number = 29 to create the variable.

2
Set up the prime flag
Create a variable called is_prime and set it to True.
DSA Python
Hint

Use is_prime = True to start assuming the number is prime.

3
Check divisors with a loop
Use a for loop with variable i to check divisors from 2 to number - 1. Inside the loop, if number % i == 0, set is_prime to False and break the loop.
DSA Python
Hint

Use for i in range(2, number): and check divisibility with number % i == 0.

4
Print if the number is prime
Print "Prime" if is_prime is True, otherwise print "Not Prime".
DSA Python
Hint

Use an if statement to print "Prime" or "Not Prime" based on is_prime.