Python can be used as a calculator, directly on the command line.


In [11]:
2 + 3


5

Numbers can be written in binary, decimal, octal and hexadecimal. Anything after a "#" on a line is a comment.


In [19]:
print 0b10 # binary!
print 010 # octal
print 10 # decimal
print 0x10 # hexadecimal


2
8
10
16

All arithmetic operators, and paranthesis works, as you'd expect:


In [12]:
(2+3)*12/2


Out[12]:
30

Python's integer datatype is an "int". This is internally implemented using the C language datatype "long".


In [4]:
z = (2+3)*12/2
type(z)


Out[4]:
int

Bitwise operations are supported too.


In [16]:
0xf2 & 0x0f


Out[16]:
2

However, arbitrary precision arithmetic is supported as well - without limits! Can you count count the number of digits in the result ?


In [9]:
x = 12
y = 80
x ** y # i.e 12 raised to the power 80


Out[9]:
216022846201030691200209793924791130326656901023785450335762680219510291602196530200576L

But you don't need to... why count when you can have python do it for you ? The inbuilt str() function converts anything to its string representation


In [10]:
z = x**y
len(str(z))


Out[10]:
87

Floating point works too... and is internally represented using C datatype double.


In [13]:
1.0 * 2.0


Out[13]:
2.0

The scientific notation works for floating point numbers.


In [21]:
2.2456e-5


Out[21]:
2.2456e-05

Note: python converts to floating point only when a floating point value is used. So, 1/2 returns zero, rather than 0.5


In [15]:
print 1/2
print 1/2.0
print 1./2
print 1./2.


0
0.5
0.5
0.5