1

I tried writing a function that returns 'True' if the given parameter is a prime number and 'False' otherwise. In a sense, I achieved my goal but I just wanted to know of a better, more pythonic way to do it, here's what I came up with:

def prime_one_liner(n):
    return True if len([i for i in range(2,n) if n % i == 0]) == 0 else False
5
  • 1
    return len(...) == 0. This already returns a boolean…
    – deceze
    Commented Feb 18, 2022 at 14:03
  • 4
    It's generally not Pythonic to write one-liners for one-liners' sake.
    – AKX
    Commented Feb 18, 2022 at 14:03
  • 2
    Some notes: (1) You should early-exit when you've found any i for which n % i == 0. (See: any and a generator expression. (2) i should range up to n // 2 (or sqrt(n), I forget which) (3) The whole True if ... else False is unnecessary with both your current approach and any().
    – AKX
    Commented Feb 18, 2022 at 14:05
  • append docs, if you want 'more pythonic way' Commented Feb 18, 2022 at 14:05
  • This will return 'True' if n=1, which is not a prime number. Commented Feb 18, 2022 at 14:14

3 Answers 3

5

"Achieving Your 'Goal'"

You cold use any to make it shorter if that is your goal:

def prime(n): return not any(n % i == 0 for i in range(2,n))

Although, "pythonic" is not how I would describe this. It is more like "hard to read." Yet, it may be more pythonic than your version.

Better way:

A more readable version would be:

def prime(n): 
    for i in range(2, n): #for every value between 1 and n
        if n%i == 0: #check if i divides n
            return False #if this is true, n is not prime
    return True

Proper Function:

However you need to account for values less than 2 so you would also need to edit your code like so:

def prime(n): return not any(n % i == 0 for i in range(2,n)) if n > 1 else False

And/or:

def prime(n): 
    for i in range(2, n): #for every value between 1 and n
        if n%i == 0: #check if i divides n
            return False #if this is true, n is not prime
    return True if n > 1 else False #values less than 2 are not prime.

General Info:

Your code will not work for all integer values of n, consider, n=1 (see the amended examples above that fix this issue), but regardless, it is possible to make it shorter if that is your goal (as shown above), but just because it is short does not mean it is "pythonic." Currently your code is quite hard to read. So maybe expanding the code would be better (similar to the code expansion above).

2
  • Just for some context, this goal was part of an exercise in a python course I'm taking since the concept of one-liners isn't "clicking" for me. I appreciate the feedback though, thanks. Commented Feb 18, 2022 at 14:41
  • @yonatangoldin sure thing, hope it helped.
    – Eli Harold
    Commented Feb 18, 2022 at 14:44
0

I think this is the most "pythonic" way to do it:

def prime_one_liner(n): return all((n%i)>0 for i in range(2, int(n**.5)+1)) if n>1 else False
8
  • 2
    Remove the [] to make it an efficient generator, avoiding the need to build the entire list first.
    – deceze
    Commented Feb 18, 2022 at 14:15
  • 1
    @deceze done. you are right. Commented Feb 18, 2022 at 14:17
  • @SalvatoreDanieleBianco this actually works only for n>1 not n>0, but you can just append if n > 1 else False to the last line to make it work for all integers.
    – Eli Harold
    Commented Feb 18, 2022 at 14:34
  • 1
    @EliHarold sorry I though that 1 was prime. I will correct my answer. Thanks for your suggestion. Commented Feb 18, 2022 at 15:22
  • 1
    No problem, 1 seems prime so I get the confusion.
    – Eli Harold
    Commented Feb 18, 2022 at 15:22
-2

My version of your idea would look something like this:

x = lambda n: False if False in map(lambda i: False if n % i == 0 else True, range(2, n)) else True

as this is a oneliner consisting of one line not two

but there are probably a lot of good/better ways of doing that but it is pythonic in some way

3
  • This is an incredibly bad coding habit. Don't name lambdas. Also, the approach is very unreadable.
    – Eli Harold
    Commented Feb 18, 2022 at 15:24
  • i mean the question was writing an oneliner what do you expect? A oneliner is nothing for prod of course so there is no BaD hAbIt. Also oneliners aren't designed with readability in mind obviously -_-. In terms of naming lambdas, it's not the best but i haven't seen an other way, could you show me one?
    – Geibelt
    Commented Feb 18, 2022 at 17:58
  • Consider: def prime(n): return not any(n % i == 0 for i in range(2,n)) if n > 1 else False that is one line, working, and more readable all without assigning lambdas a name. It's also shorter. And yes, naming lambdas and producing difficult to read code is a bad habit.
    – Eli Harold
    Commented Feb 18, 2022 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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