Complete the code to check if a number is prime by testing divisibility starting from 2.
def is_prime(num): if num <= 1: return False for i in range(2, [1]): if num % i == 0: return False return True
The loop must check divisors from 2 up to num (exclusive) to find any divisor. Using num ensures the loop checks up to num-1.
Complete the code to optimize prime check by testing divisors only up to the square root of the number.
def is_prime(num): if num <= 1: return False for i in range(2, [1]): if num % i == 0: return False return True
Checking divisors only up to the square root of num is enough to determine primality efficiently.
Fix the error in the code to correctly check if a number is prime by returning False immediately when a divisor is found.
def is_prime(num): if num <= 1: return False for i in range(2, int(num ** 0.5) + 1): if num [1] i == 0: return False return True
The modulo operator % checks if num is divisible by i with no remainder, which means i is a divisor.
Fill both blanks to create a dictionary comprehension that maps numbers to True if prime, False otherwise, for numbers 2 to 10.
primes = {num: is_prime(num) for num in [1] if num [2] 1}We want numbers from 2 to 10, so range(2, 11) is correct. The condition num > 1 filters numbers greater than 1.
Fill all three blanks to create a list comprehension that lists all prime numbers between 10 and 20.
prime_numbers = [[1] for n in [2] if is_prime([3])]
The list comprehension iterates over numbers 10 to 20 inclusive using range(10, 21), checks if each number n is prime, and collects n if true.