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.

T1.jpg

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, transaction]

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:

bmc.jpg

If you want to support my writing, buy me a coffee here.

Photo by GuerrillaBuzz Crypto PR on Unsplash

Hive divider.gif

Note: Cross-references of this article have been created by the author and have been cross-referenced on multiple platforms here. Please reference the resources and credits here. Reach out to the authors if you have any questions.

Did you find this article valuable?

Support xuanling11 by becoming a sponsor. Any amount is appreciated!