Do you think IIT Guwahati certified course can help you in your career?
No
Introduction 🤷♀️
Many times we need to do a process again and again in the program😴. So, for each time, we will have to write a block of code. Life would have been simple if we wrote just once, and it does the work the number of times we need😊. Well, it is the work of a loop.
Loop is just a way to simplify the task of repeating sets of instructions. We will learn all about loops that exist in web2py and makes our lives easy.
Web2py overview 🧐
Web2py is an open-source web application framework that focuses on rapid development by placing a strong emphasis on ease of use and productivity.
It is one of the easiest frameworks to learn and use despite its simplicity though web2py is jam-packed with features and is quite powerful and flexible. Massimo Di Pierro developed it and released it in 2007.
In the next section, we’ll see the different types of syntax in Web2py
Basic Syntax😲
As we know web2py is a very flexible language. It supports all the control structures that are supported by python. It works on the full-stack framework which is very helpful in developing database-driven web applications.
Now the above code will produce the following output:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
Here the item is added, and then the loop starts. It firstly prints the first item and iterates to the last item. The iterateable object items can be Python tuple, Python list or Rows object, or can be any other object which is implemented as an iterator.
While 😵
To use the while loop the syntax needs to be used as follows:
{{k = 3}}
<ul>
{{while k > 0:}}<li>{{=k}}{{k = k - 1}}</li>{{pass}}
</ul>
Output
The code will produce the following output:
<ul>
<li>3</li>
<li>2</li>
<li>1</li>
</ul>
Here the iteration starts as we see the top value is provided as 3. Then it starts, and then a reduction by one is made as per the condition, and the loop continues till the end. Hence, the below output is produced.
if elif else 😵
To use the if elif else loop, the following syntax needs to be used :
{{
import random
k = random.randint(0, 100)
}}
<h2>
{{=k}}
{{if k % 2:}}is odd{{else:}}is even{{pass}}
</h2>
Output
This code produces the following output:
<h2>
45 is odd
</h2>
Here a number is randomly taken from 0-100, and then it is cross-checked that the number picked is even or odd. Here, it seems that the number picked is 45.
A pass statement is unnecessary and would be incorrect because it is clear that else ends the first if block. The else block must be explicitly closed with a pass, though.
try except else finally 😵
To use this structure following syntax is needed to be used:
{{try:}}
Hello {{= 1 / 0}}
{{except:}}
division by zero
{{else:}}
no division by zero
{{finally:}}
<br />
{{pass}}
Output
The following output will be produced:
Hello division by zero
<br />
Here in this block of code, we see how any output is generated before an exception occurs, which is executed in the try block. Hello is printed as it precedes the exception.
def return 😵
To use this structure, the following command needs to be used:
We see that the same output is there, but there is a difference between both blocks of codes. Well, the work of function itemize2 is to replace the web2py tag with the HTML piece of code. We can see no '=' in front of the 'itemize2' call. Here the function is directly written into the response instead of returning the text.
The automatic indentation will not work if a function declared inside a view does not end with a return statement.
Frequently asked questions
What is web2py?
web2py is an open-source web application framework that focuses on rapid development by placing a strong emphasis on ease of use and productivity.
web2py works on which framework?
web2py is a very flexible language. Web2py supports all the control structures that are supported by python. Web2py works on the full-stack framework, which is very helpful in developing database-driven web applications.
Who developed web2py?
Massimo Di Pierro developed web2py.
What is a loop?
Loop is just a way to simplify the task of repeating sets of instructions.
In which year was web2py firstly released?
In 2007 first version of web2py was released.
Conclusion
In this article, we extensively discussed web2py, which is an open-source language. Very fast, fluid in nature. We started with the introduction, and then we discussed different types of syntaxes which are used in web2py. In the end, we discussed a few frequently asked questions.