Login

Introduction to Python Programming

Python has very little to do with Eric Idle or John Cleese. It’s an interpreted, object-oriented scripting language. Designed for clarity and simplicity, it’s useful for creating large programs and for gluing pieces of other programs together. Keep reading to add Python to your personal programmer’s toolkit.

While the rest of us were cuddled snugly in our beds, the crackling fireplace keeping us nice and warm as we dreamed of the visit from our old pal Saint Nick in the upcoming week, Guido van Rossum was fretting about his week off from work and wondering what he should do with it. He went through several scenarios: spend it with his family? No, that would not do. Volunteer at a kitchen to feed the needy? No, no. How about dressing in a Santa Claus outfit and making some extra loot letting children sit on your lap and cry all day at your horrifying appearance? Tempting, yes.

In the end however, van Rossum decided to create a programming language. He had worked on the programming language ABC (great now that Jackson 5 song is stuck in my head) and wanted to create a descendant of it. Possibly feeling a bit suggestible from an overdose of holiday cheer, he may have kicked back to watch an episode of his favorite show, Monty Python’s Flying Circus — or maybe not. Either way, he named the language Python. And the rest, is history.

Do you know how to use Notepad (if not you are probably too amazed by the pretty light of the monitor to even read this, so please, carry on)?

What is the air speed of a laden swallow? Actually, there is no third question, I just wanted it to fit the bridge scene in "Monty Python’s Holy Grail."

If you said yes to all of those, then you may continue. If you said no to any of those, then go rectify that situation, then come back and read the questions again. Then move on to the next section.

{mospagebreak title=No one expects the Spanish Inquisition!}

As is the case with all computer programming tutorials, the first thing you learn to do is print some text to the screen. Normally it is a stupid “Hello World” program, but you are far too cool for that. Besides, the Spanish Inquisition is seeking us out, and the last thing we want to do is send a big old greeting out to the world. Here is how you print text to a monitor:

#!/usr/local/bin/python #this line must be included in every program

print “We are the Spanish Inquisition!”

The above code will print this to your monitor:

We are the Spanish Inquisition!

To view the program in Python, save the file as firstprogram.py and open it in Python (type python firstprogram.py). Note that you must make sure that you can view and edit file extensions in your operating system, otherwise Notepad will change your file to a .txt and it will not work. To remedy this, simply turn on view file extensions, and rename the file with a .py extension.

You will note several things about the preceding code. First, the first line: #!/usr/local/bin/python tells the computer where Python is located. If you change where your python directory is located, you will have to change it in your code as well. This line must appear in all of your Python code.

Next, the line print “We are the Spanish Inquisition!” tells the computer to literally print the sentence in quotations to the user’s monitor. Pretty simple right?

If we wanted to print more than one line of code we could do this:

#!/usr/local/bin/python #this line must be included in every program

print “We are the Spanish Inquisition!”

print “We have two weapons!”

print “One…fear!”

print “Two…obfuscation!”

print “And vigilance!”

print “Oh shucks did I say two weapons? I meant three.”

print “Why don’t I step outside, then we will re-enter and begin again.”

This would result in the printout:

We are the Spanish Inquisition!

We have two weapons!

One…fear!

Two…obfuscation!

And vigilance!

Oh shucks did I say two weapons? I meant three.

Why don’t I step outside, then we will re-enter and begin again.

A Brief Word On Terminology

In the above code sample we worked with the print command. This was followed by quotation marks and some text, which is referred to as an argument. The text in the argument is referred to as a string. The combination of the command and the argument is known as a statement. So the line: print “We are the Spanish Inquisition!” is an example of a statement.

{mospagebreak title=Working with Numbers}

You can also work with numbers in Python. If you want to do some simple math (or even complicated math) try this:

#!/usr/local/bin/python #this line must be included in every program

print 1 + 1

print 4 * 4

print 6 – 2

print 8 / 2

print 10 ** 2

print (1 + 1) * 2

The above code demonstrates a list of expressions. It would print out the following:

2

16

4

4

100

4

If you want to mix numbers and text together, you could do this:

#!/usr/local/bin/python #this line must be included in every program

print “Spam costs two dollars.”

print “If you bought two spams it would cost”, 2 + 2

print “If you order two spams but one is bad”

print “Your refund would be”, 4 – 2

print “Of course we do catering as well”

print “We charge 1.25 cents for more than 100 spams.”

print “So a thousand spams would cost you”, 1.25 * 1000

print “Spam to the third power is”, 2 * 2 * 2

This prints out the following:

Spam costs two dollars.

If you bought two spams it would cost 4

If you order two spams but one is bad

Your refund would be 2

Of course we do catering as well

We charge 1.25 cents for more than 100 spams.

So a thousand spams would cost you 1250

Spam to the third power is 8

You will notice that some of the above statements have two arguments. A good example is the second line: “If you bought two spams it would cost”, 2 + 2. The text part is one argument, and then the math (or expression) is a second. When you use more than one argument, you must separate them using a comma. You will also note that the expression 2 + 2 is not surrounded by quotations. If it were, then the program would have printed: If you bought two spams it would cost 2+2. Since there are no quotes, it sees it as a mathematical expression and prints the result instead.

{mospagebreak title=Python Mathematical Operators}

Here is a pretty table listing the Mathematical Operators in Python:

Operator

Symbol

What it Does

Addition

+

2+2 == 4

Subtraction

–

4-4 == 0

Division

/

12 / 3 == 4

Multiplication

*

2 * 4 == 8

Exponentiation

**

10 ** 2 == 100 (Basically you are saying 10 times 10; if you wrote 10 ** 3 it would be 10 times 10 times 10 or 1000)

Remainder or Modulus

%

Returns the remainder from a division. 17 % 4 == 1. If there is no remainder, the value would be 0. Ie; 16 % 4 == 0.

Also, when working with math note that if you do not enter an equation in as a decimal, the result will not have a decimal. So if I said 14 / 3, it would give me the result of 4. If I used the expression 14.0 / 3.0 it would return 4.66666666667. In addition, you give mathematical equations precedence by using parentheses(). So: 3 * 2 + 2 would equal 8, whereas 3 * (2+2) would equal 12. This is because Python sees the parentheses and knows to calculate that portion first, then multiply by 3.

Commenting

You may have noticed earlier that I used the # symbol followed by some text. This is known as a comment. When Python sees the # it gets scared and runs away to the next line. It won’t even read what you have written. Comments are used to leave notes for yourself or other programmers who may eventually view your code. They are typically used to describe what a particular snippet of code does. That way when you view your code six years later at four A.M. you don’t rip your arm hair out trying to remember what the heck that function was supposed to do.

Well that is it for this episode. In our next action-packed show, we will discuss how to get data from the user and how to use it, and my good buddy the variable. As always, thanks for dropping in and hope to see you soon.