Getting Along With Python : For and while loops, *Args. **kwargs and Order of Code execution – a treat With Using ALgorand Python SDk.

Today I will show you some important concepts in Python programming you need to know as a developer as well relative to using them in Algorand’s Python SDK as some demanded I do. This lesson gives foundational know-how of Python hence If you are intermediate, this tutorial is perfect for your level. An advance developer may find it useful as well.

What You Will Learn!

  • For Loop in Python
  • while loop
  • Order of Code execution in Python
  • *Args
  • **Kwargs


You need to have at least a beginner knowledge of Python programming. Also, introduction to Algorand ecosystem. I have written few articles to help you get started. Previously on
Srs 1, Srs 2, Srs 3, Srs 4, Srs 5, Srs 6, Srs 7, Srs 8

If you are above beginners level, you may find interest in these:

Create customized assets for your businessPython – Guess Game On AlgorandPython: Validate average transaction time on Algorand

In previous tutorials, I had used Python 3.7 but I will now switch to Python 3.8.2 . Download python 3.8.2 stable version . Be sure you have the latest pip version to download other python packages. Update it with [“python -m pip install –upgrade pip”](). Do not try to update python using pip as it is meant to install packages for python. Most time, it comes with Python installation. So it is advisable to download Python directly as an executable file directly from official site. See documentation for details. If you already downloaded Algorand Python SDK, try to upgrade as there are improvements in the kits. Run pip3 install py-algorand-sdk


Just as other iterator functions or methods in other object-oriented programming languages, for loop performs similar functions and it is often used number of times preferable to others due to its simplicity in iterating over a sequence or set of objects of related events, movements, or items that follow each other in a particular order. These sequences are known as iterables(list, dictionary, tuple, string, set, range)

  • Looping over a range is a way of repeating a process of counting over a mathematical procedure called range. Range in this context is a set of numbers or variation between upper and lower limits of numbers.

Fig 1.1


From fig 1.1, in body of function generateAccount(), we try to generate new accounts on Algorand testnet. Caller (s) is given the privilege to determine the number of accounts they wish to have the function create for them by specifying the value num_account of type integer.

  • We set num_account as input parameter and iterate over it with a for-loop and range keyword. Using range() requires that we pass in two arguments, the first being the lower limit and second – upper limit. It may include a third argument which is an interval with which subsequent iterator is picked. Specifically, we set lower limit to 0 and allow caller to specify upper limit boundary. In this case, we called the function in line 24 with 4 as argument. Note that python begins the count from ‘0’ and ‘4’ is excluded. So in reality, the sequence used is thus [0, 1, 2, 3]. Then python goes back to execute the for loop by iterating over the list [0, 1, 2, 3]. Ideally, there are four items in the list, so, it takes the first as ‘1’ and generate one account, second as ‘1’ to generate another one account until the items are exhausted.

Order Of Code Execution In Python Since the block of code for the for-loop ends at line 21, as each account is generated from line 20, it enters next line, begins the analysis from inner parentheses or operand down to outer parentheses or operand. That is, it executes the inner code account.generate_account() first before it moves to accounts.append(<the result of account.generate_account>). The for-loop block ends here while it moves to execute outer block i.e return accounts. It is agreed that Python executes codes from right to left.

Fig 1.2image.png

Above is the output of function generateAccount(4). There are four accounts in the list [...] grouped by tuple (..). The first item in each tuple represents the private Key while the other as address ==> Iterating Over A Singlen Number Or An Integer


Let us try to replace range()in for loop statement with just an integer. The result we get is a TypeError: 'int' object is not iterable. Check the Terminal.

*Args And **Kwargs

Args is a short word for Arguments preceded by an asterisk. This is how you declare it as a parameter adding an asterisk tells Python you expect caller to pass in variable number of arguments to a function. With it, you can run higher order function such as map() and filter(). It is used to pass a non-keyword, variable-length set of argument or list. Such arguments could be grouped or ungrouped . Args allows you to accept more arguments than the number of formal arguments that you previously defined as parameter. With *args, any number of extra arguments can be tacked on to your current formal parameters (inclusive of zero extra arguments) **Kwargs represents “keyword arguments“. When you pass in any valid word preceded by double **** asterisks into a function, you are simply expecting caller to pass in arbitrary number of arguments with key - value pair. Basically, it you expect to get a dictionary object. A lot of time, the order in which items in **kwargs are written are immaterial and it used to pass in keyword/key-value pair arguments. This means caller cannot pass in just a value but must be accompanied by the key. Key is the left operand while value is the right operand. Let’s see an example.

Fig 1.4


Fig 1.4 displays practical example of how **kwargs is being used. txn is a variable with value equals a grouped data of type dict containing relevant/required transaction arguments. We supply **txn into PaymentTxn class from algodsdk module and it is able to read and recognize each item in the dictionary for the purpose it is required. Omitting field fee and flat_fee for instance causes the transaction to fail and the whole process is reverted hence nothing has been written to the chain. Alternatively, you could directly specify the necessary fields leveraging on the *args attribute of PaymentTxn as depicted in Fig 1.5.

Fig 1.5


Can I iterate over *Args and ** Kwargs?

Iterating over *Args and *Kwargs is possible using any of the loops. Let us take an example using for and while loops. Fig 1.6 – Iterating over **Kwargs



Fig 1.7 For loopforloop.JPG Fig 1.8 while Loopimage.png

Iterating Over *Args Fig 1.9image.pngimage.png

Join Algorand’ developers’ forum


Developers Site

You may also like...

2 Responses

  1. Bob says:

    This is cool. I love it. Great work admin.

  1. August 17, 2020

    […] Getting started with Python using Algorand – Day 2. […]

Leave a Reply

Your email address will not be published. Required fields are marked *