Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:36:13+00:00 2026-06-05T11:36:13+00:00

I’ve recently made the following example for Pythons for … else: def isPrime(element): just

  • 0

I’ve recently made the following example for Pythons for … else:

def isPrime(element):
    """ just a helper function! don't get religious about it! """
    if element == 2:
        return True
    elif element <= 1 or element % 2 == 0:
        return False
    else:
        for i in xrange(3, element, 2):
            print i
            if element % i == 0:
                return False
    return True


myList = [4, 4, 9, 12]

for element in myList:
    if isPrime(element):
        break
else:
    print("The list did not contain a prime.")

A fellow student told me, that this task can be done with Scala like this:

List(4, 4, 9, 12) exists isPrime

Which gets lazy evaluated.

Does something similar like the exists-keyword exist in Python? Or is there a PEP for that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:36:15+00:00Added an answer on June 5, 2026 at 11:36 am
    myList = [4, 4, 9, 12]
    
    if not any(isPrime(x) for x in myList):
        print("The list did not contain a prime")
    

    Python also has all() which cranks through any sequence and returns True if all elements evaluate true.

    any() and all() both have short-circuit evaluation: if any() finds any element that evaluates true, it stops and returns True; and if all() finds any element that evaluates false, it stops and returns False.

    Both are “lazy” in that they use Python iteration to pull values one at a time. For example:

    import random
    def rand_sequence(n_max):
        while True:
            next_random = random.randint(0, n_max)
            print(next_random)
            yield next_random
    
    all(isPrime(x) for x in rand_sequence(20))
    

    This will iterate until a non-prime number is found, then return False. It prints the numbers as a side-effect so you can watch it work. I just tried this and got:

    17
    3
    0
    

    P.S. I went to a talk at a Python conference, and the speaker mentioned that he commonly uses any() as a very efficient way to do a loop. A for loop re-binds the loop variable for each loop, but any() doesn’t do that; it just keeps checking values. So if you use any() with a function that always returns None or a false value, it will iterate its way all to the end of the sequence, and according to that guy, it’s the fastest way in Python to do it. (And if your function returns a value that is not None and isn’t false, you can use all() for the same trick. The only time it doesn’t work is if sometimes the function returns a true value and sometimes it returns a false value. But you can force it to always work:

    any(my_function(x) and False for x in sequence)
    

    P.P.S. Let’s use all() to rewrite isPrime()! I’ll change the name to is_prime() to conform to PEP 8. http://www.python.org/dev/peps/pep-0008/

    def is_prime(element):
        """ just a helper function! don't get religious about it! """
        if element == 2:
            return True
        elif element <= 1 or element % 2 == 0:
            return False
        else:
            return all(element % i for i in xrange(3, element, 2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.