How to calculate the target from bits
Let's start with a block-header, always 80-bytes that looks like this:
04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b577627061880564bec
From the 80-bytes, the bits are actually the 72nd to 76th byte:
04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b57**76270618**80564bec
or
76270618
This number, however, is in little-endian, so we have to reverse the bytes:
18062776
The first byte is the "exponent"
e = 0x18
The next 3 bytes are the "coefficient"
c = 0x062776
You plug this into a formula:
target = c * 2**(8*(e - 3))
In our case, that is:
target = 0x062776 * 2**(8*(0x18 - 3))
Which turns out to be:
0000000000000000062776000000000000000000000000000000000000000000
Let's calculate the hash of this block header using Python 2:
from hashlib import sha256
header = "04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b577627061880564bec".decode('hex')
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')
The output is
0000000000000000040199a6c7b922f711ee7e98cd58863b8b981b02d2b83e13
You can compare this to the target
>>> 0x0000000000000000040199a6c7b922f711ee7e98cd58863b8b981b02d2b83e13 < 0x0000000000000000062776000000000000000000000000000000000000000000
True
That's how we know a block satisfies the proof-of-work.