Python Primer

About Python

Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants, on the Mac, and on Windows 2000 and later.
https://docs.python.org/3/faq/general.html#what-is-python

Download Python

All Python releases are Open Source. Historically, most, but not all, Python releases have also been GPL-compatible. The Licenses page details GPL-compatibility and Terms and Conditions.
https://www.python.org/downloads/

To Chose between Python V2 or V3 visit this page. IronPython, Python with .Net, runs V2 for example.

Starting Python

IDLE is the Python IDE built with the tkinter GUI toolkit.
IDLE has the following features:

  • coded in 100% pure Python, using the tkinter GUI toolkit
  • cross-platform: works on Windows, Unix, and Mac OS X
  • multi-window text editor with multiple undo, Python colorizing, smart indent, call tips, and many other features
  • Python shell window (a.k.a. interactive interpreter)
  • debugger (not complete, but you can set breakpoints, view and step)

https://docs.python.org/3/library/idle.html

Do the following:

    • Click Start, point to All Programs, point to Python 3.4, and then click IDLE (Python GUI).

Hello World!

  1. Open IDLE
  2. Click File and then click New File
  3. Type print('Hello World!')
    1. Note: In Python V2 it would be print 'Hello World!'
  4. Execute the code by pressing [F5]
"Hello World!"

Variables

The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.
Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.
Some operations are supported by several object types; in particular, practically all objects can be compared, tested for truth value, and converted to a string (with the repr() function or the slightly different str() function). The latter function is implicitly used when an object is written by the print() function.
https://docs.python.org/3/library/stdtypes.html

message = 'Hello World!'
print(message)
"Hello World!"

Functions

The keyword def introduces a function definition. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line, and must be indented.
https://docs.python.org/3/tutorial/controlflow.html#defining-functions

message = 'Hello World!'
def WriteMessage():
    print(message)
# Call the function
WriteMessage()
"Hello World!"

Passing Variables to Functions

Previously, message was accessible because its scope was Global. You can pass variables and values to functions by appending those values to the function call. Also, your function must expect a value. In this example, we pass a string into the function that we declare as message.

def WriteMessage(message):
    print(message)
# Call the function
WriteMessage('Hello World!')
"Hello World!"

Error Handling

The try statement specifies exception handlers and/or cleanup code for a group of statements.
The except clause(s) specify one or more exception handlers. When no exception occurs in the try clause, no exception handler is executed.
If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses.
https://docs.python.org/3/reference/compound_stmts.html#the-try-statement

import sys, os

def WriteMessage(message):
    print(message)

try:
    # Call the function
    WriteMessage('Hello World!')
except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print(exc_type, fname, exc_tb.tb_lineno)
finally:
    # Write message
    WriteMessage('Finally...')
"Hello World!"

Further Reading

Python Documentation

  • The Python Software Foundation’s documenation for Python.

Python Wiki

  • The Python Wiki’s collection of programming guides, documentation, books, tools, and community.

Related

Pytools

  • PTVS is a free, open source plugin that turns Visual Studio into a Python IDE.