Hello World: Python

In this guide, I’ll show you how to setup Python, setup a Python IDE, and write your first program.

Setup

First thing’s first: you need Python. Python comes pre-loaded on OSX, but you should install the latest official distro. OSX install instructions:

  1. Xcode is a prerequisite for the Python install. Install Xcode from the Apple App Store.
  2. users-Mac:~ user$ sudo xcode-select --install


    App Store Get Xcode

  3. Install Xcode Command Line Tools.
  4. users-Mac:~ user$ sudo xcode-select --install

    Install Xcode Command Line Tools

  5. Python is distributed as a homebrew bottle, so you need to install Homebrew!
  6. users-Mac:~ user$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    ==> This script will install:
    /usr/local/bin/brew
    /usr/local/share/doc/homebrew
    /usr/local/share/man/man1/brew.1
    ...
    ==> Installation successful!
    ...
    
  7. Install Python.
  8. users-Mac:~ user$ brew install python
    ==> Installing dependencies for python: readline, sqlite, gdbm, openssl
    ==> Installing python dependency: readline
    ==> Downloading https://homebrew.bintray.com/bottles/readline-7.0.1.sierra.bottl
    ######################################################################## 100.0%
    ==> Pouring readline-7.0.1.sierra.bottle.tar.gz
    ==> Caveats
    This formula is keg-only, which means it was not symlinked into /usr/local.
    ...
    Run `brew linkapps python` to symlink these to /Applications.
    ==> Summary
    ????  /usr/local/Cellar/python/2.7.12_2: 3,477 files, 46.7M
    users-Mac:~ user$
    
  9. Confirm the Python version.
  10. users-Mac:~ user$ python --version
    Python 2.7.12
    users-Mac:~ user$
    
  11. Download and Install PyCharm CE.
  12. Launch PyCharm CE and create a new project.
  13. Install PyCharm

  14. Set the project name and the Python interpreter version to match the version you installed above.
  15. Project 2

  16. Create a Python file.
  17. Create Python File
    New Python File

Hello World!

  1. You’re finally ready for your first Python program: Hello World! Copy the following code into your Python file and click the Run button.
  2. print "Hello world!"
    

    Run Hello World
    Hello World Output

  3. As you explore Python, one of the first things you will discover is that Python uses leading white space to identify code blocks. I’m personally against syntactically significant white space because I find white space harder to read than other delimiters and therefore easier to make mistakes. You can read more about this debate here. You can see an example of this in the “99 Bottles” program below. Notice that the print statement is considered part of the for loop because it is indented. Copy the following code into your Python file and click the Run button.
  4. for i in range(99, 0, -1):
        print i," bottles of beer on the wall!"
    

    99 Bottles Run
    99 Bottles Output

Conclusion

That’s it for this Hello World post. Python is a powerful language once you get used to it and I enjoy programming in it quite a bit. I encourage you to explore the language further and draw your own conclusions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.