An Introduction to Python – Part 1

OS X has a rich array of development/scripting languages. From Objective-C and Swift for building iOS and OS X native apps to scripting solutions like Ruby, PHP, and Python.
Certainly in the Mac technical community Python has become very popular and has proved itself a very versatile solution with regards to building technical solutions. It can be used to create simple automation tasks but can also scale to build more complex software solutions.
In this multi-part series we will show you how to get started with Python. Note: There is an assumption that all demos are running on a standard install of OS X. Also, some very basic command line knowledge would help, specifically changing directories and understanding basic file paths.
Tools
Python is available on all major operating systems. OS X has it built in as standard.
The tools required are very simple. We are just creating text files to hold the Python code. You can use any text editor that can edit plain text. A nice free text editor you can use is TextWrangler, but you could use TextEdit – as long as you set it to edit plain text files. You can also use command line tools such as nano or vi.
A great tool I have been using lately is CodeRunner. This tool lets you run code from a range of languages, including Python, in a nice editor.
Any Python file you create should have the extension .py
My first Python program
It wouldn’t be a programming article if we didn’t start by building the Hello World app.

  • Launch your editor of choice and create a new file.

The first line of any python file should be this:

#!/usr/bin/python
  • Add this as your first line

This is known as the Shebang line and specifies where to find the version of Python to use for the rest of the file. It’s only required if we make the scripts executable. More on this later. Just add this to any new Python file you create from now on.
Our very first command is the print command. This outputs a string of text. Quite useful for outputting textual information when the Python program is running.

  • Add this line to the end your text file
print "Hello World"

Print is the keyword, and the instruction we wish to run. Anything between the quotes is outputted. So in this case the text “Hello World” is outputted.

  • Save this file. Best practice tip: don’t use any spaces in the file name and make sure the file name ends with .py. Call this file hello.py

Running our first Python program
We have several ways of running this program. Mostly from the command line.

  • Launch the Terminal application and change paths to the directory where we save this file.

We will look at two ways of running this program.
The first is to tell Python which file to run. We do this by entering the Python keyword, followed by the path to the file we wish to run.

  • For instance, to run our hello.py application, enter the following into the Terminal app and press return
python hello.py

After entering this line, you should see “Hello World” outputted to the Terminal windows. Congratulations, you have successfully run your first program.

  • Try changing the message that the print command outputs by changing the text between the quotes in the file. Remember to save before running again.

An alternative way of running a program is to make the file executable. When we do this, we do not need to use the python command to run it.

  • To make this file executable for just you, enter this command in the terminal.
chmod u+x hello.py
  • We can now run this program by just specifying the file name like so:
./hello.py

Adding comments
Comments are of great use. We can use them to either document our code or to disable certain commands from running.

  • First, add a second print command to your file. Put this on a separate line, below the last line in the file.
print "Hello, again"
  • Now run the program as we did before. You should see the output of both print commands.

Now we can use a comment to disable any line.

  • Add a hash, # in front of the last line we added. It should look like this.
#print "Hello, again"
  • If you run the program again, the second print command is not executed. Any line starting with # is ignored by Python.

You can use # to add comments, for instance to document who wrote this great program.

#Written by Yoda

This is the end of part 1. Hopefully, it’s got you to a point where we can start looking at Python in more depth. In the second part we will look at variables.