Learn Web3py - 10 Code Examples & CST Typing Practice Test
Web3.py is a Python library for interacting with the Ethereum blockchain. It allows developers to deploy, interact with, and query smart contracts, manage accounts, and handle blockchain transactions programmatically.
View all 10 Web3py code examples →
Learn WEB3PY with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Python Web3.py Simple Smart Contract Interaction
from web3 import Web3
# Connect to local Ethereum node
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
# Contract ABI and address (example)
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
# Read value from contract
value = contract.functions.getValue().call()
print('Contract value:', value)
# Send transaction to contract
tx_hash = contract.functions.setValue(42).transact({'from': w3.eth.accounts[0]})
w3.eth.wait_for_transaction_receipt(tx_hash)
A minimal example showing how to connect to Ethereum and interact with a deployed smart contract using Web3.py.
Web3.py Check Ether Balance
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
account = '0xYourAccountAddress'
balance = w3.eth.get_balance(account)
print('Balance:', w3.fromWei(balance, 'ether'), 'ETH')
Check the Ether balance of an account using Web3.py.
Web3.py Deploy Simple Contract
from web3 import Web3
from solcx import compile_source
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
source_code = 'contract Simple { uint public x; function set(uint val) public { x = val; } }'
compiled = compile_source(source_code)
contract_interface = compiled['<stdin>:Simple']
Simple = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = Simple.constructor().transact({'from': w3.eth.accounts[0]})
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print('Contract deployed at:', tx_receipt.contractAddress)
Deploy a simple Solidity contract using Web3.py.
Web3.py Listen to Contract Events
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
event_filter = contract.events.YourEvent.createFilter(fromBlock='latest')
for event in event_filter.get_new_entries():
print(event)
Subscribe to events emitted by a smart contract.
Web3.py Send Ether Transaction
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
from_account = w3.eth.accounts[0]
to_account = w3.eth.accounts[1]
tx = {
'from': from_account,
'to': to_account,
'value': w3.toWei(0.1, 'ether'),
'gas': 21000,
'gasPrice': w3.toWei('50', 'gwei')
}
tx_hash = w3.eth.send_transaction(tx)
w3.eth.wait_for_transaction_receipt(tx_hash)
Send Ether from one account to another using Web3.py.
Web3.py Interact with ERC20 Token
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xTokenContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
# Check token balance
balance = contract.functions.balanceOf(w3.eth.accounts[0]).call()
print('Token Balance:', balance)
# Transfer tokens
tx_hash = contract.functions.transfer(w3.eth.accounts[1], 100).transact({'from': w3.eth.accounts[0]})
w3.eth.wait_for_transaction_receipt(tx_hash)
Read balance and transfer ERC20 tokens.
Web3.py Estimate Gas
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
gas_estimate = contract.functions.setValue(42).estimateGas({'from': w3.eth.accounts[0]})
print('Estimated Gas:', gas_estimate)
Estimate gas required for a contract function call.
Web3.py Check Transaction Receipt
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
tx_hash = '0xYourTransactionHash'
receipt = w3.eth.get_transaction_receipt(tx_hash)
print(receipt)
Fetch and inspect a transaction receipt.
Web3.py Call Contract View Function
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
result = contract.functions.getValue().call()
print('Value:', result)
Call a read-only function from a deployed smart contract.
Web3.py Deploy Contract with Constructor Args
from web3 import Web3
from solcx import compile_source
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
source = 'contract Greeter { string public greeting; constructor(string memory _g) { greeting = _g; } }'
compiled = compile_source(source)
contract_interface = compiled['<stdin>:Greeter']
Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = Greeter.constructor('Hello Web3').transact({'from': w3.eth.accounts[0]})
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print('Deployed at:', tx_receipt.contractAddress)
Deploy a Solidity contract providing constructor parameters.
Frequently Asked Questions about Web3py
What is Web3py?
Web3.py is a Python library for interacting with the Ethereum blockchain. It allows developers to deploy, interact with, and query smart contracts, manage accounts, and handle blockchain transactions programmatically.
What are the primary use cases for Web3py?
Deploying and interacting with smart contracts. Reading blockchain data and logs. Automating DeFi and trading operations. NFT minting and marketplaces. Backend blockchain integrations in Python
What are the strengths of Web3py?
Pythonic syntax easy for Python developers. Supports multiple Ethereum node connections. Flexible event and contract interaction. Easy to integrate with Python data pipelines. Strong community support and tutorials
What are the limitations of Web3py?
Limited to Ethereum and EVM-compatible chains. Performance bound by Python execution and node RPC. Not suitable for high-frequency on-chain computation. No native GUI; backend-focused. Dependent on node availability and sync status
How can I practice Web3py typing speed?
CodeSpeedTest offers 10+ real Web3py code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.