3
\$\begingroup\$

I'm trying to learn how to write code well in Python. I've been tinkering with a function that will return a list of all prime numbers between the two parameters. This is the best I could come up with considering my limited experience.

Do you have any ideas of how this code can be improved to become more Pythonic? (Which I think means more efficient?) If you do, would you be so kind as to not explain what you would do differently?

This would be such a help! Here's what I have so far:

def get_primes(start,end):

    return [num for num in range(start,end+1) if num>1 and all(num%i!=0 for i in range(2,num))]

get_primes(-41,41)
\$\endgroup\$
0
6
\$\begingroup\$

If you are already into programming/STEM, you will already know finding prime numbers is a tough problem. Hence, I will not comment on the efficiency of checking whether a number is primer by checking all possible divisors (which is a terrible idea if you are trying to actually find prime numbers, but it is good enough if you are simply trying to learn).

Now, regarding what pythonic means, it does not refer to efficiency at all. It is about readability and the way the language features are used (it is explained in a bit more depth in this answer).

Furthermore, if you are new to python, and want to learn about best practises, I would recommend you giving a read to PEP-8. It is a style guide for python code widely followed by the community.

Edit:

Still, as a comment on your code, I feel the logic to check whether a number is primer or not is complex enough to be encapsulated in a function:

from typing import Iterable

def is_prime(num: int) -> bool:
    return num>1 and all(num%i!=0 for i in range(2,num))

def get_primes(start: int, end:int) -> Iterable[int]:
    return [num for num in range(start,end+1) if is_prime(num)]

get_primes(-41,41)

If you are not yet familiar with it, the syntax used above is called type annotations. You can read more about it here.

\$\endgroup\$
0
\$\begingroup\$

I too am new to this forum as well as to python.

Here's my version of your solution, its a fully functional interface and not just a code snippet, that will be useful

Also I followed most PEP-8 conventions, do point out any deviations, I shall correct myself.

def get_primes(start,end):
  l = []
  for i in range(start,end + 1):
    flag = True
    for j in range(2,i):
      if i % j == 0:
        flag = False
    if flag == True:
      l.append(i)
  return l

#__main__
ans = "y"
while ans == "y":
  st = int(input("Enter starting number: "))
  nd = int(input("Enter ending number: "))
  print(f"The list containing all prime (inclusive) between {st} and {nd} is: \n",get_primes(st,nd))
  ans = input("Once again? (y / n): ")

That should work. Do tell if any additional issues.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The aim of this website is to review the existing code, not to provide an alternative solution. \$\endgroup\$ Jul 18 at 9:32
  • \$\begingroup\$ All right! Thanks for the clarification \$\endgroup\$ Aug 18 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.