Learning Blockchain through Python: Part 1
Blockchain is a big word and a confusing word to newbies.
A blockchain is an online public bookkeeping ledger.
It automatically registers a financial transaction from the sender to the receiver.
Before we go into depth about the blockchain, let’s build a simple financial transaction from Python.
Here are the thought processes:
What information do you need to complete a transaction?
You will need:
👉 sender’s name
👉 receiver’s name
👉 amount to transact
How do you store the data with information?
In Python, dictionaries are preferable for transaction data storage since it is a set of key: value pairs that are searchable.
Construction a transaction
If Alice, the sender, sent $30 to Bob, the receiver, through a transaction, here is how to construct the data:
transaction1 = { ‘amount’: ‘30’, ‘sender’: ‘Alice’, ‘receiver’: ‘Bob’}
More transactions
You can register more transactions by repeating the above method:
transaction2 = { ‘amount’: ‘200’, ‘sender’: ‘Bob’, ‘receiver’: ‘Alice’}
transaction3 = { ‘amount’: ‘300’, ‘sender’: ‘Alice’, ‘receiver’: ‘Bob’ }
transaction4 = { ‘amount’: ‘300’, ‘sender’: ‘Bob’, ‘receiver’: ‘Alice’ }
transaction5 = { ‘amount’: ‘200’, ‘sender’: ‘Alice’, ‘receiver’: ‘Bob’ }
transaction6 = { ‘amount’: ‘400’, ‘sender’: ‘Bob’, ‘receiver’: ‘Alice’ }
Combine all transactions together
Assume you will have six transactions total. You combine them together in one assigned variable, “mempool”:
mempool = [transaction1, transaction2, transaction3, transaction4, transaction5, transaction6]
Add one more transaction
Since there is a new transaction that needs to register in the blockchain, we can add through .append() :
my_transaction = { ‘amount’: ‘100’, ‘sender’: ‘Alice’, ‘receiver’: ‘Bob’ }
mempool.append(my_transaction)
Search transactions
Now, you have got your ledger input through Python manually!
You can search transactions whichever you want to. For instance, if you want the first 3 transactions, you can do:
block_transactions = mempool[0:3]
print(block_transactions)
Congratulation!
You got your blockchain! Although it does not automatically generate and capture transactions, you get your first blockchain at least!
You can have a code below:
transaction1 = {
'amount': '30',
'sender': 'Alice',
'receiver': 'Bob'}
transaction2 = {
'amount': '200',
'sender': 'Bob',
'receiver': 'Alice'}
transaction3 = {
'amount': '300',
'sender': 'Alice',
'receiver': 'Bob' }
transaction4 = {
'amount': '300',
'sender': 'Bob',
'receiver': 'Alice' }
transaction5 = {
'amount': '200',
'sender': 'Alice',
'receiver': 'Bob' }
transaction6 = {
'amount': '400',
'sender': 'Bob',
'receiver': 'Alice' }
mempool = [transaction1, transaction2, transaction3, transaction4, transaction5, transaction6]
#1 add a new transaction
my_transaction = {
'amount': '100',
'sender': 'Alice',
'receiver': 'Bob' }
#2 add new transaction to the existing one
mempool.append(my_transaction)
#test print
#print(mempool)
#3 create a new list of addtional three new transactions
block_transactions = mempool[0:3]
print(block_transactions)