So, now you know how to output text. Programming, obviously, is about a lot more than outputting text, however. The whole idea behind programming is you instruct the computer to "do this, then do that", but all I've taught you is to instruct the computer to "print this, the print that". That's a good start - every program of any decent length should output helpful messages. But the problems you are trying to solve (because every program is just trying to solve a problem) are probably not yet tractable.

Well, here's where you learn the two most important ideas in computer programming: variables and function calls. Armed with these two things, all of a sudden your problems will seem a little more attackable. Like all big ideas, these two things are simple ideas that can be difficult to express.

Variables

A variable is a container for data. The data contained can then be reused. You set a variable's value by using the = sign. That means that if you want to print out the string "I am the very model of a modern major general" 5 times, all you have to do is:

majorstring = "I am the very model of a modern major-general"
print majorstring
print majorstring
print majorstring
print majorstring
print majorstring
That seems kind of useful - it does save you a lot of typing- but variables get really useful for things like math. Let's say there are two ways of displaying your temperature results - you can print your temperature in Celsius or Fahrenheit. Well, (just like you might hope) you don't have to regather your results when you want to print them again. You simply save the temperature in a variable. Let's say we have the temperature in Celsius, we'll store it in a variable named celsius and watch what we can do:
celsius = 22
print "The temperature is", celsius, "degrees C, or", celsius*9/5+32, "degrees F"
Try it! If you find yourself often converting between temperatures, you might want to save this program. It could be useful to you in the future. Now, when you want to convert to a new value, you all you have to do is change one value.

But wait, there's more! You can now increment things. All of a sudden, you can print out the multiplication tables without you needing to input everything.

row = 1
print row*0, row*1, row*2, row*3, row*4, row*5, row*6
row = row+1
print row*0, row*1, row*2, row*3, row*4, row*5, row*6
The code above makes sense - the only new concept is the third line: row = row+1. This is clearly not a valid mathematical statement. If you think of it as an instruction, however, everything becomes clearer. What you are instructing the computer to do is to calculate the value of row+1 and store the result in row. So first the right side gets evaluated, and then the left side.

This seems counter-intuitive the first time you happen upon it, but x=x+1 is an incredibly common idiom so it's time you got used to seeing it. Just remember that = means "store" not "are equal".

Functions

Ahh, functions. This is where things get really useful (and interesting). A function is a piece of code that (optionally) looks at the values given to it, performs some action based on those values, and then (optionally) returns a result. That's a sort of detatched and mathematical way of expressing it, but it's the best I could come up with. Functions are reusable bits of code, written by you or someone else. They help you avoid reinventing the wheel. Let's look at what you can do with these things...

What if I wanted to read in a file? I'd use the open() function. How about if I wanted to convert a string to an integer or floating point number? The int(), and float() functions, respectively. Finding a number's absolute value? abs(). Getting input from the user? raw_input(). Obviously, it's that last one that we care the most about.

Lets look at some functions in action:

print "What is your name? "
name = raw_input()
print "What is your age? "
age = int(raw_intput())

year = 2002
print "Your name is", name
print "You are", age, "years old"
print "you were born in the year", year-age
Of course, repeating entered information isn't that useful, but to return to that temperature conversion thing, we could do the following:
print "Enter degrees Celsius"
celsius = float(raw_input())
print celsius, "degrees C is", (9*celsius)/5 + 32, "degrees F"
Now this is a useful program! Using this model you can write a program to perform any conversion automatically - a very useful thing indeed.

Also, you no longer have to reinvent the wheel. All of the builtin functions of Python should be listed in the documentation that it came with. Using these functions you should be able to do almost anything!

By now you should be feeling a heady rush of power. You can now write some actually useful programs. Think about it... how much of your day is spent munging strings? Now that can all be done for you. This is usefulness, which is the whole point of computers in the first place. You should never have to do repetitive math again. If you know you're going to have to do it a few times anyway, just write a program that will read in the data and print out the result. Now you can get your job done more quickly. Which, (if you don't let on about it) can lead to more time for fun!

Before we go any farther with functions, however, there are several concepts you'll need to know about...

<< | Learn to Program | >>

Log in or registerto write something here or to contact authors.