1-6 Hello Python!

Installing the Python Software

An Integrated Development Environment (or IDE) is software that allows us to edit program code and then run (also called “execute”) the program to try it out.  There are many different IDEs out there depending on one’s needs and experience level.  For this course, we are going to download and install the Mu Editor.  Mu is a very nice Python IDE for beginners and includes the pygame library automatically (which we will need later on to make 2D games).  Also, the price is right — it’s free!

You can install the Mu IDE on Windows, Mac, or Linux.  What if you only have a Chromebook or tablet/phone?  These devices are fine for general Internet use but are poor devices for programming.  However, if you have absolutely no choice you can try a web-based option called Online Python.  This won’t be ideal, but it’s a workable solution to get started.

Once you get a little more advanced you might want to try installing the Python interpreter yourself and use a more industrial-strength IDE like Wing IDE 101, Microsoft Visual Studio, or PyCharm.  You can research these more on your own when the time is right.

Displaying Output

Here’s a really short game written in Python:

Done!  When you type this statement and click the Run button in Mu, the Python interpreter translates it into machine language and executes it.  You can see the output of the program at the bottom of the Mu window.

The print keyword must be in all lowercase, followed by round brackets that define what you’d like to display.  The quotation marks indicate the start and end of the message to be displayed.  In Python, a sequence of characters (letters, numbers, or symbols) enclosed in quotation marks is called a string.  You may enclose a string using either the single quote (‘) or double quote (“) but the opening quote must match the closing quote.  For example,

It may seem strange to have two different ways of doing the same thing, but the usefulness of this becomes clear when you want to display a single or double quote as part of the string itself.  For example, this won’t work:

because it’s not clear to the Python interpreter exactly where the string ends.  But mixing quotes solves the ambiguity:

Ah-ha!  But what if you want to display both double and single quotes as part of the message?  For that, Python supports a special kind of string called a triple-quoted string; which is either three single or three double quotes together.  Check this out:

or alternatively:

Another special feature of triple-quoted strings is that you can use them to represent multiline strings that will print exactly the way you type them:

By the way, if you’d simply like to print a blank line you can use the print() function with empty brackets () like so:

Remember ASCII?  Well, using characters to create (very) basic pictures is called “ASCII art”.  This art form has been around as long as typewriters (1898)!  Check out this site if you’re interested in other examples: http://www.chris.com/ascii

Defining Variables

All computer programming is about the manipulation of data.  Your games will store information about where the player is, what the enemies are doing, scorekeeping, and lots of other stuff.  A variable is a name that represents a value stored in the computer’s memory.  For example, a game that needs to keep track of a user’s score might use the variable name score to represent that value in memory.  When a variable represents a value in memory, we say the variable references the value.

To create a variable, one uses an assignment statement like this:

high_score = 950

Note that the variable name must be on the left side of the assignment operator (=) or you will get a syntax error.  After this statement is executed, a variable named high_score will be created and it will reference the value 950 in memory like this

Before you can use (i.e., display or calculate using) a variable you must assign an initial value to it.  There are also important syntax rules regarding naming variables:

    1. You cannot use a Python keyword; for example, False.
    2. The first character must be a letter (a-z or A-Z) or an underscore (_).
    3. After the first character, you may use letters (a-z or A-Z), digits (0-9), or underscores (_).
    4. You cannot include blank spaces or special symbols (like $, %, *, etc..).
    5. Python is case sensitive so High_Score is not the same as high_score.

Style Rule #1: Variable Naming

You must always choose descriptive variable names.  For example, it’s pretty clear what the variable high_score is intended to store.  Never use variable names like x or a1 since they give no clue as to what the variable’s purpose is.  Choosing a bad name is not a syntax error, but good style is still very important.  When you name things well in your program it becomes “self-documenting”, and can reduce the need for code comments.  For multiple word variable names, choose either pot_hole_case (preferred) or camelCase style and use it consistently.  These are described next.

Creating a good variable name often results in one made up of multiple words.  It is considered poor style to just run the words together (e.g., topstudentaveragemark) as it’s hard to read.  One way of dealing with this is to separate each word with an underscore (_) character; for example top_student_average_mark.  This style is called pot_hole_case and it’s the most popular among Python programmers.  Note that all letters are lowercase.

There is another popular style called camelCase.  The way it works is that you run the words together, but capitalize the first letter of each word after the first; for example, topStudentAverageMark.  This style is most popular among Java programmers.  For now, I recommend that you use pot_hole_case consistently.

Finally, we need to talk about what happens when you reassign a value to a variable.  Consider the following code:


First, the high_score variable is assigned the value 950.  It later gets assigned the value 1234.  The old value of 950 is still in memory, but it is no longer referenced by a variable since high_score now references 1234 instead.

When data in memory is not referenced by any variables, the Python interpreter will eventually free the memory so that it can be reused.

Python Data Types

Although everything is ultimately stored in binary, Python uses data types to categorize values in memory.  There are three basic data types:

    • Whole numbers such as -3, 0, and 123 are classified as int (integer) values.
    • Real numbers such as 3.14, 0.0, or -0.5 are classified as float values.
    • Text such as “Hello World!” are classified as str (string) values.

Even though Python is a dynamically typed language you should be aware of each variable’s data type.  Often it’s obvious which one you should use – for instance, if your game has the concept of lives, you would use an integer to store them because you are not likely to have half a life or 3.673 lives.  Floating-point values are used when greater precision is needed – for example, in a racing game your car may have a top speed of 220.403 km/h.  As we will see later, some operations behave differently depending on the type of data involved, and some operations can only be performed on values of a specific type.

You Try!

    1. Add an entry for “1-6 Hello Python!” in your Learning Journal and answer the following questions:
      1. So far we have seen three data types in Python.  List them, and give an example value that each might store.
      2. Aside from following syntax rules, what else makes a variable name effective?
      3. Which of the following variable names would give a syntax error (and why), and which would be considered poor style (and why)?
        • y
        • 2cool
        • thehighestscoreever
        • Student_Average
        • term&sumative