Developing in the Shell

Getting down to business


By now, you should be familiar with how to use Terminal to navigate through directories and create and edit files with Vim, even if it's just barely. But lets start playing around with some actual code to see why the hassle of learning the Command Line is actually worth while.

This tutorial requires any version of Python 2.7. Type python -V to check your version of Python.

Let's start by writing our first Python program! It will be very simple, but hopefully it will be enough to whet your appetite so that you can go above and beyond this tutorial. We'll begin by opening Terminal and navigating to a directory where you will store all of your work. Then, let's create a new Python file using Vim.

cd ~/Desktop/
mkdir gate-tutorial
cd gate-tutorial
vim example.py

Inside of your Vim session, type the following block of code and see if you can figure out what it's going to do before you execute it. I recommend typing it all in to help you remember, but if you'd like to copy and paste into Vim enter Insert mode (hit i) and paste like you normally would. Don't worry if you don't understand what this code is doing, we're going to cover some Python basics next!

def repeat(times):
  for i in range(times):
    print "Hello"

repeat(4)

To execute your new Python script save and quit Vim (:wq), and then do the following:

python example.py

Any Python script you write can be executed at the Command Line by typing python <file_name.py>. Click here to verify your output.

Hello
Hello
Hello
Hello

Python Basics

Getting Started