A for loop can visit each value in a collection.
Lists and tuples are visited in order, sets without relying on order, and dictionaries through keys, values, or key-value pairs.
names = ["Ana", "Marko"]
for name in names:
print(name)Each turn through the loop assigns the next collection value to name.
Try changing one value, then explain which part of the collection changed and why.
Keep this in mind: Read the collection operation before predicting the result.
student = {
"name": "Ana",
"course": "Python",
"city": "Novi Sad"
}
for key, value in student.items():
print(key, ":", value)Run the example, then change one value and observe the result.
for value in ("red", "blue"):
print(value)A loop visits list and tuple items one at a time in their order.
for tag in {"python", "django"}:
print(tag)A set can be iterated, but its items do not have reliable positions or a fixed display order.
for key in student:
print(key)
for value in student.values():
print(value)Iterating over a dictionary gives keys. Use values() for values and items() to unpack each key and value together.
