We won't write a program in this lab, but we'll practice running Python, and get a feel for the language by writing some Python expressions. These instructions are for a default installation of Python on Microsoft Windows. Python runs on many platforms, and the procedures will be only a little different on others.
Open Idle from the Windows start menu.
Idle is a program for making and running Python programs.
You will see the Python Shell window where you can type
Python expressions. Python evaluates them, and prints the result.
Now, type some numeric expressions in the Idle window. After each one,
press enter, and Python will show the result. (The >>>
is Python's prompt; it lets you know Python is ready for you to type.)
For instance,
Python arithmetic expressions generally do what you expect.
Use +
for addition, -
for subtraction, *
for multiplication
and /
for division. You can also use **
for exponentiation;
that is, you can compute 56 by writing 5 ** 6
.
Python evaluates multiplication and division before addition and
subtraction, just what your algebra teacher told you to do. You can also use
parentheses to change the order.
There is one unexpected pitfall: Do not type any spaces in front of your expressions. (We'll get to the reason for that in Lesson 3.)
Python, as many programming languages, makes a distinction between integer and floating point numbers. Floating point numbers are allowed to have fractional parts, and are always written with a decimal point, even if the fractional part is zero. Integers, of course, are whole numbers, and are written without a decimal point. Operations on floating point numbers, or on one of each, produce floats.
Why does any of this matter to programmers? In most cases it doesn't, except for the division operation. Addition, subtraction, and multiplication are tame because the sum, difference or product of two whole numbers is always a whole number. For division this is not so: one divided by two is a half, after all. When dividing integers, Python simply discards any fractional part to produce an integer result.
Integer and floating point numbers are represented differently inside the computer, and integer arithmetic is faster for the CPU to perform. Having the two forms allows programmers to choose faster integer arithmetic when it will do, or slower floating point when needed.
Understand what is happening in these two expressions.
Python calculation is not limited to numbers. It also supports strings, which are just sequences of characters. Strings must be enclosed in quotes. You can use either double quotes or single quotes, but you must end with whatever you start with.
Also notice that a string of characters is allowed to contain no characters at all. This is the empty string. It is much like an empty set in mathematics, or perhaps you might think of it as the string version of zero. Also note that it is different from the string containing a space.
The +
operator works on strings, but
it doesn't add them.
Notice how flexible the +
sign is. When you use one between
two integers, it does an integer addition. When you use floats, you
get a float addition, and when you use strings, you get concatenation.
Try some other string concatenations. Also,
see if you can figure out what the
*
means when applied to a string and an integer.
Does it make sense?
Python also supports operations to extract portions of a string.
When you select using a single number, you get a string containing the single character at that position. When you use two, the first chooses the starting character, and the second indicates the first character past your selection. Try a few, and try combining them with the other string operations.
The slice notation allows a simple shortcut. If you omit the first number, it defaults to zero, and if you omit the second, the slice goes to the end of the string.
Sometimes it's nice to know the length of a string.
You can find this with the len
function.
Len
is a an example of a built-in function. Functions are used as
you see here, name first, then a list of arguments, which are the
input values to whatever the function computes. This particular
function takes a single string as its input, and produces an integer
result.
"John"
and
"Marsha"
, then selects and prints the substring "nMa"
.
"transfiguration"
and prints as
'train'
. Your expression should contain no strings other than
"transfiguration"
, but you may need to type that in more than one
place.
'eggplant'
, your
expression produces '********'
.
So far, we have been using Python to evaluate and print expressions. Often, though, programmers like to retain an expression's value for later use. This is done with variables, which are simply names that can retain values. For instance:
=
sign to assign a value to a variable.
Put the variable name to the left, and any Python expression to the
right. The expression is evaluated, then given to the variable.
When you use
the variable name in subsequent expressions, it has the value assigned.
You can change the value of a variable by simply
assigning it again.
To know the value of a variable, simply type the variable name, and Python will print its value. When you do this, you are just typing a no-operator expression.
It is an error to use a variable name in some expression if you have not assigned it a value first.
Python variable names may contain letters, digits and the
underscore character, _
, but may not start with a digit,
and usually should not start with underscore.
Variable names may not contain spaces. Examples of legal
variable names are t
, Smith
, Bill_Smith
and Num_17
.
If you try to use Bill Smith
, 7up
or A$
as variable
names, Python will hiss at you.
Python variable names are case-sensitive. That means it matters
whether you use small or capital letters. The variables
tank
, Tank
, TANK
and TaNk
are four separate names,
unrelated as far as Python is concerned.
Variables are not limited to holding numbers.
Because Python assignments use the =
, they look a bit like
mathematical equations. They are not. An assignment directs
the computer to evaluate the expression on the right side,
then assign its value to the variable on the left.
This means that you can make assignments using the variable
assigned.
sally
is initially 17 (from
the first assignment). The second assignment
starts by computing the expression
sally + 2
to get 19, then assigns the 19 to
sally
.
This also points out that assignments are not
equations: x = x + 2 doesn't make much in algebra class.
x
, y
and
z
. Then write an expression which takes the average of
x
, y
and z
.
Change some of these values, then evaluate your expression again to
see the changed result.
s
some long string value.
Write an expression which returns a string consisting of the
first two and the last two characters in s
. For instance,
if s
is google
, your expression should return
the string gole
. Try giving various values to s
and trying your (same) expression again. What happens when your string
is shorter than four?
s
.
len
function
a variable that contains an integer value.
Now that we know how to get python to evaluate expressions, we're ready to write some Python programs. This will involve creating files of these expressions which we can run repeatedly. That will be the subject of Lesson 2.
*Computer scientists like to count things from zero because it simplifies certain calculations. For instance, In the present year 2005, it's the 21st century, which started with 2001; the year 2000 was the last of the 20th century. If we had started with year zero of the zeroth century, we'd be living in the year 2004 in the 20th century, which would have started with the year 2000. The century number and the year always agree, and the century and the first two digits of the year change at the same time.
Copyright 2005, 2006
Thomas W Bennet •
Image Credits
•
Terms of use:
Creative
Commons