Sunday, 20 March 2011

Python: Mathematical Terminology

This will be my first actual "mini-python" lesson, I'm going to assume you've read my previous post on Python, if you haven't please read it: an introduction to Python. Got everything you need downloaded? Then I'll begin.

Again this is just going to cover the absolute basics, but they are incredibly vital to know. Maths is the core behind the majority of programs, regardless of what you want to program you're bound to come across maths at some point when programming (especially with my posts, seeing as I am mathematical nerd).

Most of the mathematical characters are fairly obvious; "*" meaning times, "+" meaning add, "-" meaning minus, "/" meaning divide and "=" meaning equals. Some of the less obvious ones are to the power of, "**" and to show the remainder when divided is "%". But once you can remember these it's very easy to apply these.

Another important thing to remember is that if you want to 'print' the answer to mathematical you do not use quotes, if you just want to show the expression you do.
print "2 + 2 =", 2+2
Will show:
2 + 2 = 4
Simple right? Just remember to separate the different strings you want to print with a comma. Now let's create a basic program using these concepts.
print "The area of a circle with radius 15 is", 3.142 * 15**2
That would show:
The area of a circle with radius 15 is 706.85834715
Try this out with different numbers, expressions and signs. Only through doing this yourself will you begin learning. In the next lesson I will bring in variables and begin coding a small working program where the user inputs data.

No comments:

Post a Comment