Full tutorial code for previous lesson is available on my Github.
| DAY 1 | DAY 2 | Day 3 |
|---|---|---|
Contents
While we learn the basis of Python, it is of great advantage if we work real-life examples. We will run our first transaction on Algorand testnet with python code by Creating a wallet i.e generating a private key and a public key (account address) that will be able to hold ALGO Token on the testnet. Remember, in this lessonwe declared some variables to hold the parameters we need to pass into a class's method to established a connection to Algorand testnet using a third party service - Purestake. If you're lost or confused how I arrived at this point, please refer to this article. Aaarrhh! I don't what is testnet. No worries! Here I go...
What Is Testnet?
Following this code, we need only the first 7 lines of code. You can comment out the rest code for the purpose of this section.
1. from algosdk import account, encoding, algod, mnemonic, kmd, wallet
2. from algosdk.future import transaction
3. algod_address = "https://testnet-algorand.api.purestake."
4. algod_token = "eVXi2wPlDE8uF15mkil5Z2........"
5. myProject_token = {"X-API-Key": algod_token}
6. algod_client = algod.AlgodClient(algod_token, algod_address, myProject_token)
Create a new file and name it day_4.py. Copy the above line s of code and paste in the day_4.py file. Add the following as in image:
Yay! It worked! We just made our first transaction on Algorand Testnet. Don't get lost, I'll explain in detail what just happened.
From line 1, we import account, encoding, algod, mnemonic, kmd, and wallet from algosdk.
Line 2, we import transaction from algosdk.future.
For this section, we only need the account and algod classes to complete the transaction. The rest will be used for different purposes which I will explain later.
Line 6: variable algod_address holds an API - URL containing an access . to Purestake with which we will make a request to Algorand testnet. Note that the Algod API is used for querying historical information from the blockchain, getting information about blocks and transactions, and sending transactions.
Line 7: variable algod_token is a placeholder for an access key(gotten from Purestake) required for the API to pass through.
Line 8: variable myProject_token holding a data in dictionary format which maps the key "X-API-KEY" to the secret key needed as the third argument we need to pass in to the AlgodClient.
Line 10: variable algod_client is where the actual connection takes place. It takes in three variables and run a computation which connects us to the testnet.
Line 13: variable names - private_key and address are passed into a function called generate_account which is a method of the class account. The . sign is used for accessing methods/functions in a class. A wallet is created for us by invoking the account.generate_accountmethod and finally we print out the private and address to the wallet on lines 14 and 15.
In the terminal, you could see the result printed out for us as:
Private key : AV08C3aqF+Ey6KlimV97iOnmOHCbC8OH9fndMTnjL/O1AP+RAm77emGkzR3SWRVqU+k/owg9WT5k9UhXPfjokA==
Address: WUAP7EICN35XUYNEZUO5EWIVNJJ6SP5DBA6VSPTE6VEFOPPY5CILVXG4XM
Python dictionary is an unordered collection of items of any data type. Its structure requires a key and a value pair to be displayed as a dictionary while other compound data types have only value as an element. To retrieve a value in a dictionary, a key is expected often enclosed in an inverted comma or quotaion mark - " ".
Dictionary can accept arbitrary number of data types as depicted above. From the day_4.py file, user_profile is a variable/placeholder for dictionary containing 5 keys pointing to 5 values of types: string, string, integer, list(of 3 items) and a dictionary(of 3 items). A dictionary can be nested more deeper based on the nature and arrangement of data it is meant for. To access an item in a dictionary, you should refer to its key.
Example: name = user_profile["name"]
Or
name = user_profile.get["name"]
A dictionary by default has a get property which can be invoked to extract a value provided the key enclosed in a square bracket.
Changing A Value In Dictionary
To change the value of an item in a dictionary, simply use the placeholder followed by the key to the value you wish to change enclosed in square brackets and assign it a new value of any type.
Ways to Loop through a dictionary
Print out all the values in a dictionary: To achieve this, we use a for loop to iterate over every items in the dictionary. Passing x and y into the for loop will by default assigns the first variable to the key and second to the value. I will expatiate more on for loop in the next lesson.
Check if a key exist
Using the If statement in line 33 tests how true is the statement. Different from an ambiguous statement, python will return either yes or no. Ensure that the key you are trying to search is properly as alteration in the letters for instance, replacing h in "hobbies" with H will evaluate to false.
Line 36 and 37, excepting the keys, prints out all the values in the user_profile dictionary.
Getting the number of Items in the user_profile dictionary. requires that we use the len() function which is a readily available inbuilt function from python module..`
Conditions In Python
Conditions in Python evaluate either true or false. It is represented in 1 and 0. Condition is also used to test the genuineness of an expression or existence of a true value, result of which returns true or otherwise in the absence of it.
To keep it short, I will continue on this topic in the next series.
Algorand is a technology company that built and developed the world’s first open, permissionless, pure proof-of-stake blockchain protocol that, without forking, provides the necessary security, scalability, and decentralization needed for today’s economy. With an award-winning team, we enable traditional finance and decentralized financial businesses to embrace the world of frictionless finance.
More information about Algorand
| Medium | |
|---|---|