In this section, we'll introduce how to loop in Python and how to apply loops to explore the content of the layer loaded in the previous paragraph. Write the following snippet in the Python Console, taking special care with the code indentation:
for feature in layer.getFeatures():
print("Feature %d has attributes and geometry:" % feature.id())
print(feature.attributes())
This will print a pattern like the following:
Feature with id 68 has attributes and geometry:
[69, 9, 12.0, 'HAINES', 'Other']
The layer.getFeatures() method returns an object that can be iterated inside a for Python instruction, getting a QgsFeature instance for every loop. The feature.attributes() method returns a list (inside the brackets, []) of the integer and Unicode strings (the u' values). The feature...