The print instruction
Open your Python system. Create a new window. Put the single line:
print "start"
in it, and save as the file variables.py
If you want to run the following examples, type them into your file, at the end of what is there already.
We can display calculations, as in:
print 3*8
print 2 + 5.4
which displays 24 and 10.8
Arithmetic operators
** | power of |
* | multiply |
/ | divide |
% | modulo |
+ | add |
- | subtract |
Multiply, modulo and divide are done before add and subtract. Power is done first of all.
Examples:
print 2 + 3 ** 2
print 2+3*2
print 11.3 / 2
print 65 / 60
print 65 % 60
prints
11 8 5.65 1 5
Note that / on whole numbers (integers) gives an integer answer, truncated.
The % operator gives the remainder, imagining that the items were to be divided.
Thus, 65 divided by 60 gives 1, with remainder 5. If we have 65 seconds, we can use / and % to break
it up into minutes and seconds.
We can use round brackets to enclose calculations which are to be done first, as in:
print 3+2*4
print 3*2+4
print 3*(2+4)
which displays: 8 10 24
Warning:
print 16/2*2
displays 16, but
print 16/(2*2)
displays 4. The ( ) cause the * to be done first.
We can also use a comma in print, as in:
print 3*2, 4+5
where 6 and 9 are displayed on the same line, separated by a space.
We can also print text in quotes - we can use single quotes, or double quotes. Examples:
print "hello"
print "The answer is ", 3*4, " inches"
which displays:
The answer is 12 inches
Note the comma usage: it causes printing to stay on the same line, as in:
print "hello",
print "world"
which displays
hello world
By the same logic, print by itself causes a new line to be started:
print
print
print
creates a 3-line gap.
In the above , note that when we have a series of instructions, they are performed from
top to bottom, down the page, in sequence.
Variables
Previously, we used numbers and strings (i.e. text in quotes).
A variable can be thought of as a named box with a value (e.g. a number, or some text) stored in it.
One way to put a value in a variable is by assignment, using =
We can put e.g
width = 10
height = 20
Pictorially, 2 storage boxes:
_ _
| |
width
+-------+
| 10 |
+-------+
height
+-------+
| 20 |
+-------+
|_ _|
We choose the names (which cannot include spaces, punctuation (such as comma, quote...),
and must start with a letter.
The = operates right to left: the value on the right is calculated (evaluated) and then
copied into (stored in) the box we mention on the left of the =.
More examples:
n = 23+3.3
m = n+1
x = 1
x = x+1
print n,m,x, x+1
which prints 26.3 27.3 2 3
Note:
-
A variable keeps its value until we alter it (e.g by another assignment)
-
the right-hand-side of = can include variables. Their current value is used
-
= does not mean 'equals' - it means 'becomes'. Thus x=x+1 takes the current value of x,
adds 1 to it, then stores the result back in x. Its effect is to add 1 to x.
-
print can display the value of variables. It cannot change the value of variables. Thus, we printed
x+1, giving 3. But the x still holds 2.
-
we can put extra spaces round = + - * etc if we wish, to improve readability. There are a range of opinions
on whether this is worth doing.
Variables Example - area of rectangle
Calculate the area of a rectangle which is 2.3 by 6 metres. We could just put:
print "area is", 3.3 *6 , "sq metres"
but we will use variables, and enhance it later. Solution:
# area program
width = 6
height = 2.3
area = width * height
print "Area is" , area, "sq metres"
The output is:
Area is 13.8 sq metres
Note that area is not in quotes, so its value (not its name ) is displayed
# comments
Just as you might make jottings on your lecture notes, so you can annotate programs with #.
Any text after # is ignored, but stays in the program. Do not over-use comments. For
example, you might comment sections of programs, or difficult bits.
problem: write a similar program to calculate the volume of a room
Strings
Variables can also hold strings, as in:
myName = "Mike"
lastName = "Parr"
fullName = myName + " " + lastName # holds Mike Parr
We can use + to join strings end-to-end.
Strings can be quoted with "..." or '...', but the start and finish quotes must match. For
example:
print "Mike's room" # prints: Mike's room
More operators: += *=
Python has some 'augmented' assignment operators:
x += 4 #means x = x + 4
y *= 5 #means y = y * 5
Constants
Sometimes, programs make widespread use of 'magic numbers. Perhaps a tax rate is 15.3%, and we
have lots of lines such as:
pay=pay*15.3
It is clearer to use a name for the number. Such items are called constants - they do not change throughout
the run of a program, but we may need to edit them once a year. A convention is to use capitals, as in:
TAX_RATE = 15.3 # put at top of code
#.. lots of code...
pay=pay*TAX_RATE
| |