Python Example Code
If you develop much in Python you may find it useful to save example code, snippets of code, or templates to copy and use later. If you do this it is also nice to be able to run the code so you can check it out and mess with it a bit. Keep in mind that copying code is easier than writing it. Be an expert at both
A Trick for Keeping Your Own Example Code[edit]
This page presents a trick that makes this process more convenient.
In the past I kept the code in moderate sized files with many examples in each. I would comment out all the examples I wanted to keep but not run, then run the one I did. The commenting process was more of a pain than I wanted. In my simple trick I define the code in a function then only have to comment in and out the function call. I will list an example here you can use it as a template to make more of your own.
# ---------------------------------------- def ex_0(): print """ ================ ex_0(): Powers =============== """ a = 10 b = 20 c = a**b # powers = 10 to the power 20 print c ex_0() # ---------------------------------------- def ex_1(): print """ ================ ex_1 Floor Division rounded away from zero =============== """ print( "10/4 = " + str( 10/4 ) ) print( "10//4 = " + str( 10//4 ) ) print( "10.//4. = " + str( 10.//4. ) ) print( "-10.//4. = " + str( -10.//4. ) ) #ex_1() # ========================================================== def ex_2(): print """ ================ ex_2(): modulo or remainder =============== """ a = [30,40,50,60,70,80,90,100,120] a = range( 0, 40 ) for ix, i_a in enumerate(a): if ix%3 == 0: print ix, i_a #ex_2()
Links to Example Code[edit]
There is also a lot of example code out on the web. It can be useful if it does not take too long to find. You can try these.