Skip to content

Instantly share code, notes, and snippets.

@kaniket7209
Created December 2, 2021 07:39
Show Gist options
  • Save kaniket7209/6f7af23c723e8f36af097191376b8738 to your computer and use it in GitHub Desktop.
Save kaniket7209/6f7af23c723e8f36af097191376b8738 to your computer and use it in GitHub Desktop.
Using loops in Jinja2
1. What happens when a template is rendered?
Answer: When a template is rendered then a template that contains variables and/or expressions is replaced with values and tags, which controls the logic of the template
2. How can we access the values from the response . Show with example
Answer: In order to access the values, one must loop through the array as shown below.
{% for market in market_prices.marketprices %}
{% for key, value in market.items() %}
{{key}}: {{value}}
{% endfor %}
{% endfor %}
3. How can we use loops through dictionaries and lists that are not nested.See the below example and write the solution.
example- markets=[‘Kasubi’, ‘Wandegeya’,'Nakulabye']
Answer: For such a list, the solution would be
{% for elem in markets %}
{{elem}}
{% endfor %}
4. Suppose we have a dictionary response as below. Now rite the modifoes solution for the same.
market_details={
"id": 2,
"wholesale_price": 90000,
"retail_price": 5600,
"date": "2019-09-13T00:00:00.000Z",
"commodity": "Cassava",
"market": "Nakulabye market"
}
Answer: The modified solution would be
{% for key, value in market_details.items() %}
{{key}}: {{value}}
{% endfor %}
1. Which of the following is not used as loop in Python?
a) for loop
b) while loop
c) do-while loop
d) None of the above
Answer: c)
2. Which of the following is False regarding loops in Python?
a) Loops are used to perform certain tasks repeatedly.
b) While loop is used when multiple statements are to executed repeatedly until the given condition becomes False
c) While loop is used when multiple statements are to executed repeatedly until the given condition becomes True.
d) for loop can be used to iterate through the elements of lists.
Answer: b)
3. Which of the following is True regarding loops in Python?
a) Loops should be ended with keyword "end".
b) No loop can be used to iterate through the elements of strings.
c) Keyword "break" can be used to bring control out of the current loop.
d) Keyword "continue" is used to continue with the remaining statements inside the loop.
Answer: c)
4. What keyword would you use to add an alternative condition to an if statement?
a) else if
b) elseif
c) elif
d) None of the above
Answer: c)
5. Can we write if/else into one line in python?
a) Yes
b) No
Answer: a)
6. 14. The ________ statement is a null operation.
a) break
b) exit
c) return
d) pass
Answer: d)
7. Is it neccessary to use endfor and endif if we have used both for loop and if condition?
a) Yes
b) No
Answer: a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment