The Python Game Book

code games. learn Python.

User Tools

Site Tools


en:glossary:a:any

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:glossary:a:any [2020/05/13 10:28]
horst created
en:glossary:a:any [2020/05/13 10:36] (current)
horst
Line 4: Line 4:
  
 <blockquote>Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: <blockquote>Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:
- +<code python; gutter:false> 
-  def any(iterable): +def any(iterable): 
-      for element in iterable: +    for element in iterable: 
-          if element: +        if element: 
-              return True +            return True 
-      return False</blockquote>+    return False 
 +</code> 
 +</blockquote>
              
 ''any()'' is part of python's [[https://docs.python.org/3/library/functions.html|built-in functions]] ''any()'' is part of python's [[https://docs.python.org/3/library/functions.html|built-in functions]]
 +
 +Use any() instead of an expression with a lot of ''or'' operators:
 +
 +<code python;gutter:false>
 +# instead of:
 +coordinate_is_invalid = (x < 0) or (y < 0) or (x > 10) or (y >10)
 +# use any():
 +coordinate_is_invalid = any([x<0,y<0, x>10, y>10])
 +</code> 
 +</code>
 +
 +
en/glossary/a/any.1589358517.txt.gz · Last modified: 2020/05/13 10:28 by horst