Python  part 3


We'll move beyond the Hello, World miniprogram here.   Perhaps the single
most crucial concept in programming is that of what is called a variable.
Think back many years when you started learning abstract math in school.
The teacher showed you expressions such as  3X + 2Y -5Z (or 3*X  + 2*Y etc).
You had to work with equations such as  3X - 2Y = 15.  This is different from
saying 3*7 - 2*3 = 15.  The X and Y are what the teacher called variables,
meaning that the values could change, whereas in 3*7 -2*3 = 15 everything
had a fixed value--there were no variables.  Programming languages have the
same kind of concepts.  In a programming language, a variable is something
whose value can change. (In many programming languages there are things
usually called constants which look like variables, but whose value should
not change--for example, PI whose value is 3.14159.  Having such a special
"variable" whose value does not in fact change can be very useful.)

In many programming languages, such as C and FORTRAN, you need to
"declare" variables before using them--e.g. int  age, year--which tells the
compiler that the variables age and year hold only integer values--no
fractions, no decimal points, etc.  If you said int PI and tried to put the
value 3.14159 in PI, that doesn't work!  Declaring variables makes the job
of the compiler easier, but as we'll see not having to declare variables can
make the programmer's job easier.  Many languages, such as Python and
Perl do not have such variable declarations.  When you have to declare
variables they only can hold one type of value--such as an integer or
something with a decimal point (e.g. 3.14159) or a string of characters.
Python has variables, but they can hold an integer value in one part of
your program and a character string in another part. 

Python has rules about how to give names to variables:  variable names
can contain only letters, numbers, and underscores, and they cannot
start with a number.  They CANNOT contain spaces!
       good                                 bad
       -------                          ---------
       alpha17                            33beta
       number                             num ber
       good_value                       good value
etc.  The Python book that we may use suggests that if you want to have
a constant--a variable whose value should not change--then use all caps--
PI = 3.14159     VOWELS = "aeiou"  etc.  Otherwise you can mix caps and
non-caps.  You should have "good" variable names--names which convey
information to the reader.  Consider the variables "age" and "a"--the
latter has no inherent meaning, unlike the former.  Good variable names
make your code easier to read and understand--and they help avoid making
programming errors.  Variables such as MyCanvas  or FirstValue are fine, as
are First_Value, or Start_of_the_Sequence.  Variables are stored in memory in
RAM--they can be read from and written to--their values can change.  In
Python, variables are case-sensitive, meaning Alpha and alpha are considered
to be entirely different variables.

ASSIGNMENT.  Many languages, including Python, use the equals sign (=)
to mean assignment.  This has a different meaning from  3X -2Y = 15.
In 3X -2Y = 15, the equals sign meant that the two sides of the equation were
equal.  In most programming languages, including Python, one can have
   alpha = 3 * beta - delta.  This tells the compiler/interpreter that you want
to multiply beta by 3 (or, if you wish, multiply 3 by beta), subtract delta from
that result, and then store the result in the variable alpha.  That result gets
assigned to alpha--it does NOT mean equality.  It is NOT legal to have in
Python  3 * beta - delta = alpha.  Python has <variable> = <expression>
with a variable on the left of the equals, and expression on the right.  The
expression can be very simple:  alpha = 0   is perfectly OK.  The expression
can be complex:
    alpha = 3 * (beta + delta -5) - 8 * ((rho - omega) * (chi - mu * phi))
which is also perfectly OK.

When a variable appears on the left in an expression--alpha = 203 then
its value is going to change.  Remember that each variable gets storage
in memory.  alpha = 203 means that a write to memory to the storage that
holds alpha is going to occur.  Now suppose we have the following lines in
a program:
                         alpha = 3 * beta - delta
                         alpha = 203
                         alpha = 0
Legal?  Yes.  The value of alpha gets changed 3 times in a row.  Sensible?
No.  Why bother having alpha = 3 * beta - delta   if you're promptly
going to change alpha again with the next line of code.

SOME MATH OPERATIONS.   alpha + beta  --adds, as you would
expect.  alpha - beta  --ditto.  alpha * beta --multiplication. 
alpha / beta  --alpha divided by beta.  A warning here--what if beta has
the value zero?  Trying to divide by 0 does not make the computer happy.
alpha ** beta  --exponentiation--alpha raised to the power beta.