[pythonvis] Re: starting out in Python

  • From: "Jeffrey Thompson" <jthomp@xxxxxxxxxxx>
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Fri, 7 Jul 2017 18:47:51 -0400

Richard, 

 

This is very good and impressive.  I have a few suggestions on wording and a
question on something you discussed.

I will email to you and Jdog.  But I'm very impressed with your work
already.

 

                Jet

 

From: pythonvis-bounce@xxxxxxxxxxxxx [mailto:pythonvis-bounce@xxxxxxxxxxxxx]
On Behalf Of Richard Dinger
Sent: Friday, July 07, 2017 1:52 AM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] starting out in Python

 

Below is my first attempt at a new kind of 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 programs
to illistrate the discussion.  You will soon see that writing a program is
actually quite simple.

 

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 imperitive 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 algorithm and take a quick look at the Python language
tools needed for that solution.  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 newcommers 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 ffirst being developed, writing the program consisted of
entering a very long series of numerical codes into the computer.  And so
the practise of writing programs became known as coding.

 

The programming Process

 

The first thing newcommers 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 philosipy 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 frrequently:  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 calender -- all these activities run programs that
someone wrote.  These example programs are larger and more complex than the
sort of program we will discuss here, 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 modifyed 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, doing
something with data,that is data processing is all about processing that
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 things.  For instance you could
have three items on your kitchen table: a book, a coffee cup and a spoon.
All three can also be referred to as objects.  In Python, each item of data
like a number, a string of characters or an image are all referred to as
objects and all objects have some specific properties in common.

 

Each Python object holds information about the item of data it represents
including the following items:

- a unique identification

- a type

- a vvalue

 

Informally, an object is just a chunk of computer memory where the object's
properties and the value are storreds.  The location or address of that
chunk of memory is used for the unique id of the object.  The type controls
what operations are legal, for example 23+"Hello" makes no sense.  once
created, the object's id and type do not change.  You can even design your
own object types  for a program.  New object types are created by extending
an already existing objecct type.

 

Built-In Objects

 

Objects are a really big deal in Python and will be coverred 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 errors.

 

Python is called a dynamically typed language because it knows the types of
the objects, so programers  don't have to declare them in advance.

 

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 eachend of the stringfor 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 address.  The
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 expression and reduces it down to
a resulting object.  The name is then bound to that result object.

 

For example 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
mathimatical 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.  Object                    names that appear on the
right side must be defined in a prior statement somewhere in the code.

 

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.

# 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 seperated 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.

 

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 vvalues 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 reccomendation 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.

 

Other related posts: