Super Simple Python Tutorial

A super simple tutorial about Python.

No fluff, just for beginners.

Variables

Variables are symbols that represent numbers or strings of letters. Here we assign a value of 5 to the variable myvar:

To assign strings of letters and numbers (e.g., words, sentences, etc.), place them in single or double quotes.

Variables can be manipulated with operators. Here the variable total is 5:

Here the variable sentence is “mooses are awesome”:

This gives an error because it doesn’t make sense:

(Note that “comments,” or parts of your program that aren’t interpreted as code, start with “#”.)

Boolean variables are True or False.

They are especially useful when comparing other variables. Here are some examples:

You can also combine these comparisons using logical operators like and and or:

Outputting to the screen

Nothing is actually shown on your screen when you run the above examples. The values are stored in memory. To “print” to your screen, use the print command:

Lists

Sometimes you want to store multiple values in one variables. You can create a list (called an “array” in most other languages), which is like a collection of other variables:

You can add things to the list using append:

Notice that you can append different kinds of variables (e.g., a string and a number) to a list.

You can also define a list’s members right when you create the list:

It’s also possible to combine lists:

There are other ways to manipulate lists. Here are some examples:

Dictionaries

Sometimes rather than just listing variables (values), you want to be able to associate them with a lookup values (keys). In Python, this is accomplished with a “dictionary” (usually called an “associative array” in other languages). For example:

You can also define the dictionary when you create it:

Loops

For loops

Sometimes you want to consider all the members of a list one by one. “For loops” are great for that:

The output of the above looks like this:

Why does “Done!” only appear once? How did Python know not to include print "Done!" in the “for loop”? It’s because of the indentation. print a_var is indented by four spaces under the for loop, so Python decided it should be placed in the loop. print "Done!" was not indented relative to the for loop, so it was “outside the loop.”

This code adds all the numbers from 0 to 4 and prints out the sum:

Lists of numbers are so common that Python has a special command to generate them: range. Range is a “definition” (called a “function” in other languages) that accepts three “parameters”: the number to start at, the number to end at (actually the number to end at plus one), and the difference between sequential numbers. This example should help:

So let’s rewrite our previous code block using range:

You can also use for loops to loop through dictionaries. In this case, it loops through the dictionary keys.

The above code block prints this to the screen:

While loops

While loops are also very useful. They continue to loop until some variable comparison evaluates to True.

The output of the above code block would be:

If you want to break out of the loop early, use break:

Beware of infinite loops. You’ll have to press control-C to break out of them:

Conditional statements

Often you only want to run a certain piece of code if certain conditions are met. Conditional if statements can be used for this purpose. Consider this code block:

The above code outputs the following:

Definitions (functions)

What if you want to always perform a series of commands together? You can group them into “definitions” (called “functions” in other languages). Let’s put our adding code into a definition:

Notice that instead of printing total at the end, I’m “returning” total. The return command means, “This definition is done, and here’s a variable with the answer I got.”

How does Python know which commands belong to the definition, and which commands are outside it? Again, it’s all about the indentation (four spaces per indent).

Here’s how you can use (or “call”) a definition:

So whenever I call add_numbers(), I get 10 back.

I can make functions more useful by adding “parameters.” Consider this code block:

Classes and Objects

Sometimes you want to group variables and definitions together in one “idea.” Imagine a scientist. She has certain characteristics (variables) and can perform certain actions (definitions). Let’s make a class to represent a scientist:

Notice that the first parameter in any definition contained within a class is always self. self refers to the class itself.

As I said, a class is just an idea. It’s like the outline of what a scientist could be. Let’s use this class to make actual scientists (at least in our computer). The actual scientists are “objects”, to use programming terminology.

Reading and writing files

As you see from the example above, a variable doesn’t have to be a number or a string. It can also be an object (i.e., a class made “real”). Files on the disk can also be objects. Just use the open command. It accepts two parameters: the filename, and the mode, where “w” means write to disk, and “r” means read from disk.

Suppose we have a file on the disk named “myinput.txt” that has the following contents:

Let’s open “myinput.txt” for reading (i.e., in “r” mode).

read will read the entire contents of “myinput.txt” as a single string.

text is a single string, so the output looks like this:

(We’ve basically recreated the unix cat command using Python.)

Let’s rewrite this code using readlines, which will create a list of the file lines.

Notice “\n” at the end of each item. That represents a newline character (when you press enter or return on your keyboard).

Let’s expand our program now, adding “is awesome” to each file line.

This little program prints the following to the screen:

What if in addition to printing to the screen, we also saved the output to a file called “myoutput.txt”? Use the open command in the “w” mode (for “write” to disk).

The above prints the same thing to the screen as before, but it also created a file called myoutput.txt that contains the following text:

Manipulating strings.

There are many python definitions for manipulating strings. Since string manipulation is a common task, thought I’d give a few quick examples:

Conclusion

Python is a very powerful language, and we’ve barely scratched the surface of what it can do. There are many tutorials that cover more advanced topics on the web. I hope this gives you a good start. Best of luck!