Python Numbers, Dates & Times Quick Reference

Table of contents

  1. Rounding
  2. Decimal
  3. Infinity and NaN
  4. Large Numerical Arrays (numpy)
  5. Random

1. Rounding


In [1]:
#simple rounding
round(1.23, 1)


Out[1]:
1.2

In [2]:
round(-1.27, 1)


Out[2]:
-1.3

In [5]:
#rounding at .5 goes to the even number
round(1.5,0)


Out[5]:
2.0

In [6]:
round(2.5,0)


Out[6]:
2.0

In [7]:
#rounding negative numbers rounds out the tens,hundreds...
a= 1627731
round(a, -1)


Out[7]:
1627730

In [8]:
round(a, -2)


Out[8]:
1627700

2. Decimal

Use decimal for floating points that handle high precision base-10 numbers better


In [9]:
# example of the problem
a = 4.2
b = 2.1
a + b


Out[9]:
6.300000000000001

In [10]:
(a + b) == 6.3


Out[10]:
False

In [11]:
# with Decimal
from decimal import Decimal

# Decimal is constructed with strings!
a = Decimal('4.2')
b = Decimal('2.1')
a + b


Out[11]:
Decimal('6.3')

In [12]:
(a + b) == Decimal('6.3')


Out[12]:
True

In [14]:
# to change precision use a scoped local context
from decimal import localcontext

a = Decimal('1.3')
b = Decimal('1.7')
print(a / b) #0.7647058823529411764705882353

with localcontext() as ctx:
    ctx.prec = 3
    print(a / b)


0.7647058823529411764705882353
0.765

3. Infinity and NaN


In [22]:
import math

a = float('inf')
b = float('-inf')
c = float('nan')
a


Out[22]:
inf

In [16]:
b


Out[16]:
-inf

In [17]:
c


Out[17]:
nan

In [18]:
a+3


Out[18]:
inf

In [19]:
3+b


Out[19]:
-inf

In [20]:
36/c


Out[20]:
nan

In [25]:
math.isinf(b)


Out[25]:
True

In [26]:
math.isnan(c)


Out[26]:
True

4. Large Numerical Arrays (numpy)


In [45]:
# it is traditional to import numpy as np
import numpy as np

In [27]:
# python lists
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
x * 2


Out[27]:
[1, 2, 3, 4, 1, 2, 3, 4]

In [28]:
# numpy array is a perf-optimized math-centric array
ax = np.array([1, 2, 3, 4])
ay = np.array([5, 6, 7, 8])
ax * 2


Out[28]:
array([2, 4, 6, 8])

In [29]:
ax + 10


Out[29]:
array([11, 12, 13, 14])

In [30]:
ax + ay


Out[30]:
array([ 6,  8, 10, 12])

In [31]:
ax * ay


Out[31]:
array([ 5, 12, 21, 32])

In [32]:
# numpy provides some universal math functions that work on arrays
np.sqrt(ax)


Out[32]:
array([ 1.        ,  1.41421356,  1.73205081,  2.        ])

In [33]:
np.cos(ax)


Out[33]:
array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])

In [34]:
#numpy arrays are stored like c arrays (contiguous blocks of same-sized memory) and so very large arrays can be used
grid = np.zeros(shape=(10000,10000), dtype=float)
grid


Out[34]:
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])

In [36]:
grid += 10
grid


Out[36]:
array([[ 20.,  20.,  20., ...,  20.,  20.,  20.],
       [ 20.,  20.,  20., ...,  20.,  20.,  20.],
       [ 20.,  20.,  20., ...,  20.,  20.,  20.],
       ..., 
       [ 20.,  20.,  20., ...,  20.,  20.,  20.],
       [ 20.,  20.,  20., ...,  20.,  20.,  20.],
       [ 20.,  20.,  20., ...,  20.,  20.,  20.]])

In [37]:
np.sin(grid)


Out[37]:
array([[ 0.91294525,  0.91294525,  0.91294525, ...,  0.91294525,
         0.91294525,  0.91294525],
       [ 0.91294525,  0.91294525,  0.91294525, ...,  0.91294525,
         0.91294525,  0.91294525],
       [ 0.91294525,  0.91294525,  0.91294525, ...,  0.91294525,
         0.91294525,  0.91294525],
       ..., 
       [ 0.91294525,  0.91294525,  0.91294525, ...,  0.91294525,
         0.91294525,  0.91294525],
       [ 0.91294525,  0.91294525,  0.91294525, ...,  0.91294525,
         0.91294525,  0.91294525],
       [ 0.91294525,  0.91294525,  0.91294525, ...,  0.91294525,
         0.91294525,  0.91294525]])

multidimensional arrays


In [38]:
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a


Out[38]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

In [39]:
# Select row 1
a[1]


Out[39]:
array([5, 6, 7, 8])

In [40]:
# Select column 1
a[:,1]


Out[40]:
array([ 2,  6, 10])

In [41]:
# Select a subregion and change it
a[1:3, 1:3]


Out[41]:
array([[ 6,  7],
       [10, 11]])

In [42]:
a[1:3, 1:3] += 10
a


Out[42]:
array([[ 1,  2,  3,  4],
       [ 5, 16, 17,  8],
       [ 9, 20, 21, 12]])

In [43]:
# Broadcast a row vector across an operation on all rows
a + [100, 101, 102, 103]


Out[43]:
array([[101, 103, 105, 107],
       [105, 117, 119, 111],
       [109, 121, 123, 115]])

In [44]:
# Conditional assignment on an array
np.where(a < 10, a, 10) # syntax is where( condition, if_true, if_false)


Out[44]:
array([[ 1,  2,  3,  4],
       [ 5, 10, 10,  8],
       [ 9, 10, 10, 10]])

5. Random


In [58]:
import random

values = [1, 2, 3, 4, 5, 6]

In [59]:
# seed the random function
random.seed() # Seed based on system time or os.urandom()
random.seed(12345) # Seed based on integer given

In [60]:
# pick a random item out of a sequence
random.choice(values)


Out[60]:
4

In [61]:
# take a sampling of N items where selected items are removed from further consideration
random.sample(values, 5)


Out[61]:
[6, 1, 3, 2, 5]

In [62]:
# shuffle items in a sequence in place
random.shuffle(values)
values


Out[62]:
[6, 2, 1, 4, 5, 3]

In [63]:
# produce a random integer between 0 and 10 inclusive 
random.randint(0,10)


Out[63]:
1

In [64]:
# produce a random floating point number between zero and one
random.random()


Out[64]:
0.874174337334456