{"id":34,"date":"2010-10-30T12:00:34","date_gmt":"2010-10-30T16:00:34","guid":{"rendered":"http:\/\/www.deadcodersociety.org\/?p=34"},"modified":"2010-10-27T13:40:54","modified_gmt":"2010-10-27T17:40:54","slug":"introduction-python-thomas-rush","status":"publish","type":"post","link":"http:\/\/www.deadcodersociety.org\/blog\/introduction-python-thomas-rush\/","title":{"rendered":"Introduction to Python: Thomas Rush"},"content":{"rendered":"<p>Tom&#8217;s presented an introduction to the Python programming language, and provided an in-depth exploration complete with example code. He also touched on his Map Editor project, which he originally designed using Python and began to port to C#. You can read his notes below, and download his source code at the bottom of this post:<\/p>\n<p><strong>Python superbrief overview<\/strong><\/p>\n<p>There are currently two major branches of Python, 2.x and 3.x. The 3.x branch doesn\u2019t add anything very significant to the language, and is more to clean up a few things that were seen as inconsistent. However, code is not backwards-compatible from 3.x to 2.x. So if you download the 3.x interpreter and attempt to run a module from a project written in 2.x, you may have problems.<\/p>\n<p>There is a means of converting 2.x to 3.x code, but there\u2019s nothing bad about sticking to 2.x. If you\u2019re not sure which one to grab, I would grab <!--more-->2.x just to be sure that you\u2019ll be compatible with a higher percentage of modules out there.<\/p>\n<p>Python is an interpreted language, and does not produce platform-specific executables. Your download of Python will include its interpreter and an \u201cIDE\u201d called IDLE. IDLE isn\u2019t a full-blown IDE, but it\u2019s a great place to play around with Python code and test things out. It\u2019s a shell where you can throw Python commands at the interpreter and check results.<\/p>\n<p>There are a bunch of Python IDEs out there. I use either <em>Notepad++<\/em> or <em>PyDev<\/em> which is a full-featured add-on for Eclipse. There are a ton of other options including <em>WingIDE<\/em> and <em>Kommodo<\/em> <em>IDE<\/em>.<\/p>\n<p>List of Python IDEs and discussions:<\/p>\n<p><a href=\"http:\/\/wiki.python.org\/moin\/IntegratedDevelopmentEnvironments\">http:\/\/wiki.python.org\/moin\/IntegratedDevelopmentEnvironments<\/a><\/p>\n<p>Python is object-oriented, but can be written like a script (for instance, you don\u2019t have to define a class just to be the container for a Main method where execution starts). Python reads code from top-to-bottom, left to right.<\/p>\n<p>Python is strongly typed, meaning that it cares about what types your objects are. For instance, if you attempt to concatenate an integer to a string, it\u2019s going to yell at you for not explicitly casting that integer. It\u2019s also dynamically typed, meaning that you can reassign variables at runtime without any problems. The secondary effect of this is that you don\u2019t have to define variables, you can simply say \u201ci=5\u201d. This could potentially cause problems, since type errors will be caught at runtime instead of at compile time, but as long as you know that you have to be careful with types going in, you should be ok.<\/p>\n<p>Useful functions:<\/p>\n<p><strong>dir()<\/strong> &#8211; the parameter to this function will be an object you wish to inspect. It returns a list of the available functions for the object<\/p>\n<p><strong>type()<\/strong> \u2013 the parameter for this function is an object which you want to know the type of. It\u2019s better to write code that doesn\u2019t need to you keep checking types. If you start off every re-used function with a check to see if you received the object type that you\u2019re expecting, then you\u2019re doing something wrong (so sayeth the Pythonistas).<\/p>\n<p><strong>help()<\/strong> \u2013 If you issue this command without a parameter from the interpreter command line, it puts you in help mode where you can get quick explanations for most of the language keywords and features. Putting a parameter with a descriptor brings you targeted help.<\/p>\n<p>Everything in Python is \u201cby ref\u201d, except for the immutable types (tuples, ints, strings, etc). If you pass an object into a function and change it, it changes in the calling code. If you want to make a copy of an object, you can use <strong>deepcopy<\/strong>:<\/p>\n<p>from copy import deepcopy<\/p>\n<p>a = deepcopy(b)<\/p>\n<p>Python uses whitespace to delimit code blocks. The first time you indent (to show a separate code block), Python remembers this indent standard and expects the rest of the code to use it. Using whitespace as a code block delimiter eliminates the need for curly braces, and forces good coding habits.<\/p>\n<p>You do not have to end a Python statement in a semicolon unless you want to put multiple commands on one line.<\/p>\n<p><strong>Importing modules can be done in four ways:<\/strong><\/p>\n<p><strong>from<\/strong> name <strong>import<\/strong> <strong>*<\/strong> \u00df (this imports every function name from the module directly into your current namespace. Not advised to do this)<\/p>\n<p><strong>from<\/strong> name <strong>import<\/strong> function \u00df (this imports one function into your namespace)<\/p>\n<p><strong>import<\/strong> name \u00df (imports the module itself; you can then call functions off of it using name.function)<\/p>\n<p>name<strong> = __import__(\u2018<\/strong>name<strong>\u2019)<\/strong> \u00df (imports using a string; the advantage here is that you can use a variable to represent the name of the module you want to import)<\/p>\n<p><span style=\"text-decoration: underline;\">The three main import uses with examples:<\/span><\/p>\n<p>import math<\/p>\n<p>math.sqrt(9)<\/p>\n<p>&#8212;-<\/p>\n<p>from math import sqrt<\/p>\n<p>sqrt(9)<\/p>\n<p>&#8212;-<\/p>\n<p>from math import *\u00a0\u00a0 # However, this imports EVERYTHING into your namespace. Not good!<\/p>\n<p>sqrt(9)<\/p>\n<p>&#8212;-<\/p>\n<p>Python supports all of the basic types you\u2019d expect \u2013 integers, strings, floating point variables, characters, etc\u2026 Two data types you will use extensively are Lists (dynamic arrays) and Dictionaries (hashmaps). Python is extremely expressive in terms of working with these types.<\/p>\n<p><strong>See code samples for examples of List usage<\/strong><\/p>\n<p>The amount of useful modules out there for Python is what makes it great.<\/p>\n<p><span style=\"text-decoration: underline;\">PyGame<\/span> : SDL wrapper similar to how XNA is DirectX wrapper. Graphics\/sounds\/input<\/p>\n<p><span style=\"text-decoration: underline;\">Py2Exe<\/span>: for building platform-specific executables<\/p>\n<p><span style=\"text-decoration: underline;\">Beautiful Soup<\/span> \u2013 for manipulating, stripping, replacing or otherwise working with markup text<\/p>\n<p><span style=\"text-decoration: underline;\">SciPi + NumPy<\/span> \u2013 functions for use in science and statistics\/numerical analysis<\/p>\n<p><span style=\"text-decoration: underline;\">Iron Python<\/span> &#8211; .net implementation of Python; uses the .net class library and compiles down to runnable .net code.<\/p>\n<p><span style=\"text-decoration: underline;\">Jython<\/span> \u2013 Java implementation of Python; uses the Java class library and compiles down to runnable java bytecode<\/p>\n<p><span style=\"text-decoration: underline;\">wxPython \/ pyQt<\/span> \u2013 UI frameworks<\/p>\n<p>The built-in Python modules provide an incredible array of functionality.<\/p>\n<p>Database access, working with audio files, file compression, xml parsing, multithreading, object serialization, working with images, server functions, encryption, hashing, FTP, email, working with cookies, etc\u2026<\/p>\n<p>There are thousands of great projects out there. Chances are, if you need something for a program you\u2019re writing, someone has a module out there that will make your life easier.<\/p>\n<p><a href=\"http:\/\/www.deadcodersociety.org\/blog\/wp-content\/uploads\/2010\/10\/PythonOverview.zip\">PythonOverview.zip &#8211; Notes and Source Files<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thomas Rush gave an introduction to the world of Python<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_links_to":"","_links_to_target":""},"categories":[3],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/posts\/34"}],"collection":[{"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":4,"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":113,"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions\/113"}],"wp:attachment":[{"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.deadcodersociety.org\/blog\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}