Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM
24-Sep-2025Basic Syntax
variable→ takes the value of each item in the iterable, one at a timeiterable→ a collection (like list, tuple, string, range, etc.)Examples
1. Loop through a list
Output:
2. Loop with
range()The
range()function generates a sequence of numbers.Output:
3. Loop with start and end in
range()Output:
4. Loop through a string
Output:
5. Using
break(stop the loop early)Output:
6. Using
continue(skip current iteration)Output:
7. Loop with
elsePython’s
forloops can have anelseblock that runs when the loop finishes without abreak.Output:
So,
forloops in Python are very flexible and often used withrange(), lists, dictionaries, and strings.