Python Scripting

And they asked him, “Are you the one who is to come, or shall we look for another?” And Jesus answered them, “Go and tell John what you hear and see: the blind receive their sight and the lame walk, lepers are cleansed and the deaf hear, and the dead are raised up, and the poor have good news preached to them. Blessed is the one who is not offended by me.”

Matt 11:3-6

I'm a teacher by vocation and gifting; I love to teach. By corollary, I love to learn. I'm curious about everything and I have a blast learning about almost anything. Over the years, I've picked up quite a bit of information through the Internet. This website is my simple attempt to pay back; to contribute to the community and try to make the interwebs a better place.

As such, I don't require registration. I don't want your personal information. I don't set cookies. I won't put up advertisements. If I mention a product or service, it's because I use it, benefit from it, and I think others would too. I don't need, or want, your money. I simply want to offer some information to other curious characters.

I'm writing this site as an experiment. It's all written in a text editor. I make my own graphics, write my own JavaScript, CSS, PHP, and HTML from scratch because I'm trying to learn more about how all these pieces fit together. I use TextWrangler because I think it's a great text editor and it does everything I want in a very simple and efficient way.

If you want to contact me, try FaceBook or Twitter.

Original: March 16, 2012

Update: May 14, 2017

Python is a cross platform scripting language. It uses an interpreter rather than a compiler so writing and testing a script is pretty painless. There is an IDE (integrated development environment) called IDLE in homage to Eric Idle of Monty Python fame—the namesake for the language, but I prefer to edit using TextWrangler and test in terminal.

I taught myself to script in Python, but I already know several languages so it was mostly a matter of learning the Python syntax and some of the unique, and quite brilliant, features of the language. Python has some very nice data structures, including a dictionary class, and it is great for parsing strings. Out of the box it's a little weak when it comes to math, but there are several add on modules that fill in the gaps. When combined with Unix shell scripts Python makes a very powerful automation system.

If you don't have much programming experience or you just prefer to have someone to guide you through the learning process, I suggest heading over to Code Academy and taking their Python course. It is a free online course for beginners. The course is designed to last a year so there is plenty of time to learn quite a bit. I took their JavaScript course and it was decent. That said, some of the lesson authors write pointless, trite lessons and the code checking module is a major source of frustration. You can sneak bad code past their code checker at certain times and you can't get good (superior) code past it at other times. However, most of the authors do a good job, and if you put in the work you will learn to code.

Regardless of how you decide to proceed you will need to set up a programming environment. If you don't want to commit too much at first, Code Academy has the Python 2 environment (editor and console) set up on their web site; no installation necessary. If you use OS X then Python is already installed, but like Code Academy it's version 2. There are enough differences that I think it's worth installing the latest version 3 available at Since I prefer Python 3, and Python 2 is installed by default, I include the first line of the following at the top of each script:

#!/usr/bin/env python3 # _._ coding: UTF-8 import sys
The first line directs the OS to use Python 3 to execute the script. The next line line specifies the text encoding in the script to be UTF-8, and the third line imports the system module. I may also include other modules in the third line depending on the complexity of the script.

I suggest you install TextWranger for free from the app store. Open a new document and type the following:

#!/usr/bin/env python print('Hello old friend.') print('How are you today?')

After you have typed that much you can click the she-bang (#!) menu item and select 'run in terminal'. If you have typed everything correctly you will be rewarded with a terminal window that will have a bunch of text, but the two lines before the word 'logout' will be the message your program is printing to you. You're off and running.

Play around with Python to get your feet wet and write a few simple scripts just to get a handle on the process. Some programming suggestions to get started:

Some keywords and syntax for a quick start:

name = input("what is your name: ") # Python 3 name = raw_input "what is your name: " # Python 2 if name = "": name = input("I really want to know your name: ") #the indenting is peculiar but necessary in python else: print("Hello ", name) #this really is the end of the if statement -- this is why the indenting is necessary. It's the same idea with loops and functions. sentence = input("Enter a sentence: ") words = [] #words is an array - no variable declaration necessary in most cases though I like to do it words = sentence.split(" ") #parse the sentence at white space for word in words: #loop through the array and print the sentence one word at a time on each line print(word) import math x = float(input('Enter a positive number: ')) if x<0: print('I asked for a positive number but I can still do it.') print('The square root of', x, 'is', math.sqrt(abs(x)),'i.') else: print('The square root of', x, 'is', math.sqrt(x),'.') total = 0 for i in range(1, 21): #i runs from 1 to 20 print("{:} + {:} = {:}".format(total, i, total + i)) total += i import math def C(n, r): #defines a statistical combination function C = math.factorial(n)/(math.factorial(r)*math.factorial(n-r)) return C def bounce(height, cr): # function to return the number of bounces from a given height with a coefficient of restitution = cr bounces, total = 0, 0 while height>=.1: # bounce the ball until its height is less than .1 m height = height * cr bounces += 1 return bounces print(bounce(20, .75))

One of the things that helped me to learn some of the unique features of Python was to take the python programming challenge. The challenge is really well constructed. It forces you to research and learn to do things the "Python way" rather than just translating methods better suited to other languages. It's a great challenge and a lot of fun in a geeky sort of way. I highly recommend it. I also do the chanllenges in Advent of Code and Project Euler with Python

All content and graphics copyright (c) 2012-2017 Brian Dentler - all rights reserved.