Matt Learns Python: Part 1 - Getting Started

The goal of this blog is to document my journey learning new things in the world of web development and programming. At the same time, I hope to help others learn as well. Feel free to say hello on Twitter!

A friend of mine had been urging me to take a look at Python for awhile. Specifically, he told me about Django, a web framework written in Python, and how great it was. After awhile of giving excuses, I relented and dug in. I’m glad I did.

Before we get started I want to share with you The Zen of Python:

The Zen of Python

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren’t special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one– and preferably only one –obvious way to do it.

Although that way may not be obvious at first unless you’re Dutch.

Now is better than never.

Although never is often better than right now.

If the implementation is hard to explain, it’s a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea – let’s do more of those!

How great is that?! If that doesn’t get you pumped about Python then perhaps you need more coffee. I’ll wait…

… are we good? All caffeinated? Great! Let’s get to this!

Prerequisites #

This post assumes you’re coming from a similar background as myself, meaning you have experience with other web technologies. Things like: PHP, JavaScript, MySQL, etc. If not, that’s probably fine. Most of what I’ll be discussing is low-level. If you find yourself struggling initially, don’t fret! Maybe give a gander at Introduction to Computer Programming Concepts.

One very important prerequisite: You need to be familiar with command line. Most of what we’ll be doing is done there. Check out the awesome Command Line Crash Course if you need help getting started.

Windows users: Power Shell will make your life a lot easier.

Installing Python #

To use Python, we must have Python. More than likely you’re running either Mac OS X, Windows, or some flavor of Linux. Below are resources for getting Python installed on each of those:

Our First Line of Code #

Most “beginning programming” tutorials start with the tried and true “Hello, world!” program. That’s fine. There’s nothing wrong with that in and of itself. In fact, every morning when I awake I rub my eyes, stretch, and proclaim, “Hello, World!” Not really. I’m lucky if I remember my name first thing in the morning. “Hello, world!” is boring. I’m going to use a quote from my favorite movie, The Matrix:

This is your last chance. After this, there is no turning back. You take the blue pill - the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill - you stay in Wonderland and I show you how deep the rabbit-hole goes.

Step 1 #

Open up your favorite text editor. I use Sublime Text, it’s great.

Step 2 #

Find a movie quote you’d like to use and type (no copying and pasting):

print "This is your last chance. After this, there is no turning back. You take the blue pill - the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill - you stay in Wonderland and I show you how deep the rabbit-hole goes."

Be sure to replace my quote with yours. It should look something like this:

Example 1

Save this as example1.py

What’s going on here?

print is a Python command that simply prints text to the screen. When the Python interpreter comes to this command it knows to output whatever is in the proceeding quotes.

Step 3 #

Open up Terminal (or Power Shell for Windows) and navigate to the directory that contains your newly created file. To get to mine I did the following:

cd Documents/mattlearnsthings/python/example1/

Once we’re there, running our script is as easy as typing:

python example1.py

That’s it! Your output should resemble:

Example 1

What’s going on here?

python starts the Python interpreter, next adding a space and typing example1.py tells the interpreter to run our script.

Finishing Up #

In the next post, we’ll dig deeper into printing, strings, and all that jive. In the mean time, here’s some additional reading for you to check out:

I hope you enjoyed my first post! I’d love to hear from you, if you have a Twitter machine find me at @themattsparks

 
7
Kudos
 
7
Kudos

Now read this

Matt Learns Python: Part 2 - More Strings, Variables, and Math!

The goal of this blog is to document my journey learning new things in the world of web development and programming. At the same time, I hope to help others learn as well. Feel free to say hello on Twitter! Previous Post: Part 1 -... Continue →