Editing Russ Python Tips and Techniques

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 7: Line 7:
 
* when division by 0 is useful
 
* when division by 0 is useful
 
* code is multiple files - scratch file
 
* code is multiple files - scratch file
 +
* msg, print( msg )
 
* moving to 3.6 use fstrings
 
* moving to 3.6 use fstrings
 
* example files with functions  [[Python Example Code]]
 
* example files with functions  [[Python Example Code]]
Line 12: Line 13:
 
* dict based case statements
 
* dict based case statements
 
* choose names for cut and paste
 
* choose names for cut and paste
 +
* spyder using comments for navigation
 
* mark your code with unusual string  * zzz in your code  
 
* mark your code with unusual string  * zzz in your code  
 
+
* make your names searchable
  
 
=== Move these to a good place in the outline ===
 
=== Move these to a good place in the outline ===
Line 81: Line 83:
  
 
If you do not see an easy way to do this with names use a comment.  Using zzzz.... is a short term way of doing this.
 
If you do not see an easy way to do this with names use a comment.  Using zzzz.... is a short term way of doing this.
 
==== Dict Based Case Statements ====
 
 
 
Python does not have a case statement, but it does have if....elif which can be used and is only a little more verbose.  However if you have more than a few cases it can be cumbersome.  Using a dict gives a very compact and fast case statement.  Suppose you want to have a case statement based on strings:
 
 
 
<pre>
 
    # set up, ideally only done onece, perhaps at module or class level
 
    dispatch_dict  = { "one":    print_one,
 
                        "two":    print_two,
 
                        "three":  print_three,
 
                        }
 
 
    # set a case for an example call:
 
    key = "two"
 
 
    # the slightly verbose case statement
 
    function      = dispatch_dict[ key ]
 
    function()
 
</pre>
 
  
 
= Parameters for Applications =
 
= Parameters for Applications =

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)