Python Tutorial : CHAPTER 2: Numbers and Operators
CHAPTER 2: Numbers and Operators.
Simple math:
The use of numbers and operators in Python is very fast and simple.
You can use the Python shell as an arithmetic calculator to make even
complicated math operations. Python will accept a wide range of
operations and arithmetic symbols. For example, you can type and test
with the following codes.
>>> 5 + 5
10
>>> 800 + 500 + 350
1650
Also, you can even combine with different operators such as sum with
multiplication and others.
>>> 5 + 5 * 25
130
>>> 2 + 2 * 25/2
27
>>> (50-5*6)/4
5
>>> 7/-3
-3
How to print results?
Knowing that, you will be able to use the famous command in Python
“print”. You can even print formatted numbers with decimals and
others. Also if you are working with currency and you would like that
your value is printed with the dollar symbol this example will very
useful for you.
>>> print “US$%.02f” %800.0
US$800.00
The command “print” is used to display all kind of information you
process in Python. The way you can format this numeric value is using
the preceding element you would like to print after the currency value.
In this case is “US$”. Also wit the “.02f” you can define the decimal
places this value can take.
