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:
- Xcode is a prerequisite for the Python install. Install Xcode from the Apple App Store.
- Install Xcode Command Line Tools.
- Python is distributed as a homebrew bottle, so you need to install Homebrew!
- Install Python.
- Confirm the Python version.
- Download and Install PyCharm CE.
- Launch PyCharm CE and create a new project.
- Set the project name and the Python interpreter version to match the version you installed above.
- Create a Python file.
users-Mac:~ user$ sudo xcode-select --install
users-Mac:~ user$ sudo xcode-select --install
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! ...
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$
users-Mac:~ user$ python --version Python 2.7.12 users-Mac:~ user$
Hello World!
- You’re finally ready for your first Python program: Hello World! Copy the following code into your Python file and click the Run button.
- 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.
print "Hello world!"
for i in range(99, 0, -1): print i," bottles of beer on the wall!"
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.