PYTHON INTERACTIVE - USING PYTHON AS A CALCULATOR - Complete Guide For Python Programming (2015)

Complete Guide For Python Programming (2015)

PYTHON INTERACTIVE - USING PYTHON AS A CALCULATOR

You can use python as a calculator, as you can add, subtract, multiply and divide numbers in python language.

Start Python (or IDLE, the Python IDE).

A prompt is showing up:

>>>

Display version:

>>>help()

Welcome to Python 2.7! This is the online help utility.

...

help>

Help commands:

modules: available modules

keywords: list of reserved Python keywords

quit: leave help

To get help on a keyword, just enter its name in help.

Common Operators in Python

Example For Simple Calculations in Python

>>> 3.14*5

15.700000000000001

Take care in Python 2.x if you divide two numbers:

Isn't this strange:

>>> 35/6

5

Obviously the result is wrong!

But:

>>> 35.0/6

5.833333333333333

>>> 35/6.0

5.833333333333333

In the first example, 35 and 6 are interpreted as integer numbers, so integer division is used and the result is an integer.

This uncanny behavior has been abolished in Python 3, where 35/6 gives 5.833333333333333.

In Python 2.x, use floating point numbers (like 3.14, 3.0 etc....) to force floating point division!