Editing Python Code Example Files

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 3: Line 3:
 
When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks" that I will describe here.
 
When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks" that I will describe here.
 
I like to have many examples in one file to make them easy to review, and find the right example.  Comments can be used to turn on/off some of the code.  I use a technique to make it easy to control
 
I like to have many examples in one file to make them easy to review, and find the right example.  Comments can be used to turn on/off some of the code.  I use a technique to make it easy to control
examples by commenting a single line: put each example in a function and then comment in/out the function call.
+
examples by commenting a single line: put each example in a function and then comment in/out the function call.
 
 
= Moved =
 
 
 
I am beginning to move the actual code to:
 
 
 
*'''[https://github.com/russ-hensel/python_example_code russ-hensel/python_example_code: python_example_code ]'''
 
 
 
an there is a better wiki for this topic there.
 
  
 
= How =
 
= How =
  
 
I think this is best seen with a commented example file.  The example is very simple, the central point is the technique of using subroutines.  You should be able to copy the code into your IDE of choice and run it.
 
I think this is best seen with a commented example file.  The example is very simple, the central point is the technique of using subroutines.  You should be able to copy the code into your IDE of choice and run it.
I am pretty far out of compliancee with PEP 8, but it is my file so I format it the way I want.  Run it through Black if you want.  Formatting is not the point here.
+
 
 
<pre>
 
<pre>
  
# -*- coding: utf-8 -*-
+
comming...
 
 
""""
 
this is a template/example file for booleans
 
( it is commented also as a guide to the subroutine technique for example files )
 
 
 
 
 
    In python pretty much everything has a truthiness which is "boolean like""
 
 
 
 
 
Try searching on:
 
    True
 
    truthy
 
    ....
 
 
 
Author: Russ Hensel  You can reach me ( EMAIL ) at:
 
    no_spam_please_666 at comcast dot net (REFORMAT FOR A VALID EMAIL ADDRESS).
 
    This address is just for first contact, I will respond with a better
 
    address after first contact.
 
 
 
"""
 
 
 
 
 
# a helper function is not an example but will be used by an example
 
 
 
# ------------ helper function ------------------
 
def is_truthy( a_value ):
 
    """
 
    what is says: is a_value truthy or not?
 
    """
 
    if a_value:
 
        return "truthy"
 
 
 
    else:
 
        return "Not truthy"
 
 
 
# ------------ helper function ------------------
 
def eval_me(  exp ):
 
    """
 
    print and evalueate exp
 
    """
 
    # print( msg )
 
    print( f"eval of {exp} is {eval(exp)}")
 
 
 
 
 
 
 
# all example functions start with ex_ and right after the function definition
 
# is a function call.
 
# try to make name discriptive
 
 
 
 
 
# ----------------------------------------
 
def ex_is_truthy():
 
 
 
    # I alsays print something so I know which example is running
 
    print( """
 
    ================ ex_is_truthy(): ===============
 
    test to see what simple expressions have what truthyness
 
    """ )
 
 
 
    exp  = "is_truthy( 0 )"
 
 
 
    test_me  = [ "True", "False", "0", "1", '"TRUE"', '"FALSE"', "1 < 2" ]
 
 
 
    for i_test in test_me:
 
 
 
        exp    = f"is_truthy( {i_test} )"
 
        eval_me(  exp)
 
 
 
# call to function, comment out to eliminate the entire example
 
 
 
ex_is_truthy()
 
 
 
 
 
# ----------------------------------------
 
def ex_case_statement_using_if():
 
    print( """
 
    ================ ex_two(): ===============
 
    just show another example
 
    """ )
 
 
 
    x  = 3
 
 
 
    if  x == 1:
 
        print( "x is one" )
 
    elif x == 2:
 
        print( "x is two" )
 
    elif x == 3:
 
        print( "x is three" )
 
    elif x == 4:
 
        print( "x is four" )
 
    else:
 
        print( "what else could x be?"    )
 
 
 
#ex_case_statement_using_if()
 
 
 
 
 
"""
 
typically I would have many more examples in the file....
 
"""
 
  
 
</pre>
 
</pre>

Please note that all contributions to OpenCircuits may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see OpenCircuits:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)