## 3: Python for Homework

November 17, 2016 1 Comment

Book ref: Pg 64-66

If you are ever stuck for a calculator, Python can be a stand in for you. It is able to do any calculations that a calculator can do. Some things might be easier on a calculator, but, since Python is a general purpose programming language, there’s a heap of things that Python can calculate that your calculator can’t.

Addition and subtraction are pretty straight forward, just use the + and – keys on your keyboard like this:

>>> 1+1 2 >>> 2-1 1

Try some yourself.

In the last post you were using string literals. Here you’re using numbers, but you can use them in the same way that you can with string literals. For example, you can give them names (here, I’m using* a* and *b* as very simply names):

>>> a = 1 >>> b = 2

As in the last post, once you’ve named them you can use the names to refer to the numbers indirectly:

>>> a + b 3 >>> b - a 1

Addition and subtraction are easy because you have a plus and minus sign on your keyboard. Not so multiplication or division! Since there’s no times sign on the keyboard, and you aren’t able to write a number on top of another for divide, Python instead uses symbols that are already on your keyboard, You use * for multiply and / for divide (on my keyboard * is Shift-8 and / is next to my right hand Shift key). Note that / starts in the bottom left and goes to the top right. You don’t want \ – that’s a different character. Here are some examples:

>>> 2*3 6 >>> 6/2 3.0

Try some yourself!

In the example above, see that the answer 6/2 gives a decimal answer (ie it ends in .0), even though 2*3 doesn’t. It used to in Python 2. In Python 2 you’d get this:

>>> 6/2 3

However, Python 2 also did this:

>>> 7/2 3

When you used / for division and both numbers were whole numbers, Python 2 would round the answer down to the next whole number. If you *want* to do this in Python 3 (stranger things have happened!) you use a double slash //. Like this:

>>> 7/2 3.5 >>> 7//2 3

The other thing you might be interested in trying is raising a number to a power. To do this in Python you use a double star: **. To calculate 3 squared and cubed respectively you would type:

>>> 3**2 9 >>> 3**3 27

To find the square, or cube root, you raise to the power of 0.5 and 1/3 respectively:

>>> 9**(0.5) 3.0 >>> 27**(1/3) 3.0

Python has the advantage that it can calculate stuff really quickly and display many more digits than your calculator can. If you want to know what 2 to the thousand is, Python can work it out in the blink of an eye:

>>> 2**1000 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376L

Try some yourself. If your computer stops responding Ctrl-C should stop it. If not, close the window and restart Python.