Skip to content

Arithmetic Expression

  • Upto now we have seen variables assigned to string values.
  • Jinja2 supports other data types too.
  • Let's use integers to perform a arithmetic calculation.
  • Adding {{ X }} and {{ Y }} equals {{ X + Y }}.
  • The values assigned to the variables are integers.
  • Below example describes it.

python code for Arithmetic Expressions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from jinja2 import Template

template = "Adding {{ X }} and {{ Y }} equals {{ X + Y }}"

data = {
    "X": 7,
    "Y": 8,
}

j2_template = Template(template)

print(j2_template.render(data))

Output

  • Adding 7 and 8 equals 15
Authors(Git):Devi(96.77%), Thejesh GN(3.23%)