Difference between revisions of "Python"

From LPTMS Wiki
Jump to: navigation, search
(J'ajoute une petite rubrique sur la lecture de gros fichiers en python)
(Tips)
Line 33: Line 33:
 
== Tips ==
 
== Tips ==
  
* equivalent of the C ternary operator ?: (''bool'' ? ''restrue'' : 'resfalse''), use a tuple
+
* equivalent of the C ternary operator ?: (''test'' ? ''restrue'' : 'resfalse''), use a tuple
 
<source lang="py">
 
<source lang="py">
(resfalse,restrue)[bool]
+
(resfalse,restrue)[test]
 +
</source>
 +
or the
 +
<source lang="py">
 +
res = restrue if test or resfalse
 +
# example
 +
min = lambda x,y: x if x<y else y
 +
min(1,2)
 
</source>
 
</source>
  

Revision as of 15:23, 16 July 2015

Documentation

Libraries and softwares

Miscellaneous

Tips

  • equivalent of the C ternary operator ?: (test ? restrue : 'resfalse), use a tuple

(resfalse,restrue)[test]

or the

res = restrue if test or resfalse
# example
min = lambda x,y: x if x<y else y
min(1,2)
  • adding a path to a directory containing your module files

import sys
sys.path += [ "/home/username/bin/Python" ]
  • test whether a string has only digits or letters

str = '1321'
str.isdigit() # returns True/False
str.isalpha() # returns True/False
  • Nested for loops in a single line:

for n,m in [ (n,m) for n in range(10) for m in range(2) ]:
    print n,m