Fork me on GitHub

So You Want To Learn Python

This page is used to capture some useful tips-n-tricks to create Python programs. It’s for my nephew, but it can be for anyone that you want to share it with.

Getting Python onto your computer

Do you have a Mac? Great! You don’t need to do anything more, because you already have Python. Where is it? You won’t find it underneath Applications. So try this:

  1. Hit Cmd+Spacebar. You should see a little box pop-up named Spotlight in the top-right corner.

  2. Enter terminal. An application called Terminal should appear.

    spotlight
  3. Pick it with the mouse.

  4. A new window should open up. It’s probably white with black text.

  5. Type python -V to see what version of Python you have. (Note the capital V)

    terminal
  6. The last thing you need is an editor to write Python programs. Go to TextWrangler and click on the Download button. Don’t worry; it’s free!

  7. After it finishes, open TextWranger_<version>.dmg.

  8. You’ll see a pop-up screen. Drag the TextWrangle icon into Applications.

  9. The first time you launch TextWranger, you might see something like TextWrangler is an application downloaded from the Internet. Are you sure you want to open it?. Click Open.

You’re all set!

Your first Python program

To get off the ground, let’s write something really simple. For starters, you might want to put all your programs in one place.

  1. Using Finder, create a new folder on your Desktop. Call it myprograms.

    myprograms
  2. Open myprograms.

  3. Position the windows so you can see the terminal and the myprograms at the same time.

  4. Inside Terminal:

    1. Type cd ~/Desktop/myprograms (cd lets you Change Directories to that folder you just made.)

    2. Type touch app1.py (this creates an empty file so we can write our first program.)

  5. Inside Finder:

    1. You should see app1.py appear automatically.

    2. Right click app1.py and navigate to Open WithOther....

    3. Scroll down and pick TextWrangler. Check Always Open With. Then click Open.

      pick_textwrangler

    4. You’ll see an option to register. Click Cancel.

  6. Inside TextWrangler:

    1. Enter a tiny program.

      app1 textwrangler
    2. Either navigate to FileSave, or simply hit Cmd+S and save your new program.

  7. Inside Terminal:

    1. Type python app1.py, and you should see the greeting get printed to the screen:

      $ python app1.py
      Hello, world

You just created your first Python program!

Can you guess what’s happening?

  • print is a command to write something to the screen.

  • Anything inside the double-quotes is what gets written to screen. The stuff inside the double-quotes is called a string, which is short for string of characters.

This is probably the absolute simplest program one could possibly write in Python. But don’t let that fool you. Giant companies like Google, Amazon, eBay, and others use Python to run big stuff. And I use it all the time as well.

Questions?

  1. Why can’t I use TextEdit?

    TextEdit comes with your Mac, but unfortunately, it tries to make the text too fancy. To write Python programs, you need things to be plain text.

  2. What about Microsoft Word or some other word processor?

    These tools are better used when writing papers for school. They let you format the text with bold, italics, and other things. This type of stuff confuses python and won’t run very well. That’s why there are instructions to download TextWrangle.

  3. Why does my program inside TextWrangle show different colors?

    If you look at the program inside TextWrangle up above, you’ll notice that print is black while "Hello, world" is red. That’s called syntax highlighting, a handy feature that as you get more familiar with Python, will let you spot mistakes quicker. Notice at the bottom of the editor, you can see Python. That’s because TextWrangler speaks Python and can help you out.