Mastering For Loops in Python
Mastering For Loops in Python
Python
This presentation will guide you through the use of for loops in
Python. For loops are essential for traversing sequences. We'll
explore syntax and examples. Grasp how to iterate lists, strings,
tuples, and dictionaries effectively.
by Aditya Pandey
For Loop Syntax and Traversal
Basic Syntax Example Output
The for in loop is a powerful iteration mechanism in Python, allowing you to systematically process each element in
a sequence. This example demonstrates iterating through a range of numbers, printing each index from 0 to 3.
Iterating Through Data Structures
Lists
Tuples
Strings
s = "Geeks"
for i in s:
print(i)
Dictionaries
d = dict({'x':123, 'y':354})
for i in d:
print("%s %d" % (i, d[i]))
For loops can traverse various data structures. The code snippets demonstrate iteration. Iteration is possible through lists,
tuples, strings, and dictionaries.