Python as a Calculator

To use the Python console as a calculator, Open IDLE
StartIDLE_001

In the Python console type the following:

13 + 5
39 - 13
5 * 3
12 / 2
10 % 3

In the Python Console will see the following
cal_000a
As can be seen python preforms the following operations resulting a number (for the  expressions above):

  • +‘ performs addition
  • ‘ performs subtraction
  • *‘ performs multiplication
  • /‘ performs division
  • %‘ performs a modulo operation, returns the remainder after a division

The result of the the division operation result appears different, it has a decimal point
cal_001a
This highlights that Python has two types of numbers,

  • Integers (int), whole numbers sometimes called counting numbers i.e. numbers without fractions
  • Floats (float), any number with a decimal point and are used when when more precision is need. As dollars and euros have cents, meters have centimeters, grams have milligrams a lot of real world measurement units can be divided.

Expressions are not limited to just one operator and can have many, but need to be careful of operator precedence ( i.e. which operators are evaluated first). For example in the following code is the result of the expression on line 1 equal to 16 or 13 and on line 2  equal to 1 or 10

1 + 3*4
12 - 8/4

The result of the expression on line 1 is equal to 13 and on line 2 equal to 10 as  the multiplication and division have a higher precedence than addition and subtraction. For the python operators see here, highest precedence operators are listed first below

  1. *‘, ‘/‘, ‘%‘ (multiplication, division and modulo)
  2. +‘, ‘‘ (addition and subtraction)

Variables

Variables are used by nearly all programs and  give programs a great deal of flexibility. Variables can be used to refer to anything a program needs,  to display,  change or use. The first usage explored here is to use variables is to refer to numbers.

In Python we can  think of variables as labels that refer to a box( or container) that contains a value such as the score in a game, the result of calculation, an answer to a question or even portion of a program that can perform a calculation. Where there are different types of boxes (or containers) such as an integer and float to hold different types of numbers.

Below is an example of a variable that refers to a number used to store the score of a game. In this example the assignment operator(‘=‘ ) is used to label an integer that contains the value:

score = 400294

cal_007

Below is an program  that uses variables to update the score and health of a character in a game. The program also uses a variable to get a value that does not change, i.e. a constant used to add a bonus

In a python console type the following:

BONUS = 10
score = 0
# character fell while climbing a tree
health = 100 - 6
print(health)
# character reached top tree
score = score + 24
# found golden coin in the tree
score = score + BONUS
print(score)
# fell again
health = health - 6
print(health)

Note: The text after ‘#’ is ignored by Python and are comments typically used to explain the meaning of code, also in the code the subtraction of 6 occurs twice where we should use a variable with a meaningful name in these expressions, e.g.

 FALL_PENALTY = 6

The advantage of using a variable is that if we want to change the variable we only need to change it in one place plus it makes the code easier to read,

 health = health - FALL_PENALTY

indicates why health has decreased.

 

cal_003

Up until now we have using the python console which can be slow,

  • Open File, on the IDLE menu press File, then press New File from the drop down menu and a new window will appear
    cal_004
  • Copy or type the code into the new window(Can also download from here)
    cal_005
  • Save file, On the keyboard press CTRL+S or on the menu press File, then Save on the drop down menu and a “Save As” dialog will appear. In the dialog select an location and name for the file with a “py” extension, i.e. example.py
    cal_006
  • Run the script, on the keyboard press F5 or on the menu press Run, then Run Module on the drop down menu
    cal_008
  • On the IDLE Console window will see
    cal_010

The advantage is we run this code as many times and when we like without having to type in all in again. In IDLE press File on the menu, then Open on the drop down menu and a Open Dialog will open which will allow selection of a python file.

Note: To reduce the amount of typing programming languages often have operations that reduce the amount of typing a programmer needs to do to achieve an effect(often these operations are run faster as when a program is executed) in the example code there are two such examples

score = score + 24

can be directly replaced by

score += 24

And

health = health - 4

can be directly replaced by

health -= 4

 

In Python these are called “augmented assignment” operators and as well as addition and subtraction there are multiplication, division and modulo versions as well, i.e.

result = 5
result *= 4
result /= 2
result %= 3

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

damian mooney

bits and bytes

%d bloggers like this: