Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Computer Science How Blockchain Works
Computer Science Beginner FREE

How Blockchain Works

Lesson 1 of 17 Beginner Interactive

Blockchain is a decentralized, immutable ledger that records transactions across many computers.

Key Properties

  • Decentralized — no single authority
  • Immutable — can't alter past records
  • Transparent — all can verify
  • Consensus — agreement mechanism

Syntax

COMPUTER-SCIENCE
# Block structure
{
    index: 0,
    timestamp: 1700000000,
    data: "Genesis Block",
    previous_hash: "0",
    nonce: 0,
    hash: "0000abc..."
}
Blockchain Concept
PYTHON
import hashlib
import json

class Block:
    def __init__(self, index, data, previous_hash):
        self.index = index
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.calculate_hash()

    def calculate_hash(self):
        content = json.dumps({
            "index": self.index,
            "data": self.data,
            "previous": self.previous_hash
        })
        return hashlib.sha256(content.encode()).hexdigest()

# Create blockchain
chain = [
    Block(0, "Genesis Block", "0"),
    Block(1, "Transaction: Alice->Bob", chain[0].hash),
]

for block in chain:
    print(f"Block {block.index}: {block.hash[:16]}...")

Practice

1
Exercise

Why is blockchain called immutable?

Answer
Because each block contains the hash of the previous block, making it virtually impossible to alter past records without detection.

Quick Quiz

1

What makes blockchain decentralized?

Blockchain distributes data across many nodes with no central control.

Interview Questions

A blockchain is distributed and immutable. A database is centralized and can be modified by the administrator.