If you’re a Programmer / Developer, chances are you’ve at least heard of this neat little program called Python. Those who know it sing it’s praises, and it can be found in some very powerful applications. If you’ve been curious about how to get started with Python I have built this tutorial to help you on your way to becoming a text manipulating ninja.
Ok, that sounded a little corny but Python is really, really good at parsing and manipulating text. It’s one of the duties you’ll see it doing often. Let’s get started.
Step 1:Get Python
I’m not going to go into great detail about installation, but you can get python for any of the big three (PC, Mac Linux) operating environments and the setup is pretty similar. In many versions of OSX and Linux Python is installed by default. One way to see if it’s installed is by opening up a command window, and simply typing “python” or “py”. If it’s installed you’ll see something like this:
1 2 3 4 5
If not, the best place to get the latest version is from Python.org.
Plain Text Editor
Running Python at the prompt
The editor I use the most is Kate in Linux. This is because Kate manages your files, and you have a command terminal below, allowing you to enter stuff. If I’m on a windowless machine Vim works very well too. For Windows machines I would recommend Notepad++ to edit. Both are free and there are tons of free text editors out there, especially in Linux.
Your first step is the obligatory “Hello world”. You’ll find it’s nearly as simple as it could possibly be in Python.
Create a file called test.py and put the following line in it:
and save it. Now type:
Amazing right? Now do something silly, to make it spit out an error, and remove the last quote. Type this exactly as you see it:
You should see the following message:
File “test.py”, line 1 print “hello world ^ SyntaxError: EOL while scanning string literal
Like every programming language you have to close everything you open. (With the exception of tags in PHP, but let’s not get into that).
So to clarify, our steps are:
Put python code into a text file.
run that text file with python ( python + filename )
look at the results.
Seems simple enough, so let’s do some more stuff.
A quick note about comments: use them. When you become a better programmer you can decide whether “good code comments itself” but for now, let’s use comments to explain what’s going on. Basically a comment is any text that YOU can see as a programmer, but the computer can’t. This is so you can leave yourself notes, or notes for other programmers.
In Python, you use the # pound sign for commenting, like so:
But usually they’re explaining something a little more important:
1 2 3 4 5 6 7 8 9 10 11
When you run classroom.py the computer (python interpreter to be exact) will ignore all the comments you insert, but they remain in the file for the next person to look at (which could be you).
Speaking of math, let’s do some!
As you saw above it doesn’t take much to do basic arithmatic in Python. Here is one way of performing mathmatic expressions:
1 2 3 4 5 6 7 8 9 10 11 12
Notice how easy it was to insert the text into the sentence? That was done with a variable.
Here is what you should see:
There are 52 weeks in a year
You can do all kinds of things with the output, you can output just answers:
But why stop there?
You can group expressions and output the answer however you like, or feed it into a variable and use the variable.
Python also outputs boolean (true / false) as well:
Since 6 obviously less than 10 it will output true. This statement isn’t exactly useful, but once you start using variables and inputs it will.
Variables are simply containers to put data in. Think of a set of mailboxes for an apartment building. Each mailbox contains someone’s mail (data) and each mailbox is labeled with a number (variable name). The variable is the mailbox, and the number on the mailbox is the variable name.
So let’s create some variables.
1 2 3 4 5 6 7 8 9 10 11 12
Here you can see we’ve created some variables here to hold my name, age, and the current year. We also added in a couple of formulas to figure out how long I’ve been driving, and (legally) drinking alchohol. If you copy this script exactly (don’t!) you will get the following output:
1 2 3 4 5
OK, so this script makes me feel pretty old. Create a similar script for yourself and feel free to modify it. Add in how many years you’ve been married or how long you’ve been in school. I encourage you to take these examples and run with them, modify and go crazy, because that’s the best way to learn. Experiment and don’t worry about breaking things.
Now it’s time to change it up a little.
1 2 3 4
Notice something different? We’re now using a floating point number, which Python detected and used, then output another float. Python supports many different datatypes including integers, long, floats and complex.
In the next tutorial we’re going to get a little more in depth and start exploring some more math, and loops.