create array with first ten primes in reverse in python

To create an array with first ten primes in reverse in Python, you can use the following code:

main.py
# function to check if a number is prime
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

# create an empty list to store the primes
primes = []

# loop through the numbers starting from 2 until we have 10 primes
n = 2
while len(primes) < 10:
    if is_prime(n):
        primes.append(n)
    n += 1

# reverse the list
primes.reverse()

# print the array
print(primes)
478 chars
25 lines

This code defines a function is_prime that checks if a number is prime or not. It then creates an empty list primes and loops through the numbers from 2 until we have found 10 primes. When a prime is found, it is added to the primes list. Finally, the list is reversed and printed.

The output of this code will be:

main.py
[29, 23, 19, 17, 13, 11, 7, 5, 3, 2]
37 chars
2 lines

related categories

gistlibby LogSnag