>>> int(3.1415)
3
>>> float(3)
3.0
>>> long(4)
4L



% python
>>> a = 3           # Name created
>>> b = 4



>>> a + 1, a - 1        # Addition (3+1), subtraction (3-1)
(4, 2)
>>> b * 3, b / 2        # Multiplication (4*3), division (4/2)
(12, 2)
>>> a % 2, b ** 2       # Modulus (remainder), power 
(1, 16)
>>> 2 + 4.0, 2.0 ** b   # Mixed-type conversions
(6.0, 16.0)



>>> c * 2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'c' is not defined



>>> b / 2 + a             # Same as ((4 / 2) + 3)
5
>>> print b / (2.0 + a)   # Same as (4 / (2.0 + 3))
0.8



>>> b / (2.0 + a)            # Auto echo output: more digits
0.80000000000000004

>>> print b / (2.0 + a)      # print rounds off digits.
0.8



>>> 1 / 2.0
0.5



>>> num = 1 / 3.0
>>> num                     # Echoes
0.33333333333333331
>>> print num               # Print rounds
0.333333333333

>>> "%e" % num              # String formatting
'3.333333e-001'
>>> "%2.2f" % num           # String formatting
'0.33'



>>> repr(num)               # Used by echoes: as code form
'0.33333333333333331'
>>> str(num)                # Used by print: user-friendly form
'0.333333333333'



>>> (5 / 2), (5 / 2.0), (5 / -2.0), (5 / -2)
(2, 2.5, -2.5, -3)

>>> (5 // 2), (5 // 2.0), (5 // -2.0), (5 // -2)
(2, 2.0, -3.0, -3)

>>> (9 / 3), (9.0 / 3), (9 // 3), (9 // 3.0)
(3, 3.0, 3, 3.0)



>>> from __future__ import division

>>> (5 / 2), (5 / 2.0), (5 / -2.0), (5 / -2)
(2.5, 2.5, -2.5, -2.5)

>>> (5 // 2), (5 // 2.0), (5 // -2.0), (5 // -2)
(2, 2.0, -3.0, -3)

>>> (9 / 3), (9.0 / 3), (9 // 3), (9 // 3.0)
(3.0, 3.0, 3, 3.0)



>>> x = 1        # 0001
>>> x << 2       # Shift left 2 bits: 0100
4
>>> x | 2        # bitwise OR: 0011
3
>>> x & 1        # bitwise AND: 0001
1



>>> 9999999999999999999999999999999999999L + 1
10000000000000000000000000000000000000L



>>> 9999999999999999999999999999999999999 + 1
10000000000000000000000000000000000000L



>>> 2L ** 200
1606938044258990275541962092341162602522202993782792835301376L
>>>
>>> 2 ** 200
1606938044258990275541962092341162602522202993782792835301376L



>>> 1j * 1J
(-1+0j)
>>> 2 + 1j * 3
(2+3j)
>>> (2+1j)*3
(6+3j)



>>> 01, 010, 0100              # Octal literals
(1, 8, 64)
>>> 0x01, 0x10, 0xFF           # Hex literals
(1, 16, 255)



>>> oct(64), hex(64), hex(255)
('0100', '0x40', '0xff')



>>> int('0100'), int('0100', 8), int('0x40', 16)
(100, 64, 64)



>>> eval('100'), eval('0100'), eval('0x40')
(100, 64, 64)



>>> "%o %x %X" % (64, 64, 255)
'100 40 FF'



>>> import math
>>> math.pi, math.e                            # common constants
(3.1415926535897931, 2.7182818284590451)

>>> math.sin(2 * math.pi / 180)                # sine, tangent, cosine
0.034899496702500969

>>> math.sqrt(144), math.sqrt(2)               # square root
(12.0, 1.4142135623730951)

>>> abs(-42), 2**4, pow(2, 4)
(42, 16, 16)

>>> int(2.567), round(2.567), round(2.567, 2)   # truncate, round
(2, 3.0, 2.5699999999999998)



>>> import random
>>> random.random()
0.49741978338014803
>>> random.random()
0.49354866439625611

>>> random.randint(1, 10)
5
>>> random.randint(1, 10)
4

>>> random.choice(['Life of Brian', 'Holy Grail', 'Meaning of Life'])
'Life of Brian'
>>> random.choice(['Life of Brian', 'Holy Grail', 'Meaning of Life'])
'Holy Grail'



>>> 0.1 + 0.1 + 0.1 - 0.3
5.5511151231257827e-017



 >>> print 0.1 + 0.1 + 0.1 - 0.3
5.55111512313e-017



>>> from decimal import Decimal
>>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3')
Decimal("0.0")



>>> Decimal('0.1') + Decimal('0.10') + Decimal('0.10') - Decimal('0.30')
Decimal("0.00")



>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.1428571428571428571428571429")

>>> decimal.getcontext().prec = 4
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.1429")



>>> x = set('abcde')
>>> y = set('bdxyz')



>>> x
set(['a', 'c', 'b', 'e', 'd'])



>>> 'e' in x                            # set membership
True

>>> x  y                               # set difference
set(['a', 'c', 'e'])

>>> x | y                               # set union
set(['a', 'c', 'b', 'e', 'd', 'y', 'x', 'z'])

>>> x & y                               # set intersection
set(['b', 'd'])
 


>>> engineers = set(['bob', 'sue', 'ann', 'vic'])
>>> managers  = set(['tom', 'sue'])
>>> 
>>> engineers & managers                   # who is both engineer and manager?
set(['sue'])
>>> 
>>> engineers | managers                   # all people in either category 
set(['vic', 'sue', 'tom', 'bob', 'ann'])
>>> 
>>> engineers  managers                   # engineers who are not managers
set(['vic', 'bob', 'ann'])
