Programming Basics

We will start with the very basics of programming. Please ensure your virtual machine from the previous two chapters is up and running. Open the XTerm application. If none of this makes sense please revisit the first two chapters to get comfortable. One of Python's most useful tools is the Interactive Shell.

You can start your interactive shell by typing python in your XTerm application.

python
Python 2.7.8 (default, Oct 18 2014, 12:50:18)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> (also chevron prompt) is the interactive shell. Enter 21 + 21 at the prompt and let Python do some simple arithmetic.

>>> 21 + 21
42
>>>

Interactive Shell: interactive shell

In Python, 21 + 21 is an expression, which is the smallest and most basic kind of instruction in the language. Expressions will have values (in this case 21) and will have operators (in this case +) which will evaluate to a single value (in this case 42). Expressions are used anywhere in your code that you could use a value.

In our second example enter the value 42 with no operators. This is also an expression which just evaluates to itself.

>>> 42
42

We have seen the use of the addition operator above. Python uses others that we will demonstrate below.

>>> 2 + 8 * 5
42
>>> (2 + 8) * 5
50
>>> 234567 * 456789
107147625363
>>> 2 ** 4
16
>>> 42 / 5
8
>>> 42.0 / 5
8.4
>>> 42.0 // 5
8.0
>>> 42 % 5
2
>>> (8 - 2) * ((3 + 4.0) / (5 - 2))
14.0

All the examples we just entered expressions that Python evaluates to a single value. Python uses the same order of operations as mathematics. The ** operation is evaluated first; the *,/,//, and % operators are evaluated next, from left to right; and the + and - are evaluated last (also from left to right). Parentheses are used to override the usual precedence if you need to.

The rules for putting operators and values together to form expressions are a key part of Python programming language. The ability to enter these values and operators and values in the interactive shell allows one to test validity of the Python language syntax as we will demonstrate below.

>>> 41 +
  File "<stdin>", line 1
    41 +
       ^
SyntaxError: invalid syntax
>>>

Python here is telling in "almost English" that there we used invalid Syntax. Part of learning how to program is learning how to get comfortable with the numerous errors that you will encounter. In the example above, adding an integer after the + operator or eliminating the operator will rid you the error.

Integer, Floating-Point and String Data Types

A data type is a category is a category for values, and every value matches to exactly one data type. In the last section we demonstrated how Python expressions evaluate to one value. This value goes into tha various data type that Python has. The three most common ones we will encounter are Integers, Floating-point numbers (sometimes floats) and Strings. The values 42 and -21 for example are integer values. The integer (sometimes int) data type indicates values that are whole numbers. Numbers that have a decimal point such as 3.14 or 2.718 are called floating-point numbers. In our examples earlier we used 42 and 42.0 the former is an integer while the latter is a floating-point number and the differing results demonstrated that. We will address these as we delve further. Finally the last commonly used data type is Strings. Python programs that contain text values are referred to as strings. The text characters are surrounded either with double or single quotes. It is only important to remain consistent. The author of this book has used single quotes significantly and you will notice that here. This is a matter of taste. These are some examples of strings 'Hello' and 'Goodbye Cruel World'.

As you get used to using programming one of the syntax errors you will encounter quite a bit early on is the SyntaxError: EOL error demonstrated below.

>>> 'Goodbye Cruel World!
  File "<stdin>", line 1
    'Goodbye Cruel World!
                        ^

SyntaxError: EOL while scanning string literal
>>>

Once again Python marks the exact spot where it failed to understand what we mean. Learning how to interpret these messages will suit you as you start out.

String Concatenation and Replication

Python operators meaning vary based on the data types values next to it. As we will see below the + is used for addition when next to integers or floating-points values. Start your Python shell and do the following.

>>> 'Monty' + 'Python'
'MontyPython'

In the example above Python joins the strings together. This is known as string concatenation. The expression evaluates down to a new string value that combines the text of the two strings. When we try to add a string and an integer we will get a TypeError as seen below.

>>> 'Monty' + 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

Once again pay attention to the error message at the very bottom which in almost English tells you what Python interprets what we typed in. In this case Python is telling us it cannot concatenate an integer to the string 'Monty'. Python can convert data types as we will see shortly but as the example above demonstrates it does not do this automatically. Earlier we say the * operator is used in multiplying two or more integers or floating-point values. In the example below we will see what it does with strings.

'Spam' * 5
'SpamSpamSpamSpamSpam'

Here we see the expression evaluates to a single string value that repeats the original value to a number equal to the integer value.

'Monty' * 'Python'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
'Spam' + 2.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'float' objects

In the examples above we learn that the * will not multiply strings together and will not concatenate strings with floats.

Storing Values in Variables

Variable in progamming are best thought of as a location in the computer's memory that stores a value.

In Python they take the format of

variable = expression

How does a variable name get stuck to an object? Generally, the assignment statement does this labeling. First, we need to talk about what assignment means, then we’ll look at how to write it.

  1. An assignment statement evaluates the expression.
  2. An assignment statement then assigns the result to a variable. In effect, it pins the variable name onto the result. There are two variations on this.
    • If the variable name already existed, the name tag is moved from the old value to this new value.
    • If the variable name did not already exist, it is created and pinned on this new value.

We generally don’t worry about creating new variables; there’s no cost, they’re just names. Create as many as you need to make your program’s purpose clear.

Let's create a few variables in the interactive shell:

>>> ham = 40
>>> ham
40
>>> jam = 2
>>> spam = ham + jam
>>> spam
42
>>> spamalot = spam + ham + jam
>>> spamalot
84
>>> spamalot = spamalot + 2
>>> spamalot
86

What happened here is we initialized (really created) a variable ham and the value 40 was stored in it. We then can use this variable in expressions as we did. Take special note that Python forgets the old value when you assign a new one to it. The variable spamalot in the last exercise illustrates this.

Variable Names

Python provides the following three rules for variable names

  1. Can only be one word
  2. Can only use letters, numbers and the underscore _ character.
  3. Cannot begin with a number.

A good variable name describes the data it contains. It allows the person reading the code (oftentimes it is you) to understand what your program is doing. Thus far our variables have been not so clever references to Monty Python and Hitchhiker's Guide to the Universe. This is discouraged as it is often less funny than you think.

Hello World!

Until now we have explored Python using the interactive shell. All these examples cease to exist when we exit the shell. The interactive shell excels in experimenting with python but all our programs will have to be written using a Text Editor. Our Virtual Machine already has an editor in Leadpad. This file editor is similar to Notepad or Textmate. From the Bottom left menu select Accessories > Leafpad

Leafpad Launch: Leafpad Launch

From your XTerm application create a new directory with the name pythonfiles where we will save our work using the following commands.

cd ~
mkdir pythonfiles

Type the following into your text editor and save the file as helloworld.py

#!/usr/bin/env python
# This is our first program

print('hello world!')
print('What is your name?') 
user_name_entered = raw_input()
print('That is a lovely name,' + user_name_entered)

Hello World Save As: helloworld save

After you save your file. Switch to the XTerm application and type the following commands to run your program.

cd ~
python pythonfiles/helloworld.py
hello world!
What is your name?
...

The three ellipses at the bottom will not show up and are there because results will vary based on what you will enter. Congratulations! You've just saved your first Python (admittedly for this class) program. We did however cheat and it is worth revisiting what the program actually did.

Program Breakdown

If you have strictly followed the instructions thus far let's walk through each of the lines. Open the helloworld.py program or use the image above as we walk through each line.

  • Line 1: #!/usr/bin/env python

In Unix-like operating systems a character sequence consisting of the number sign and an exclamation mark (#!) (also known as a shebang) is at the top of a script the program loader parses the rest of the line as an interpreter directive. Given the right set of permissions the example above can be run ./helloworld.py - Line 2: # This is our first program

This is called a comment. Unlike the number sign in Line 1, in this case the number sign means ignore everything after. Comments are extremely useful notes to the person reading the program (often you) at a future date. Sometimes the number sign can be placed in front of actual code -known as commenting out code- This can be useful when trying to test what actually breaks your program.

  • Line 3:

This has blank or white space. It is in there to add legibility. Python will ignore this white space but it makes it easier for one to read your (again often you at a future date) programs when you use proper spacing.

  • Line 4: print('hello world!')

The print() function displays the string value inside the parentheses on the screen. In our program we can python is calling the print function and the string value of 'hello world!' is passed to the print function. A value passed to a function is called an argument

  • Line 5: print('What is your name')

In Python there isn't a difference between lines 4 and 5 except for arguments passed to the print function.

  • Line 6: user_name_entered = raw_input()

This raw_input function call evaluates to a string whatever text the user enters and strips of the new line from the input. In our example it assigns this to the user_name_entered variable. If we entered the name 'Monty', then the expression would evaluate to user_name_entered = 'Monty'.

  • Line 7: print('That is a lovely name,' + user_name_entered)

Once again we are using the print function. We pass the expression 'That is a lovely name' and the contents of the variable user_name_entered. Remember expressions evaluate to one single value so in this case if we used the name 'Monty' we will be passing the argument 'That is a lovely name Monty' to the print function.

This concludes our simple program evaluation.