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

Both sides previous revision Previous revision
Next revision
Previous revision
en:glossary:a:any [2020/05/13 10:31]
horst
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+    return False 
 +</code>
 </blockquote> </blockquote>
              
Line 15: Line 16:
  
 Use any() instead of an expression with a lot of ''or'' operators: 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.1589358671.txt.gz · Last modified: 2020/05/13 10:31 by horst