How to Code a Blockchain in 6 Steps
Build a quick and simple blockchain in Python

What exactly is a blockchain? The best way to understand it is to see one in action , or better yet, to build one. Essentially a blockchain is a way to store data with its history — and a blockchain can be built in any programming language.
I played around with coding a blockchain when I wrote this piece about it being the data innovation showing the value of data lineage. If you’re interested in the background of blockchain and what it has to do with Bitcoin then that’s a good place to start.
Ok, enough talk — here’s the code, step-by-step in Python. To keep it simple we’ll code the blockchain to print to the terminal and I’ll explain each step so you don’t need to know Python to follow along.
If you don’t have Python set up then you can run the steps in an online Python session → here

Step 1 / Put Some Data in a Block
The starting point for a blockchain is actually simple.
A blockchain has blocks like this:
[ ]
With data in them:
[ data ]
Our blocks are going to have transaction data in them. The transaction will just be a sentence saying what happened. We’ll model our simple blockchain on Bitcoin and our first fictional transaction is a payment of 1 bitcoin from one wallet (Wallet#1) to another wallet (Wallet#2).
Our first block — known as the genesis block — looks like this.
#Start with a basic block
transaction_1="Wallet#1 paid 1 bitcoin to Wallet#2"
genesis_block=(transaction_1)
print(genesis_block)
Print() in python shows or prints the value in the terminal.
The above code snippet just returns the transaction:
Wallet#1 paid 1 bitcoin to Wallet#2
Step 2 / Prepare the Chains
The next thing we need is something to chain blocks together.
To do this we use a hash. A hash is a cryptographic algorithm that creates a particular value from any input. The value is one-way meaning it cannot be decrypted. Use a strong enough hash method and two different inputs will practically never lead to the same hash value —
a hash is the key ingredient to a blockchain
Each block gets a hash. Each block also stores the hash of the prior block. The hash for each block is derived from the other data that will go in the block. Then the hash itself is added onto the block.
Our block data structure is now:
[ prior block hash, data, block hash ]
The thing to remember here is that a practically unique code (the hash) is being created for each block that everyone on the network can use to verify blocks as they receive them.
Python has a built-in method: hash(). But it won’t work for a blockchain as it produces different results in each session. We need a stable hash so let’s import a Python library that has the same hashing algorithm Bitcoin uses, it’s called SHA-256. We’ll create our own hashing function from it.
Add this code snippet below the previous one:
import hashlib # the library with the hash function we need# Create our hash function
def bit_hash(data):
string_data=str(data) # always use a string
formatted_data= string_data.encode('utf-8') # encode for hashing
bit_hash = hashlib.sha256(formatted_data).hexdigest() # hash it
return bit_hash # function outputs the hash
We now have everything we need for a simple blockchain.
Step 3 / Add Chain Links to Our Block
Now we get the hashes for our block and add them to our new data structure:
[ prior block hash, data, block hash ]
Since there is no prior block for the genesis block, we use zero for the prior block hash.
#Create the genesis block hash
genesis_block_hash = bit_hash((0,transaction_1))
print(genesis_block_hash)
I get — daaec479bfec0c4469a085e4ba938193133b4b81d9b018c2ff4e7ea7bf1c87a4 as the hash value. Now let’s recreate our genesis block, this time complete with hashes for our chain.
#Recreate the genesis block - this time with hashes
genesis_block=(0,transaction_1,genesis_block_hash)
print(genesis_block)
Now I get —
(0, ‘Wallet#1 paid 1 bitcoin to Wallet#2’, ‘daaec479bfec0c4469a085e4ba938193133b4b81d9b018c2ff4e7ea7bf1c87a4’) and you should too.
Step 4 / Create the Next Block
With our blockchain now ready for chaining — let’s create the second block.
Add this code snippet below the previous one:
# Create the transaction for block 2
transaction_2 = "Wallet#1 paid 2 bitcoin to Wallet#2"# Create the block 2 hash
block_2_hash = bit_hash((genesis_block_hash, transaction_2))# Create block 2
block_2 = (genesis_block_hash,transaction_2,block_2_hash)
print(block_2)
I get —
(‘daaec479bfec0c4469a085e4ba938193133b4b81d9b018c2ff4e7ea7bf1c87a4’, ‘Wallet#1 paid 2 bitcoin to Wallet#2’, ‘33f7429cbd8613bc8a09809d35d269223dd3b7cc84fcc516de31b19db028ba41’)
Now we can see the pattern for creating the next block — all we need to do is create its hash and then create the new block with new transaction data.
Step 5 / Create a Function for Creating New Blocks
Add this code snippet below the previous one:
#Create a function that creates a new block and add a property to tell us what number block it is
def create_block(prior_block_hash,prior_block_number,transaction_data):
block_number = prior_block_number + 1
block_hash = bit_hash((prior_block_hash,block_number,transaction_data))
new_block= (prior_block_hash,block_number,transaction_data,block_hash)
return new_block
Step 6 / Use the Function to Print Blocks
We’ll now use our function to ̶m̶i̶n̶t̶ ; I mean print blocks to the terminal.
We follow the programming convention of starting our block numbers at zero rather than one (computers do work in binary 0/1 after all.)
Add this code snippet below the previous one:
#Print a genesis block
genesis_block = create_block(0,-1,"Wallet#1 paid 1 bitcoin to Wallet#2") # block numbering to start at zero
print(genesis_block)#Print 3 more blocks
prior_block=genesis_block
for i in range(3):
prior_block_hash=prior_block[3] # 3rd element of prior block
prior_block_number=prior_block[1] # 1st element of prior block
next_block=create_block(prior_block_hash,prior_block_number,"Wallet#1 paid " + str(i+2)+" bitcoin to Wallet#2") # pay 1 more bitcoin in each block
print(next_block)
prior_block=next_block
Let’s look at the blocks that printed in my session:
Genesis Block:
(0, 0, ‘Wallet#1 paid 1 bitcoin to Wallet#2’, ‘e8dfb4c4330e6cec6b34f1ff67e58f5d6fde2e206653a185a3f43b6a7737bf3b’)
Block 1:
(‘e8dfb4c4330e6cec6b34f1ff67e58f5d6fde2e206653a185a3f43b6a7737bf3b’, 1, ‘Wallet#1 paid 2 bitcoin to Wallet#2’, ‘dd28f753d6b8cd1830dba218a44de42c44d9c68c969c16259a45205c604c4365’)
Block 2:
(‘dd28f753d6b8cd1830dba218a44de42c44d9c68c969c16259a45205c604c4365’, 2, ‘Wallet#1 paid 3 bitcoin to Wallet#2’, ‘a3c0f13dd1107c8f9953e7f8ef38d45fe4be1bbacfd1a89bd362a24aa984b906’)
Block 3:
(‘a3c0f13dd1107c8f9953e7f8ef38d45fe4be1bbacfd1a89bd362a24aa984b906’, 3, ‘Wallet#1 paid 4 bitcoin to Wallet#2’, ‘b33ce26bcde310dd102fa8748f9221ac053312448e01114409a20405c553369f’)
Great — the blocks are chaining together. The hash of each block becomes the prior hash of the next block.
Anyone can verify a block as valid and that it chains back to the prior block, also meaning that it is consistent with the entire history of all prior blocks.

Conclusion
We’ve now coded a basic blockchain in 6 steps in the Python terminal, built on the same principles as the Bitcoin blockchain.
Maybe this exercise has got you thinking about the possibilities of this technology and what a more complete programme would look like. Hopefully you learned something new and had some fun along the way. I’d love to know your thoughts.
And if you’re interested in more reading
You can read the Bitcoin Whitepaper introducing the first blockchain → here
You can read about the Bitcoin data structure and hashing → here
You can check out the Bitcoin genesis (first) block → here
Post Updates:
Special thanks to u/yawpitch and the other members of r/Python for helping me get out of the tangents and improving on the original post.