def is_prime(n): if n == 2: return True for i in range(2, n): if n % i == 0: return False else: continue else: return True numbers = [2, 5, 34, 37, 8, 9] for n in numbers: if is_prime(n): print("The number ", n, " is a prime number") else: print("The number ", n, " is not a prime number")
Please note that the function is_prime() returns a boolean value. We are passing an integer as an argument to the function. If the passed number is a prime number, the function is_prime() returns True, and else it returns False.
โnumbersโ is a Python list that contains a list of positive integers. We are taking each number from the Python list and calling the is_prime() function with each number. If is_prime(n) returns True, the program prints the number n is a prime number and otherwise, it prints the number is not a prime number.
The program will print the following output:
The number 2 is a prime number The number 5 is a prime number The number 34 is not a prime number The number 37 is a prime number The number 8 is not a prime number The number 9 is not a prime number
Now, if we need to check the primality of another number from a different part of the code, we can do so easily using the is_prime() function.
Please note that we pass arguments to a function. And, in the function definition, the passed arguments are called …






0 Comments