For loop in Python
For loop in Python:
The for loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range) or any iterable object. It allows you to execute a block of code for each item in the sequence.
Here's the syntax of a for loop in Python:
Here are a few examples to illustrate how the for loop works:
- Iterating over a list:
Output:
- Iterating over a string:
Output:
- Using the
range()function:
Output:
- Skipping iterations using
continue:
Output:
- Terminating the loop using
break:
Output:
These examples demonstrate the basic usage of the for loop in Python. You can use it to perform various operations based on your requirements.
Comments
Post a Comment