How to Use the Shell to Calculate Sums on the Raspberry Pi - dummies

# How to Use the Shell to Calculate Sums on the Raspberry Pi

You can use the shell to carry out simple calculations on your Raspberry Pi. There are different mathematical operators you can use in your sums. Just put the sum after the print command, like this:

```>>> print 5+5
10
>>> print 9-4
5
>>> print 7*7
49
>>> print 10/2
5```

Note that you don’t use quotes around the sum in your print command. What would happen if you did? Python would put on the screen literally what you asked it to, like this:

```>>> print “5+5”
5+5```

There are a few surprises in how division is carried out in Python. If you cast your mind back to your mathematics lessons, you might remember that whole numbers, which have no decimal portion, are called integers.

In Python 2.7, if you divide an integer by an integer, you get an integer as a result, which means the answer can be less accurate than you might expect from a computer. For example, what’s 7 divided by 2?

```>>> print 7/2
3```

Close, but not close enough. To force Python to give you an answer that would pass a teacher’s scrutiny, add a decimal portion to one of the values in your sum, like this:

```>>> print 7/2.0
3.5
>>> print 7.0/2
3.5```

If you want to force the rounding effect to remove any decimal portion from your answer, you can use the // (floor division) operator, like this:

```>>> print 10.0/3
3.33333333333
>>> print 10.0//3
3.0```

An operator you might not have come across before is modulo. It uses the % sign and tells you the remainder after a division. Here are some examples:

```>>> print 10%3
1
>>> print 10%2
0```

You can use that operator to tell whether one number is divisible by another (the modulo is 0 if so).

Operator Description
Subtraction
* Multiplication
/ Division
// Division, discarding any decimal portion
% Modulo, which shows the remainder after a division

These sums are quite basic, but you can enter more advanced sums by stringing together numbers and operators. As in algebra, you use parentheses to surround the bits of the sum that belong together and should be carried out first. For example:

```>>> print (10.0/3)*2
6.66666666667
>>> print 10.0/(3*2)
1.66666666667```

You can also do mathematics in the shell by just entering the sums without a print command, but it’s essential to use it when you’re creating programs, as you’ll see shortly.