Table of contents
1.
Introduction 🤷‍♀️
2.
Web2py overview 🧐
3.
Basic Syntax😲
3.1.
for in loop😵
3.2.
Output
3.3.
While 😵
3.4.
Output
3.5.
if elif else 😵
3.6.
Output
3.7.
try except else finally 😵
3.8.
Output
3.9.
def   return 😵
3.10.
Output
3.11.
Output
4.
Frequently asked questions
4.1.
What is web2py?
4.2.
web2py works on which framework?
4.3.
Who developed web2py?
4.4.
What is a loop?
4.5.
In which year was web2py firstly released?
5.
Conclusion
Last Updated: Aug 13, 2025
Easy

Basic syntax in web2py

Author Ankit Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Basic syntax in web2py

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. 

Types of different syntax in web2py

for in loop😵

For these loops, the syntax used is as follows

{{items = ['a', 'b', 'c']}}
<ul>
{{for item in items:}}<li>{{=item}}</li>{{pass}}
</ul>

Output

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 😵

If-else image

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:

{{def itemize1(link): return LI(A(link, _href="http://" + link))}}
<ul>
{{=itemize1('www.google.com')}}
</ul>

Output

The above snippet produces the following output:

<ul>
<li><a href="http:/www.google.com">www.google.com</a></li>
</ul>


itemize1 gives back the helper object, which is inserted at the location itemize1 is being called. 

Let’s see another block of code.

{{def itemize2(link):}}
<li><a href="http://{{=link}}">{{=link}}</a></li>
{{return}}
<ul>
{{itemize2('www.google.com')}}
</ul>

Output

The above block of code gives the following output.

<ul>
<li><a href="http:/www.google.com">www.google.com</a></li>
</ul>


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.

Now you must be curious after knowing about web2py and how simple it is to learn. You can visit Basics of Python with Data Structures and Algorithms and Free Python Foundation with DS and Algo and start your journey.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja! 🥷

Thankyou image
Live masterclass