Tricky Talks (or TWS Blog)
Unlock the Future of Tech with Tips, Tutorials, and Industry Updates
Blockchain Basics: Creating Your First Smart Contract with Solidity and Python
- Posted By: trickywebsolutions
-
June 11, 2025

Did you know that Solidity can be used with Python? Or that it is indeed possible to deploy a Solidity smart contracts in Python? If you are curious and want to know more about it, then you are in the right place. In this blog, we will guide you through how to write a smart contract with Solidity and Python. We have made this blog beginner-friendly; therefore, you don’t need any expertise in a specific programming language to create a smart Contract with Solidity. We’ll begin by building a basic smart contract with Solidity, next deploy it with Python, and lastly learn how to communicate with it in the programmatic sense.
What are Smart Contracts?
Nick Szabo introduced the phrase smart contract back in the 19’s. Smart contracts function as computer programs deployed and executed within a blockchain network. In simple terms, we can say these are essentially programs that reside on a blockchain. They can be triggered either by external method calls or by interactions with other smart contracts. When properly written and reviewed, smart contracts help minimize fraudulent activities, malicious exceptions, and the reliance on trusted third parties.
However, smart contracts may include functions that allow for data updates. Although data can be overwritten in subsequent blocks, the historical record remains intact and fully auditable. As they run on a decentralized network, there is no requirement for a central server or authority to execute them. We can not modify the code of a smart contract after it is deployed because the blockchain is immutable.
What is Solidity?
Since we already understand what smart contracts are, we need a specific programming language to code these smart contracts. That’s where the Solidity language comes into play. These are the object-oriented, high-level programming languages developed by Ethereum. Solidity language runs on the EVM platform and has a typical syntax like JavaScript. Different types of programming languages are available out there and can be utilized to write smart contracts, such as C++, Rust, Vyper, and languages like Python and JavaScript, but Solidity is the most preferred programming language to create smart contracts.
Why Python?
Python, with its simplicity and strong libraries, is ideal for communicating with Ethereum smart contracts. Developers have tools such as Web3.py available to them, which enable them to deploy contracts, read information, and make transactions—all in Python code.
How to Write a Smart Contract: Step-by-Step Guide
In this section, we have explained how to write a smart contract with the Solidity programming language and deploy it using Python. Follow these few simple steps to create your first smart contracts in Python:
Node.js: A JavaScript runtime environment that allows you to utilize tools such as Hardhat. It is necessary for installing and executing Ethereum development frameworks.
Python: We will deploy and interact with our smart contracts using Python (along with the Web3.py library).
MetaMask: A wallet available as a web-based tool to interact with your Ethereum identity. Enables you to experiment with interactions against deployed contracts from a user interface.
Hardhat: A powerful Ethereum development environment. It helps with compiling, testing, and deploying smart contracts locally or on testnets.
Ganache (optional): A personal local Ethereum blockchain that allows for fast testing and debugging of smart contracts.
Infura (alternative to Ganache): A remote service that provides access to Ethereum networks, useful for connecting to testnets like Goerli or Sepolia.
Install dependencies:
npm install -g hardhat # For compiling Solidity code
pip install web3 # Integrating Python with Ethereum
2. Writing Your First Smart Contract
Create a file SimpleStorage.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private number;
function set(uint256 _num) public {
number = _num;
}
function get() public view returns (uint256) {
return number;
}
}
Explanation:
- pragma solidity ^0.8.0; specifies the compiler version.
- uint256 private number; declares a private unsigned integer to store a number.
- set() is a function that lets anyone store a number.
- get() is a function that returns the stored number.
This is a basic storage contract, like a key-value pair, where the “key” is predefined (we’re just storing one value) and the “value” is an integer.
3. Compile with Hardhat
Initialize a Hardhat project:
npx hardhat
Select “Create a basic sample project” and proceed with the on-screen instructions.
Place SimpleStorage.sol inside the /contracts folder and run:
npx hardhat compile
This compiles the contract and generates an ABI (Application Binary Interface) and bytecode needed for deployment.
4. Interact Using Python (web3.py)
Create a Python script like deploy_and_interact.py:
from web3 import Web3
import json
# Connect to local Ganache or testnet
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))
w3.eth.default_account = w3.eth.accounts[0] # Use the first account for transactions
# Load ABI and bytecode
with open('SimpleStorage_abi.json') as f:
abi = json.load(f)
with open('SimpleStorage_bytecode.txt') as f:
bytecode = f.read()
# Create contract instance
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Deploy contract to blockchain
tx_hash = SimpleStorage.constructor().transact()
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
# Create contract instance at deployed address
contract = w3.eth.contract(
address=tx_receipt.contractAddress,
abi=abi
)
# Store a value
contract.functions.set(42).transact()
# Retrieve the stored value
print("Stored value:", contract.functions.get().call())
Code breakdown:
Web3.HTTPProvider() connects to the blockchain.
.contract() loads the contract using the ABI and bytecode.
.constructor().transact() deploys the contract.
contract.functions.set(42) calls the set() function and stores the value 42.
.call() is used for read-only operations like get().
Conclusion
We hope, with this guide, you have learned about what smart contracts are and how to create a smart contract with the Solidity programming language and successfully deploy it in Python. You should now have a clear understanding of how Ethereum smart contracts work, how to write and compile Solidity code, and how to interact with your deployed contract using Python tools like Web3.py. This foundational knowledge opens up a wide range of possibilities in decentralized application (dApp) development. As you continue experimenting, consider exploring more advanced topics such as contract security, gas optimization, and integrating frontend interfaces. Keep building, keep learning! The world of blockchain is vast and full of potential—stay curious, contribute to open-source projects, and be part of the future of decentralized technology.