Below is my revision 1 attempt at a first lesson in Python programming. Please
look this material over and let me know if anything is confusing or not
explained. The first few paragraphs explain the approach that I am trying to
use.
First Step: What is a Program
Everyone should know how to write a simple computer program. Since you are
reading this article, perhaps you agree and are interested in learning.
Programming is largely a problem solving skill and sharpening your problem
solving skills never hurts. In this first article we will explain some
terminology, introduce some program components and write one small program to
illustrate the discussion. You will soon see that writing a program is actually
quite simple and fun too!
What is Python
For those interested in such details, Python is named after Monty Python and
has nothing to do with constrictor snakes. Python is a general purpose
programming language that is simple and easy to learn. Python like most
programming languages is an imperative language that means you write commands
called statements that tell the computer what to do. There are around 40
statements available.
The Learning Approach
In the first few articles, we will start out by posing a problem to solve with
a computer program to be written in Python. Then we will sketch out a problem
solution method and take a quick look at the Python language tools needed for
that solution. In programming, a solution method is often called an algorithm.
The program will then be written and tested. The idea is to focus on the
problem to solve and learn enough Python in each article to build the working
program.
These articles will be geared for newcomers to programming so the approach
taken in the demonstration programs may not be the "best way" to do something,
but they were selected to demonstrate a point to new programmers. So while I
encourage correction of any errors I make, please limit discussions of those
other ways to solve the problem.
Why is programming called Coding
When computers were first being developed, writing the program consisted of
entering a very long series of numerical codes into the computer. And so the
practice of writing programs became known as coding.
The programming Process
The first thing newcomers should know is that programming is often a trial and
error process. The following is the way most programs are written:
1 code
2 try
3 repeat
This is the approach for developing a program or even part of a program. First
code what you think is a correct program, try it by running python and repeat
until it works. A good philosophy is first make it work, then make it work
correctly and then, if needed, make it work fast.
Use a text editor not MS Word! A text editor program will not insert hidden
formatting text in your source code files (called modules), unlike Word or
WordPad. There are many free text editors to choose from such as MS NotePad,
NotePad++ and EdSharp. I use TextPad, which is shareware and costs around $30.
The Problem
This first problem is simple, but a realistic type of problem that programmers
run into frequently: You have some data (in this case numbers) and you want to
do some processing using each data item (in this case find their sum and
average value).
Solution Approach
The program will simply go through the list of numbers adding up their sum and
counting how many entries there are in the list Then the sum and average will
be calculated and printed.
What is a Program
So what is a program? We all think we know what a program is, we use programs
every day; we check our mail, write a shopping list or make an appointment in
our calendar -- all these activities run programs that someone wrote. These
example programs are larger and more complex than the sort of program we will
discuss herein this article, but all Python programs, large or small, have the
following structure
- A Python program is made up of one or more source code files called modules.
If there are multiple modules, the first module to run is called the main
module.
- Modules are made up of Python statements that tell the computer what to do.
- Python program data called objects are created and modified by statements
Statements perform most of the actual processing work and are normally executed
in order from top to bottom, unless that order is changed by a control
statement.
Statements generally concern themselves with one of four computer activities:
- input of data (open/read files, read keyboard, etc.)
- moving data into or out of memory (put/get data to/from a data structure,
etc.)
- processing data (manipulate data)
- outputting of data (write/close files, print to screen, etc.)
Processing and Data
'In an informal sense, in Python, we do things with stuff. "Things" take the
form of operations like addition and concatenation, and "stuff" refers to the
objects upon which we perform those operations.' from "Learning Python" by Mark
Lutz. That is, in essence, what programming is all about: A program running in
the computer does things with the data and the written program tells it how to
do it. Programming is all about processing data. So lets talk for a few minutes
about these two areas: processing and data starting with data.
What are Objects
All Python data are called objects. First, the term object is a collective term
that can be USED TO REFER TO different types of things. For instance you could
have three items on your kitchen table: a book, a coffee cup and a spoon. Each
of the three types of item can be referred to by its specific type (book, cup
or spoon)and it can also be referred to in general as an object. Lets take a
quick look at how an object is organized.
Informally, an object is just a chunk of computer memory where the object's
properties and the value are stored. Each Python object holds information about
the data it represents including the following items about the specific object:
- a unique identification of the object
- the type of the object
- the value of the object
The unique ID is simple, Python uses the memory address of the object itself as
the ID. All Python objects have a type like "str", which is a string of
characters. The type controls what sort of operations a string is permitted to
participate in. For example trying to divide one string by another like:
"Hello"/"green"
would be disallowed because string division is undefined.
The value of the specific object is stored in the object in a manner dependent
on that type of object. A string (type str), for example, stores the individual
characters as numerical values in a sequence of bytes. Since a byte can hold a
value from 0 to 255, each byte can hold one character.
once created, the id and type of the object do not change. You can even design
your own object types for a program. New object types are created by extending
an already existing object type.
Built-In Objects
Objects are a really big deal in Python and will be covered in detail as we
progress. Note that all data comes encapsulated in objects. For now we will
restrict our examples to three types of built-in objects:
- int the type name for integer values (no decimal point)
- float the type name for numbers with a decimal point
- str the type name for a string of characters
There are two kinds of numbers because there are two ways of representing and
manipulating numbers in a computer. The int type when an exact value is known
like counting something and the float type is used for numbers that are
calculated and are likely to have fractional parts, they are called floats
because the location of their decimal point appears to float around.
Strong Typing and Dynamic Typing
Python is called a strongly typed language because it controls how objects of
different types may interact. Thus nonsense operations like multiplying your
phone number by your last name are considered undefined and are not attempted.
Python is called a dynamically typed language because it knows the types of the
objects, so programmers don't have to declare them in advance. Many programming
languages require that the type of an item be declared prior to usage.
Literal Values
Data objects may appear in an expression by literally typing their value such
as 5 or 3.7 or "Hi", which is called a literal value. Note when a string
appears as a literal value it is delimited with either single or double quote
symbols. You may use either, but the same symbol must appear at each end of the
string for example: 'hello' and "bye"
Alternatively, you may use the assignment statement to assign a name to an
object and refer to the object by that name.
The Assignment Statement
Objects often are easier to use and keep track of if they have names. You can
associate a name with an object using an assignment statement. The idea behind
the assignment statement is to bind a name to an object's unique ID, which you
will recall is the object's memory address. The name bound to the object
address is sometimes called a variable, but may also be called a pointer or is
said to point to the object. The assignment statement form is:
name = expression
The right side of the equal can be any legal expression from a simple literal
value to a complex equation. Python uses the following arithmetic symbols in
expressions: + add, - subtract, * multiply, / divide, ** exponent and () for
grouping. Python evaluates the right hand side expression and reduces it down
to a resulting object. The name is then bound to that resulting object's unique
ID.
For example in the assignment:
x = 5
Python will evaluate the right side and look for the 5 result object and if it
does not yet exist, it will be created. Then the name will be bound to the
object so x points to 5. Numbers (and strings) are immutable object types that
is they cannot be changed. The number 5 cannot be changed to some other number,
5 is always 5, the name x can be changed to point to some other number,
however.
If the next statement assigns y to 5, you do not have two 5 objects, you have
two names for the single 5 object. If the right side is a complex mathematical
expression, the same process is followed, the objects are manipulated according
to their type and the operators in the expression to form a result object.
Names that appear on the right side must be bound in a prior statement
somewhere in the code in order to have a value.
So the first way an object is used is as an operand in a statement or in an
expression. In these cases the value associated with the object is used to
execute the statement being evaluated. In the next article, we will look at how
to access other specific object data called attributes.
Using Comments in Your Code
The idea behind comments is to provide a way of leaving instructions on how the
software works. Everything from a number sign on is ignored by Python.
# this is an entire line comment
a = 24 # this is an in-line comment
In two months, you will likely not remember why you did something a certain way
and in two years you may not remember the project! A few short comments
sprinkled through the code may help. Try to document why you did something a
certain way. Try not to explain what is obvious.
Printing Screen Output
before we get too deep into other things, we will take a look at how to print
any results to the screen. When Python changed from version 2.x to version 3.x,
printing changed from a language statement to a function. What does that mean?
Well, for a start, I need to explain the difference between the two.
The statement version of print consists of the word print followed by a comma
separated list of expressions to be printed. For the function version, the list
of expressions is enclosed in parentheses.
So, the statement form and the function form of a simple print example look
like this:
print "hello" # statement form
print("hello") # function form
Note that Python version 2 will take either, but version 3 requires the
function form of print.
The idea behind the print statement is to easily print default formatted values
on the screen. The function form is the word print followed by a comma
separated list of objects enclosed in parentheses. The object values are
printed separated by a space.
These are the default behaviors, other formats will be discussed in another
article.
Examples:
print(5) # prints 5
print(1, 2) # prints 1 2
print(2.5) # prints 2.5
print('hello') # prints hello
Our Simple Program
Now lets turn to our problem: recall that we have a list of numbers of an
unknown length and we want to do some sort of processing on these numbers.
Individual names work for some situations, but for a group of objects you may
need a container object called a data structure to hold the objects while they
are processed. Python comes with several different data structures, but a
simple list should work well for this situation.
The List Container Object
The idea behind a list is a container object that holds a sequence of objects
in memory. The items on the list can be objects of any valid type including
another list. The literal form for a list is acomma separated sequence of data
objects delimited by square brackets for example:
[1, 5, 3, 9, 14]
is a literal list of five numbers. The same result can be achieved using the
list statement as follows:
list(1, 5, 3, 9, 14)
Since a list is an object, you can bind a name to the list with the assignment
statement that we just looked at, for example:
values = [1, 5, 3, 9, 14]
The name values now points to that list. A list is a mutable object, that is it
can be changed during program execution, unlike a number.
Individual items on the list can be changed through their offset in the list.
The first item on a list has a 0 offset, the second has an offset of 1 and so
forth. Here are some simple accesses to the values list:
x = values[0] # x points to 1
values[1] = 42 # the second item now points to 42
Now we need a way to read through the list one item at a time and add up all
the values. That is what the for statement is designed to do.
The For Loop Control Statement
Control statements change the default sequential execution of statements. The
idea behind a for loop is to take each item in the list one at a time and apply
it in an indented code block. The form of a for loop is:
for item in container:
do_something_with_item
This process is called iteration and a for loop will iterate through any
iterable object. Iterable objects include containers like strings, lists,
tuples and sets.
Lets walk through a really simple example that prints each item on a list:
values = [2, 5, 3] # some test values
for val in values: # bind val to the current object being processed
print 'process', val # use current object being processed
This example does the following actions:
1 for statement bind val to first item in values list
2 indented block is run printing val
3 block is done jump back to for statement
4 for statement bind val to next item in values list
5 indented block is run printing val
6 block is done jump back to for statement
7 for statement bind val to next item in values list
8 indented block is run printing val
9 block is done jump back to for statement
10 for statement is done jump forward to code after indented block
That example should print the following to the screen:
process 2
process 5
process 3
The indented code block must be indented as that is how Python knows where the
block starts and ends. Indentation should be consistent throughout your code.
The recommendation is four spaces (no tabs). The code block can contain another
for loop, in which case the indented code block for that second for loop would
be indented two levels or eight spaces. The indented code block ends when a
line appears with less indentation than the current block.
Our Average Problem
So our plan is simple: add up the total and divide by the count:
# average.py
# initialize program variables
count = 0
total = 0.0
values = [1., 5., 3., 9., 14.]
# process data
for item in values:
count = count + 1
total = total + item
# results
average = total / count
print "The average value is:", average
print "The sum is", total
# end program
Notes and Further Reading
Here are some things to look into.
1 The construct:
name = name + value
is so common in programming that the following short hand version called
augmented assignment can be used.
name += value
2 The standard type names like str, int and float can be used like functions to
create an object of a specified type, for example:
value = float(2) # ensures value is a float
3 This particular problem could be simplified by using the two built-in
functions len and sum. Look them up in the standard library documentation and
revise the program.