Friday, August 20, 2010

Sorry about the wait, I'm also making a c++ blog, so check that out. I'll post the link up later. For the next post we will build a text based game.

Sunday, August 1, 2010

Operations

Another capability that python has is that it can do math. You can use simple operations to solve difficult equations. Just input the equation you want to solve into the interpreter and press enter.

for example:
1+2

Here are all the operations:

operation               example                 equivalent
      +                       1+1=2                    addition
       -                       2-1=1                     subtraction
      *                       2*2=4                    multiplication
      /                       6/2=3                    division
     %                      7%3=1                    modulus (gives the remainder of a division)


This will be very helpful in any program that needs any sort of mathematical calculations.


You can also use these in more complex programs using the raw_input() command. Open a new file and start your program. 


as an example, we will make a tip calculator:

print "Welcome to tip calculator! Type in the amount of your bill and I'll calculate your tip!"

bill = raw_input("Bill:")
tip = .15*bill

print tip
This program asks for the bill, and takes 15% of it, which is the typical tip and displays it.

These operators can serve any purpose, like a car price calculator, or, as demonstrated above, a tip calculator.