In this Python Django tutorial, we will learn How to compare two integers in Python Django. And we’ll also see different examples related to this. These are the following topics that we are going to discuss in this tutorial.
- Comparison Operators in Python
- How to compare two integers in Python Django
- Compare two integers in view
- Compare two integers in Templates
Comparison Operators in Python
In this section, we’ll explore what a comparison operator is and the different kinds of comparison operators available in Python.
In Python, a comparison operator is also known as a relational operator. Comparison operators are used to compare the values of two operands and returns True or False depending on whether the condition is satisfied or not.
The comparison operators available in Python are listed below.
Operator | Operator Name | Description |
---|---|---|
== | Equality | The condition becomes true if the values of two operands are equal. |
!= | Inequality | When the values of two operands differ, the condition is true. |
> | Greater than | If the left operand’s value is greater than the right operand’s value, the condition is true. |
>= | Greater than or Equal to | The condition is true if the value of the left operand is larger than or equal to the value of the right operand. |
< | Less than | If the left operand’s value is less than the right operand’s value, the condition is true. |
<= | Less than or Equal to | The condition is true if the value of the left operand is less than or equal to the value of the right operand. |
Read: If statement in Django template
How to compare two integers in python Django
It’s time to learn how to show Python program output on an HTML page using Django till now we’ve only known how to represent it in a console or terminal. So in this section, we will learn to compare two integers in python using Django.
There are two ways to compare two integers using Django:
- Using Views
- Using Template
Using Views: Views are Python functions or classes that receive and respond to web requests in the Django framework. A plain HTTP response, an HTML template response, or an HTTP redirect response that sends the user to another page can all be used as responses.
Using Template: Django templates use tags and filters to define a language that’s similar to Python—but isn’t Python. Tags are similar to the keywords and functions in the template language. Keywords and functions in Python offer control flow and the tools on which your code is constructed. Inheritance, conditional operation, looping, comments, and text handling are all provided by Django’s built-in tags.
Compare two integers in view
In this section, we’ll learn how to use Django’s view to compare two integers. Let’s, clarify our concepts by using various examples.
I’ll show you the project’s urls.py and app’s urls.py file before I start the demonstration. As it remains the same in all of the examples.
The PythonGuides urls.py file:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls'))
]
This means that when you open your project in the browser, it will take you to your home.urls.
Now, see the home urls.py file:
from django.urls import path, include
from home import views
urlpatterns = [
path('', views.home, name='home')
]
It moves to the views.py file of the home app.
Example #1
In this example, we are using the equality operator to compare two integers.
The views.py file:
from django.http import HttpResponse
# Create your views here.
def home(request):
x = 6
y = 6
if(x == y):
html = "<html><body><b>Yes, X is equals to Y </b></body></html>"
return HttpResponse(html)
else:
html = "<html><body><b>No, X is not equal to Y</b></body></html>"
return HttpResponse(html)
We define two integers, x & y, and then use the equality operator to compare them. We render the output on the HTML page by using the if-else statement in Python.
Now, if we run the development server and move to the mapped URL, we will get the following output.
Example #2
In this example, we are using the inequality operator to compare two integers.
The views.py file:
from django.shortcuts import render
# Create your views here.
def home(request):
x = 10
y = 12
result = (x != y)
return render(request, 'home.html', {'result':result})
We define two integers, x & y, and then use the inequality operator to compare them. We render the result on the HTML page.
The home.html page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PythonGuides</title>
</head>
<body>
<p>
<b> X is not equal to Y: </b>{{result}}
</p>
</body
</html>
In the home.html file, we are just using the variable to get the result. In the end, we will get the following output.
Example #3
In this example, we are using the greater than operator to compare two integers.
The views.py file:
from django.shortcuts import render
# Create your views here.
def home(request):
x = 10
y = 12
result = (x > y)
return render(request, 'home.html', {'result':result})
We define two integers, x and y, and then use the greater than operator to compare them. We render the result on the HTML page.
The home.html page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PythonGuides</title>
</head>
<body>
<p>
<b> X is greater than Y: </b>{{result}}
</p>
</body
</html>
In the home.html file, we are just using the variable to get the result. In the end, we will get the following output.
Example #4
In this example, we are using the greater than or equal to an operator to compare two integers.
The views.py file:
from django.shortcuts import render
# Create your views here.
def home(request):
x = 1500
y = 1500
result = (x >= y)
return render(request, 'home.html', {'result':result})
We define two integers, x & y, and then use the greater than or equal to an operator to compare them. We render the result on the HTML page.
The home.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PythonGuides</title>
</head>
<body>
<p>
<b> X is greater than or equal to Y: </b>{{result}}
</p>
</body
</html>
In the home.html file, we are just using the result variable to get the output. In the end, we will get the following output.
Read: Convert HTML page to PDF using Django in Python
Compare two integer in template
In this section, we’ll learn how to use Django’s template tag to compare two integers. Let’s, clarify our concepts by using various examples.
Syntax:
{% if variable boolean_operator value %}
Statement To Print
{% endif %}
I’ll show you the project’s urls.py and app’s urls.py file before I start the demonstration. As it remains the same in all of the examples.
The Project’s urls.py file:
Here our project name is PythonGuides.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls'))
]
This means that when you open your project in the browser, it will take you to your home.urls.
Now, see the home urls.py file:
from django.urls import path, include
from home import views
urlpatterns = [
path('', views.home, name='home')
]
It moves to the views.py file of the home app.
Example #1
In this example, we are using the equality template tag to compare two integers.
The views.py file:
from django.shortcuts import render
# Create your views here.
def home(request):
x = 100
y = 150
return render(request, 'home.html', {'x':x, 'y':y})
Firstly, we will create a view that will redirect to an HTML page. In the views.py file, we define the numbers and redirect the numbers to the home.html page.
The home.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PythonGuides</title>
</head>
<body>
<p>
{% if x == y %}
{{ x }} is equal to {{ y }}
{% else %}
{{ x }} is not equal to {{ y }}
{% endif%}
</p>
</body
</html>
Here, we add an equality boolean operator in the template using {%if%} tag. It tag evaluates the variables, if the condition is “true” the contents of the block are output.
The output is as follow:
Example #2
In this example, we are using the less than template tag to compare two integers.
The views.py file:
from django.shortcuts import render
# Create your views here.
def home(request):
x = -10
y = 10
return render(request, 'home.html', {'x':x, 'y':y})
To begin, we’ll make a view that redirects to an HTML page. We declare the numbers in the views.py file and redirect them to the home.html page.
The home.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PythonGuides</title>
</head>
<body>
<p>
{% if x < y %}
{{ x }} is less than {{ y }}
{% else %}
{{ x }} is greater than {{ y }}
{% endif%}
</p>
</body
</html>
Here, we add less than the boolean operator in the template using {%if%} and {%else%} tag. If the tag evaluates the variables, If the condition is “true” the contents of the block are output, otherwise, else tag contents block is output.
Example #3
In this example, we are using the less than or equal template tag to compare two integers.
The views.py file:
from django.shortcuts import render
# Create your views here.
def home(request):
x = 150
y = 150
return render(request, 'home.html', {'x':x, 'y':y})
To begin, we’ll make a view that redirects to an HTML page. We declare the numbers in the views.py file and redirect them to the home.html page.
The home.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PythonGuides</title>
</head>
<body>
<p>
{% if x <= y %}
{{ x }} is less than or equal to{{ y }}
{% else %}
{{ x }} is greater than or equal to {{ y }}
{% endif%}
</p>
</body
</html>
Here is the output:
Also, take a look at some more Django tutorials.
- Get URL parameters in Django
- Python Django app upload files
- Python Django length filter
- Create model in Django
- Get Current time in Django
In this Django tutorial, we discussed how to compare two integers in Python Django. Also, we discussed the following lists of topics:
- Comparison Operators
- How to compare two integers in python Django
- Compare two integers in view
- Compare two integers in Template
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.