Python: Difference between revisions

From LPTMS Wiki
Jump to navigation Jump to search
mNo edit summary
Line 29: Line 29:
== Tips ==
== Tips ==


* equivalent of the C ternary operator ?: (''bool'' ? ''restrue'' : ''resfalse''), use a tuple
* equivalent of the C ternary operator ?: (''bool'' ? ''restrue'' : 'resfalse''), use a tuple
<source lang="py">
<source lang="py">
(resfalse,restrue)[bool]
(resfalse,restrue)[bool]
Line 45: Line 45:
str.isdigit() # returns True/False
str.isdigit() # returns True/False
str.isalpha() # returns True/False
str.isalpha() # returns True/False
</source>
* Nested for loops in a single line:
<source lang="py">
for n,m in [ (n,m) for n in range(10) for m in range(2) ]:
    print n,m
</source>
</source>

Revision as of 15:16, 21 September 2012

documentation

Libraries and softwares

Miscellaneous

Tips

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

<source lang="py"> (resfalse,restrue)[bool] </source>

  • adding a path to a directory containing your module files

<source lang="py"> import sys sys.path += [ "/home/username/bin/Python" ] </source>

  • test whether a string has only digits or letters

<source lang="py"> str = '1321' str.isdigit() # returns True/False str.isalpha() # returns True/False </source>

  • Nested for loops in a single line:

<source lang="py"> for n,m in [ (n,m) for n in range(10) for m in range(2) ]:

   print n,m

</source>