___  ___    _ _    _  _ _____   _____
 / __|/ _ \  | | |  | || |_ _\ \ / / __|
| (_ | (_) | |_  _| | __ || | \ V /| _|
 \___|\___/    |_|  |_||_|___| \_/ |___|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Getting started with Python using Algorand - Day 3

BY: @bob-elr | CREATED: May 2, 2020, 5:22 p.m. | VOTES: 42 | PAYOUT: $6.27 | [ VOTE ]

https://images.hive.blog/DQmdFvq5LEDPKzBducY7Su3uZi7E1Jezogvf3bF2MJ9m5U6/image.png
Yay! Its the second day of May, 2020. I received couple of chats requesting that I update my blog with more tutorials. Many thanks to everyone who sent me thumbs up. If you have quests to build solutions on Algorand blockchain, from this time, I will refactor the tutorial to take you down the solution lane to reflect a real-life solution platform - ALGORAND.

image src

Previously On

DAY 1 |
DAY 2
----------------------------------------------- | ---------------------------------------------
Getting started with Python using Algorand - Day 1 | Getting started with Python using Algorand - Day 2.

Contents

If you don't have python installed on your computer, I assume you have gone through the earlier posts itemized above so you quickly install Python application and Visual studio Code (recommended for this tutorial). For this series, we will need Algorand package to interact with Algorand chain. It supports four major languages - Java, Python, Goal and Javascript.

To install on Window OS, please follow these guide(Please note this has worked for me). See doc to install on other operating systems

Requirement

Although, it may change in future, currently, there are three ways to connect to Algorand chain as specified in the documentation.

For simplicity and fast connection, I will use a third party service.
Now, open your terminal (in the search box, type CMD and enter). Type and enter the following in the terminal.

pip3 install py-algorand-sdk

[IMAGE: https://images.hive.blog/DQmNnbNzBj49yNDpcpTsMB1KC9WvdcQVn7yV6rKmXaYxGZd/installalgo.JPG]

From the image, "it says requirement already satisfied" because I have it installed already. At this point, we have Algorand SDKs in our Python environment. Let us proceed from where we left.

To interact with Algorand testnet, we will need a token as an Application Programming Interface (API) since we are going to use a third party service instead of downloading a full copy of the blockchain or run a node which can be very tedious and consume more of our device's resources. Good to know that Purestake offers such convenient service. Head to the website, perhaps, you need to complete a registration before you can access the services. A free membership would give you what we need for this course. Go to your dashboard, copy the API and your token. They are major two parameters we will need to get connected to Algorand Network.

By now, you should have the token and API ready. We will need a connect ion to Algorand development kits (SDKs).

from algosdk import algod

Boom! our connection is ready.

[IMAGE: https://images.hive.blog/DQmcfngb8tvKRfb7TF9dYBAxvxEZrBrsRhryasGh4sDoQ3f/image.png]

If you are a beginner, you obviously would not understand much of what I just did. Still, don't worry, II will break everything down. Please follow the tutorial accordingly

Understanding Data Types In Python

Variables are used to hold data in storage and it can hold different types of data. Below are inbuilt data types in Python.

Text str msg = "Welcome" Numeric int, float, complex int --> dozen = 12, float --> one_decimal = 12.1, complex --> y = 12j Sequence list, tuple, range list --> vowel = ['a', 'e', 'i', 'o', 'u'], tuple --> vowel = ('a', 'e', 'i', 'o', 'u'), range --> x = range(10) Mapping dict x = Boolean bool y = True Set set, frozenset set --> veges = {"cucumber", "pumpkin", "garden egg"}, frozen set --> x = frozenset({"apple", "banana", "cherry"}) Binary bytes, bytearray, memoryview bytes --> a = b"My name", bytearray --> y = bytearray(8), memoryview --> memoryview(bytes(6))

Note: Python treats data without quotes as number. So, to save and get values as text, you must use " " (double quote) or ' ' (single quotes).

If you would need to use single quotes within a text, the right practice demands that you start your text with double quotes and can use as many single quotes within the text. See example below.

[IMAGE: https://images.hive.blog/DQmbF6WkvJwoKbAH5k43ppGJFzgrcVaxaaQiECok2BK7nVd/Python%20quotes.JPG]

Comments In Python

Comment in python is denoted with # in VSCode. Python uses it to denote a comment and it ignores whenever it comes across it. As a good developer, you should develop the habit of leaving short and precise comments in your code. The importance is to enable other programmers who will read your code in the future to understand your code and what you are trying to achieve.

[IMAGE: https://images.hive.blog/DQmZMobx4JT5HZdEU42HW48zmVzPQoNZjwhXqVutnzKkKKC/commentinPython.JPG]

How To Know The Type Of Data You Input Into Your Program?

In your code editor, type the following:

x = 100
y = "What did you notice?"
z = True
a = False
b = x = frozenset({"apple", "banana", "cherry"})
c = bytearray(8)
d = memoryview(bytes(6))
veges = {"cucumber", "pumpkin", "garden egg"}
f = {"name" : "James", "age" : 30}
g = range(10)
vowel_list = ['a', 'e', 'i', 'o', 'u']
vowel_tup = ('a', 'e', 'i', 'o', 'u')
one_decimal = 12.1
h = 12j

print(type(x))
print(type(y))
print(type(z))
print(type(a))
print(type(b))
print(type(c))
print(type(veges))
print(type(f))
print(type(g))
print(type(vowel_list))
print(type(vowel_tup))
print(type(one_decimal))
print(type(h))

Result

Copy the codes above and paste in your code editor, you get the result in the image.
[IMAGE: https://images.hive.blog/DQmTNbm5aNxwh2w9qaPg8ZqfjuaMD3jK8oag7kJ6nUfDFjR/image.png]

Integer (int)

Int is short for integer, It is a whole number and can either be a positive integer or negative. Usually, int object does not contain decimals and are of unlimited length.

score_number = 20
short_number = 133333
long_number = 15392099222126744

To convert a float type of number or integer literal or from a string literal to a whole number, you will have to do what is called casting.

[IMAGE: https://images.hive.blog/DQmXCtaymceCyFunrdDrmJrFips71sKzZtTfzedZZRHw5JY/undesiredResult.JPG]

From the image above, We asked user to enter two numbers in lines 46 and 47, add the numbers together (line 48) and print the result to user on line 50. Oops! we get an undesired result of 2355 in the terminal after user entered 23 and 55 as separate numbers. That's not what we want right? In programming, you as the developer should already know the result a line of code will give and not a kind of guess game except where you expressly want Python to select random numbers on your behalf.

Using the input keyword, Python, by default, expects a value in string data type hence reason it treated the entries as string by adding two strings 23 and 55 together. But we will have to tell python what exactly we want. That is why you're the smart guy not python. It does exactly what you ask it to do.

Now, let us cast the value we expect from the user to a whole number for our program to work properly.'

[IMAGE: https://images.hive.blog/DQmcnE79EJjKcmKD1VPFWCkzjbzbG6wJmgUHeeM6jQhUyE8/concatError.JPG]

Again we didn't get what we want. Python throws and error says Can only concatenate str (not 'int') to str. Print() function can only concatenate(a way of joining data together) data of same type. So let us cast the result back to string.

[IMAGE: https://images.hive.blog/DQmYTp5kQYLesvnBYBwkSZTuXoMbzf6cq8YdBTMuPhmQWZj/rightResult.JPG]

Yay! we get the desired result.

Note: The same method of casting applies to other data types.

Oh, I don't know what is a constructor. Ok good. No worries. I just explained it below.

In any programming language, parentheses ( ) is very important and powerful. It is used as access control and restriction to sensitive objects. So don't be surprised that blockchain codes are a function of parentheses. I will expatiate more on it shortly.
A constructor is incomplete without parentheses which is used to create an instance of a Classobject. More on 'class' later.

String (str)

score_number = "twenty"
short_number = str(133333)

Boolean (bool)

Boolean has basically two values -True or False. Comparing two situations, states, variables, expressions etc, evaluates to either of the boolean values. To compare two objects, the following operations are used:

> comparison sign == --> a == b --> Does a equal to b?
> "greater than" sign > --> a> b --> Is a equal great than b?
> "less than" sign < --> a < b --> Is a less than b?

Other operator in Python

Arithmetical Operators

Assignment Operators

List

A list in Python is a collection of items that in order of input. Members or items in a list can be altered i.e add, removed, replaced and can be duplicated(you an make a copy of it). A list in Python is denoted with [] square brackets.

menu = ["sausage", "fish pie", "meat pie"]
arbitrary_list = ["sausage", 10, "fish pie", True, "meat pie", {"name": 'James', "age": 49,}]
`

We created a variable menu and assigned a list containing 3 items to it.Here, Python sees "menu" as one object which contains 3 food items - a way of grouping series on objects that are of same or different types together. You would notice arbitrary_list contains four different data types - string, integer, boolean and dictionary.

Tuple
A tuple is denoted with parentheses (). It is a collection of ordered items that cannot be changed. It can take arbitrary number of data types.

arbitrary_list = (["sausage", 10, "fish pie", True, "meat pie", {"name": 'James', "age": 49,}], 10, {"email": "bob@gmail.com"}, ["country", "town"])

You can see from the examples, I have given examples to other data types. I want you to try it out something as a challenge. Give examples of other data types I listed above. Leave your solutions or link to them in the comment. If you like this tutorial, do well to give a like and reblog. See you in next series.>

[IMAGE: https://images.hive.blog/DQma5fLEsedtouxuJYWMVm7vK4nQPPqvfjKLDSKKeKPWsT9/image.png]
src

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.

Website Medium Telegram Twitter
TAGS: [ #algorand ] [ #datatype ] [ #constructor ] [ #curangel ]

Replies

@barineka | May 4, 2020, 8:13 p.m. | Votes: 1 | [ VOTE ]

this is really helpful. wished i could code. well written

@bob-elr | May 5, 2020, 10:48 a.m. | Votes: 0 | [ VOTE ]

Yes you can if you indeed want to actualize your wish. If you follow these tutorials closely, you will start to code in no time. Thank you @barineka

@barineka | May 6, 2020, 1:48 p.m. | Votes: 0 | [ VOTE ]

are serious, but i dont know the basics of coding

@bob-elr | May 7, 2020, 1:03 p.m. | Votes: 0 | [ VOTE ]

Here are the basics I started on my blog. This is just the third series. You only nerd to take your time and read through it. You will understand better.

@barineka | May 7, 2020, 8:44 p.m. | Votes: 1 | [ VOTE ]

thanks for the tutorial

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>