Python programs read from top to bottom with (typically) one command on each line. There are many commands you can use, and we'll start out very simply.
Before we go on, let me say first that the sort of programs you will write (at least at first) will be small and non-graphical. Graphics take a great deal more time, and a lot more programming. BUT that being said, the basic programming you will learn here can point you in that direction. I hope that this blog will continue long enough to get to the point where I can show you the way I made games on my website.
The print Command
The first language I learned was called QBasic, and the very first command one learns in BASIC (more often than not) is the print command.
All right. It's time to fire up the Python interpreter. (If you don't remember how to do that, click here.)
I'll be using my Mac for now. Maybe later I'll switch over to a Windows machine.
Time to write your very first line of Python script ever! Simply type (or copy and paste):
print "Hello World"
and hit the return key (or the Enter key).
Ta da! Python executed your line of code! The "print" command simply writes something out to the console. (The Terminal or Command Prompt window is called the "console.")
The "Hello World" part of that command is called a string. A string is simply a "string" of characters (letters, numbers, and symbols), whether its one character or a billion. You'll be using strings a lot in programming, mostly because that's the way that you store and display words. Strings are always indicated by quotation marks, whether double-quotes ("Hello World") or single-quotes ('Hello World').
Don't worry. This isn't algebra. A variable is a container for a piece of data (like a string). Unlike algebra where you're solving for variables, you get to tell that variable who's boss. To assign a variable, you can simply do something like this:
a = "Hello World"
Unlike math class, the single equals(=) sign tells Python to store the string "Hello World" into a variable called a. Now you can do something like this:
Notice how I didn't use quotes. We don't want to print the letter "a"; we want to print the string stored in the variable called a. Here's what your console (remember that term?) should look like:
Now if we try the command (notice the quotation marks)
we should get this:
See how it printed the string "a" instead of the string stored in variable a?
Variable names don't have to be single letters; they can be full words. A variable name can include numbers, letters, and certain symbols (most commonly the underscore (_)) provided the variable does not begin with a number. For example, type this into the interpreter:
myString = "This is a string."
Now print it to the screen using
See how that works? Variable names help you keep track of what the variable is used for.
Now what if you want to type two strings at the same time? Simply use the + sign. When used on two string, Python puts one string directly after the other to form a new string. Type
print a + myString
and you should get something like this:
This is called concatenation (from Latin meaning "linked together").
You may be wondering why there is no space between the word "World" and the word "This"; this is because you did not have a space at the end of "World" and Python just put together what was there. To add a space, you can type something like this:
print a + " " + myString
You can also assign this new string to another variable.
newVariable = a + "! " + myString
(Notice how I added an exclamation point in there.) Now you can type
An important thing to remember about variables is that they are case-sensitive (capital and lowercase letters matter). Try typing this:
Aaaaaaah! What happened? Congratulations, you just experienced your first error. Don't worry about what it says; simply put it means that you tried to use the variable newvariable when it didn't exist.
That pretty much wraps it up for Lesson One. I hope it wasn't too confusing. By way of review, here's what you should have learned:
- The print command puts information on the console.
- Strings are strings of characters surrounded by quotation marks.
- You can use single-quotes (') or double-quotes (") to indicate a string.
- You can store strings in variables.
- Variable names are case-sensitive and can be made up of one or more letters, numbers, or symbols. (Variable names cannot start with a number.)
- Variable names cannot start with a number.
- You can concatenate strings using the plus sign (+)
That's it! See you next time!