Counter is a container included in the collections module.
This approach requires prior knowledge of total number of iterations.
# A C-style way of accessing list elements
cars = ["Aston", "Audi", "McLaren"]
i = 0
while (i < len(cars)):
print cars[i]
i += 1
By default python’s print()
function ends with a newline.
Python’s print()
function comes with a parameter called ‘end’.
By default, the value of this parameter is ‘\n
’, i.e. the new line character. You can end a print statement with any character/string using this parameter.
# This Python program must be run with
# Python 3 as it won't work with 2.7.
Transpose of a matrix is a task we all can perform very easily in python (Using a nested loop). But there are some interesting ways to do the same in a single line.
In Python, we can implement a matrix as nested list (list inside a list).
Each element is treated as a row of the matrix.
For example m = [[1, 2], [4, 5], [3, 6]]
represents a matrix of 3 rows and 2 columns.
First element of the list – m[0]
and element in first row, first column – m[0][0]
.
Contents Creating forms from models ModelForm Field types A full example Validation on a ModelForm Overriding the clean() method Interaction with model validation Considerations regarding model’s error_messages The save() method
- Formsets
- Using initial data with a formset
- Limiting the maximum number of forms
- Formset validation
- Validating the number of forms in a formset
- Dealing with ordering and deletion of forms
- Adding additional fields to a formset
- Passing custom parameters to formset forms
- Customizing a formset’s prefix
- Using a formset in views and templates