What You Will Learn!
- For Loop in Python
- while loop
- Order of Code execution in Python
- *Args
- **Kwargs
Requirements
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.
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 business | Python - Guess Game On Algorand | Python: Validate average transaction time on Algorand |
|---|
pip3 install py-algorand-sdkFor-Loop
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
rangeis a way of repeating a process of counting over a mathematical procedure calledrange. Range in this context is a set of numbers or variation between upper and lower limits of numbers.
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_accountas input parameter and iterate over it with afor-loopandrangekeyword. Usingrange()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 to0and allow caller to specify upper limit boundary. In this case, we called the function in line24with 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.
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.
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

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 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 .
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 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.

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.

Output
For loop while Loop 


Congratulations @bob-elr! You received a personal badge!
You can view your badges on your board And compare to others on the Ranking
Do not miss the last post from @hivebuzz:
Support the HiveBuzz project. Vote for our proposal!