I’ve decided to go with Python for blockchain since we use it the most and Python is the most popular programming language. Here I’ll show you how to use Python for Ethereum, since it is very easy to use and understand, and for all beginners in blockchain, Python might be the right tool to start.
Also, since people start combining blockchain with Artificial Intelligence and Python being the number one choice for Artificial Intelligence, this means that you can write your blockchain code and your Artificial Intelligence code using one codebase, that being Python. Also, the library that I’m going to use comes with tons of useful methods for the Ethereum blockchain, making Python very useful for our situation.
Wikipedia: “Ethereum is a decentralized, open-source blockchain featuring smart contract functionality. Ether (ETH) is the native cryptocurrency of the platform. It is the second-largest cryptocurrency by market capitalization, after Bitcoin. Ethereum is the most actively used blockchain. Ethereum was proposed in 2013 by programmer Vitalik Buterin.
Development was crowdfunded in 2014, and the network went live on 30 July 2015, with 72 million coins pre-mined. The Ethereum Virtual Machine (EVM) can execute Turing-complete scripts and run decentralized applications.
Ethereum is used for decentralized finance, and has been utilized for many initial coin offerings.”
Blockchain developer jobs skyrocketed in recent years with the rise of cryptocurrencies. The salaries vary based on the skills and industry needs, but a well-equipped engineer can get over $120,000 per year.
Of course, everything depends on the place you live in and the skills you possess, but for a detailed explanation, you can check DevSkiller.
The most used language to work with Ethereum is Solidity.
Wikipedia: “Solidity is an object-oriented programming language for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum.
It was developed by Christian Reitwiessner, Alex Beregszaszi, and several former Ethereum core contributors to enable writing smart contracts on blockchain platforms such as Ethereum.”
Solidity is very easy to understand and use, it is highly influenced by languages that are used at a largescale nowadays (JavaScript, Python, C++). The salaries for Solidity developers can vary based on your skills but usually, the average is around $110,000-$120,000 per year.
You can check CryptocurrencyJobs for more information.
Despite Solidity being the number one choice, today I’m going to use Python to work with Ethereum.
I’m going to install the requirements, write a contract, and send ethers to other accounts.
All of that is going to be done in a test environment provided by Ethereum, so you are not going to work with real ethers, and there is no risk at your data or money at all.
If you are interested in learning Artificial Intelligence or Computer Science for free, check our previous articles:
- This is The Entire Computer Science Curriculum in 1000 YouTube Videos
- 14 Life-Changing Books That Andrew Ng from Coursera Recommends
- Top 50 FREE Artificial Intelligence, Computer Science, Engineering and Programming Courses from the Ivy League Universities
- Top 40 COMPLETELY FREE Coursera Artificial Intelligence and Computer Science Courses
- How to Gain a Computer Science Education from MIT University for FREE
- You Can Now Learn for FREE: 9 Courses by Google about Artificial Intelligence, Machine Learning and Data Science
- How To Become a Certified Data Scientist at Harvard University for FREE
Python for Blockchain: here’s how to use Python for Ethereum
This article is going to be straight to the point, how to use the Python programming language to work with Ethereum. If you are not familiar with Ethereum and blockchain I suggest you read about those topics first, there are tons of great resources on the internet.
Our suggestion is this 26 minutes long YouTube video by 3Blue1Brown where he makes a detailed explanation about blockchain. Once you understand that, you are going to understand this tutorial pretty easily.
Ok, so the first step is to install the required libraries that will allow us to connect to Ethereum.
Considering that you’ve created a Python project and activated a virtual environment, you need to open the terminal and type:
Web3.py: “Web3.py is a Python library for interacting with Ethereum. It’s commonly found in decentralized apps (dapps) to help with sending transactions, interacting with smart contracts, reading block data, and a variety of other use cases.”
This is one of the best libraries for this occasion, it has derived from Web3.js which is used for JavaScript.
On the official website of the library, you can find tons of other Ethereum tutorials.
As I’ve mentioned in the intro part, we are going to use a test environment. To be able to use it, we need to install it using the pip command in the terminal:
This is all we are going to need, the next step, let’s write some code.
To be able to work in the test environment create our variable pyEth as an instance of the Web3 class, that takes the testing environment as an argument.
Next, we check if we are connected to the node. We can do that using the code:
Inside this block, we are going to add the rest of the code.
Our tester environment has provided some accounts with fake ether. We can list them with the code above.
The output is a list of accounts represented with their hexadecimal code which is their address. In some ways, the address is analogous to the account number on a checking account. You would provide this address to someone that wanted to send you ether.
To check the balance of a certain account, you must use the following code:
The output is the account balance in Wei. The smallest denomination of ether is called Wei, so that’s the value specified when sending transactions.
1 ether = 1000000000000000000 wei
1 wei = 0.000000000000000001 ether
The balance of the first account is: 1000000000000000000000000
You can do a conversion of the balance form Wei to the ether, using the function fromWei(ammountOfWei, wantedDenomination) in this case it will be:
To check the state of out blockchain we can use the following code:
The output is a dictionary with information about our block.
{ “number”: 0,
“hash”:”0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46″,
“parentHash”:”0x0000000000000000000000000000000000000000000000000000000000″,.
.. “timestamp”: 1429287689, “transactions”: [], “uncles”: []}
The block number is zero - no matter how long ago you configured the tester provider. Unlike the real Ethereum network, which mines a new block roughly every 15 seconds, this simulation will wait until you give it some work to do.
“transactions” is an empty list, for the same reason: we haven’t done anything yet. This first block is an empty block, just to kick off the chain.
Notice that the parentHash is just a bunch of empty bytes. This signifies that it’s the first block in the chain, also known as the genesis block.
Now, we are going to make a transaction. We are going to send 3 ethers from the first account to the second.
We can check the results of our transaction, with the following code:
{
‘hash’: HexBytes(‘0x15e9fb95dc39…’),
‘blockNumber’: 1,
‘transactionIndex’: 0,
‘from’: ‘0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf’,
‘to’: ‘0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF’,
‘value’: 3000000000000000000,
‘gas’: 21000,
…
}
Now we can check the account balance, to see if everything is alright.
The balance of the first account after the transaction is: 999996999999999999979000
The balance of the second account after the transaction is: 1000003000000000000000000
From the output, we can see that everything is alright. The balance went from 1,000,000 to 1,000,003 ether. But what happened to the first account? It appears to have lost slightly more than three ether. Alas, nothing in life is free, and using the Ethereum public network requires that you compensate your peers for their supporting role. A small transaction fee was deducted from the account making the transaction to the tune of 21000 Wei.
Don’t forget to check out these Python for Blockchain-related books:
Conclusion
So, here is how to start using Python for Ethereum. It is not as complicated if you first understand how the whole blockchain “thing” works. This is one of the most popular technologies in the world today and is the things are, it will definitely be very crucial for our financial future.
Before you start using our tutorial for real transactions, I kindly suggest you learn everything about Ethereum and smart contracts. You can find tons of information and free educational materials on their website.
If you are interested in learning Artificial Intelligence or Computer Science for free, check our previous articles:
- This is The Entire Computer Science Curriculum in 1000 YouTube Videos
- 14 Life-Changing Books That Andrew Ng from Coursera Recommends
- Top 50 FREE Artificial Intelligence, Computer Science, Engineering and Programming Courses from the Ivy League Universities
- Top 40 COMPLETELY FREE Coursera Artificial Intelligence and Computer Science Courses
- How to Gain a Computer Science Education from MIT University for FREE
- You Can Now Learn for FREE: 9 Courses by Google about Artificial Intelligence, Machine Learning and Data Science
- How To Become a Certified Data Scientist at Harvard University for FREE
This article is inspired by Ethereum Development.
Like with every post we do, we encourage you to continue learning, trying, and creating.