Day-4 - Getting started with Python using Algorand : Creating first transaction on Algorand.

@bob-elr · 2020-05-09 18:53 · python
https://images.hive.blog/DQmecfqeACQyEeofEHj6pnV7dqBu6PpdzLyvbLx7qPGhAzP/image.png
In the past series, we set up our environment to write Python code using Algorand SDKs in VSCode. If you haven't seen, paths to previous series are tabulated below. With this, we can be sure that our code will run successfully if we deploy it on Algorand testnet as we had it set up. As a reminder, these tutorials are to make you a good Algorand developer using Python programming language by giving you in-depth of foundational knowledge with assumption that you have little or no previous idea in programming perhaps Python(taking you from absolute beginner to intermediate). To get the best, please follow all steps diligently and accordingly. Full tutorial code for previous lesson is available on my **[Github](https://github.com/bobeu/pythonTutorial/blob/master/example.py)**. **[image src](https://www.google.com/search?q=day+4&bih=625&biw=1366&hl=en&sxsrf=ALeKk03UWtBRGBo4ACRRCNUvuGXEhCQ_gQ:1589007938854&tbm=isch&source=iu&ictx=1&fir=TSRfX4lgnKgGsM%253A%252CNm26O3_bYgyQgM%252C_&vet=1&usg=AI4_-kSxQ2xNKorRzuBfsGr9NAB25jyFng&sa=X&ved=2ahUKEwjAgvbtm6bpAhVBAmMBHQUQBukQ9QEwAHoECAcQGQ#imgrc=TSRfX4lgnKgGsM:)** ****

Previously On:

**** **DAY 1** | **DAY 2** | **Day 3** ------------- | ------------- | --------------------- **[![image.png](https://images.hive.blog/DQmRSHYVDS3dgeoAmzLuHvW5sPMdGT4B8MoYSaANEVGCPa4/image.png)](https://hive.blog/python/@bob-elr/python-become-a-programmer-and-build-real-world-traditional-or-decentralized-applications-on-platforms-such-algorand-blockchain)** | [![image.png](https://images.hive.blog/DQmcobYdJFqEgyh8qw99JZ8yn1bcdS8EHU7EVzR1hqaWA3r/image.png)](https://hive.blog/python/@bob-elr/build-on-algorand-blockchain-using-python-part-2-comprehensive-python-tutorial) | [![image.png](https://images.hive.blog/DQmdFvq5LEDPKzBducY7Su3uZi7E1Jezogvf3bF2MJ9m5U6/image.png)](https://hive.blog/python/@bob-elr/become-a-python-developer-build-on-algorand-blockchain-day-3) **** **Contents** * Run first transaction on Algorand testnet * Others. ****

Run first transaction on Algorand testnet

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 lesson](https://hive.blog/python/@bob-elr/become-a-python-developer-build-on-algorand-blockchain-day-3)we 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](https://purestake.com). If you're lost or confused how I arrived at this point, please refer to [this article](https://hive.blog/python/@bob-elr/become-a-python-developer-build-on-algorand-blockchain-day-3). Aaarrhh! I don't what is testnet. No worries! Here I go... **What Is Testnet?** A testnet mirrors an exact copy of a network or blockchain except that it is not intended for production. That is, it has exactly every attributes of the original called **Mainnet** but for the tokens which has no real value. Ethereum network has a mainnet and testnet. The Ether token on testnet is worthless having no real value hence cannot be traded. But as the name implies, it is used primarily for testing decentralized applications before deploying on mainnet. **** 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: ![dy4.JPG](https://images.hive.blog/DQmNm3HecMZevioJeyDUbqzahEZ6aezRXpyPgfj7NQj2jZv/dy4.JPG) 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](https://purestake.com)) 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_account`method 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`** ****

Dictionary In Python

**** ![image.png](https://images.hive.blog/DQmauYWsSPckErY2omQP5LYGB2NnAAck3EdwshbVihFcUdW/image.png) [src](https://www.google.com/search?q=dictionary+in+python&bih=625&biw=1366&hl=en&sxsrf=ALeKk02mT7j8XFQ87QbxqF_tJXrsF_hicQ:1589030473568&tbm=isch&source=iu&ictx=1&fir=pRYce031EN7lRM%253A%252CUtf7NdCwNlpOCM%252C_&vet=1&usg=AI4_-kR4HmsGpJJSNo_M-QTysl9oq9t_mQ&sa=X&ved=2ahUKEwjt7qfn76bpAhXXAGMBHbSzBAUQ_h0wAXoECAsQBg#imgrc=pRYce031EN7lRM:) 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` - **`" "`**. ![image.png](https://images.hive.blog/DQmeQLFN2TATURPi7FXZ2Nw4vBroggAzF88WdSGGrQz3jMo/image.png) 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. ![image.png](https://images.hive.blog/DQmcwpXCdrBFU6R1sEhT4iaZ1jKzkEYwGWYi6nG2VP1t8N7/image.png) **** **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. ![image.png](https://images.hive.blog/DQmdy1Nc8fMFyy9A5XxQmbc7Hh3cm6E1tXQM9dKRVSmwKPX/image.png) **** **Check if a key exist** ![image.png](https://images.hive.blog/DQmZ7rSZi51Ba9N8GUTeK6xd3JFCAWJ9iBXf54eG1J3MoL6/image.png) 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.* ![image.png](https://images.hive.blog/DQmNgfnGhX53vqVu7jc99ed7zs3TxxXqLXcarUT5xUK9WpW/image.png) *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..`* ![image.png](https://images.hive.blog/DQmdyrUYybZWqBQxFC3SqP1591TinA3w14FpxJeYL8Hhey7/image.png) **** **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. **** ![image.png](https://images.hive.blog/DQma5fLEsedtouxuJYWMVm7vK4nQPPqvfjKLDSKKeKPWsT9/image.png) **[src](https://www.google.com/search?q=algorand+logo&sxsrf=ALeKk01kq_quXLTGEFsGRjWDk-30zKwwsw:1587742910216&tbm=isch&source=iu&ictx=1&fir=3M1mH60Fch9kyM%253A%252CqQmH49Zz_84GVM%252C_&vet=1&usg=AI4_-kSJk77jvCPI8jSWfWLrhqul_8coew&sa=X&ved=2ahUKEwjMxqGhs4HpAhWpBGMBHTn9CdoQ9QEwAXoECAoQHw#imgrc=3M1mH60Fch9kyM:)** **[Algorand](https://.algorand.com/)** 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** **[Website](https://algorand.com)** | **[Medium](https://medium.com/Algorand)** ------------------------------------------------- | --------------------------------------------------------------------------- **[Telegram](https://t.me/Algorand)** | **[Twitter](https://twitter.com/Algorand)**
#algorand #dictionary #curangel #qurator
Payout: 0.000 HBD
Votes: 359
More interactions (upvote, reblog, reply) coming soon.