Repeat Loop For In Python -The Creation Of Repeat Loops In Python
Repeat Loop: What is the whole Python programming language? The for loop in Python is often referred to as a Loop. For is a keyword used as a loop in all programming languages, including Python.
Records show that in the past, programming languages used iterative loops in various structures, which we will briefly discuss.
The ring is limited to a specific number.
The iteration loop in Python is essentially a simple loop with a specific numerical range that has a defined start and end value. The exact Format of the for loop depends on the programming language, but normally, most of the mentality we have about the for loop is as follows:
1 2 3 4 | <span style=“font-size: 16px;”>for i = 1 to 10 <loop body> </span> |
Here, the body of the loop will be repeated 10 times. The variable i will be set to 1 in the first step and to 2 in the second step, and will continue to the end of the loop. This type of iteration loop is used for BASIC, Algol, and Pascal languages.
The loop repeats three phrases.
There is another form of the popular for loop in the C programming language that has three parts:
- Initialization
- The second statement considers a condition for the end of the loop
- The third phrase identifies the action performed at the end of each iteration
The general form of the Command is as follows:
1 2 3 | <span style=“font-size: 16px;”>for (i = 1; i <= 10; i++) <loop body> </span> |
Technical Note: In the C programming language, the i++ operation increments the value of the variable i by 1. This operation is approximately equivalent to i += 1 in Python.
This loop is interpreted as follows:
- Variable i is initially set to 1.
- The loop continues until the variable i has not yet reached a value of 10.
- At the end of each loop, a value is added to the variable i (i++).
Rings with the possibilities of three expressions are very popular because in these three expressions, any state can be established. This circle can be managed in different ways, and in general, it can be said that this type of circle is flexible. It has a lot of simplicity. These types of loops are also more common in C++, Java, PHP, and Perl.
Collection-based repetition loop
These types of loops are mostly used on a set of objects instead of specifying the values of the numerical range:
1 2 3 | <span style=“font-size: 16px;”>for i in <collection> <loop body> </span> |
Each time the loop is repeated, the variable i assigns an object of the set in order. The type of these rings is more abstract and general. Perl and PHP languages support these types of loops, but use the Foreach Keyword instead of the for keyword. If you are also interested in hacking, reading about Python hacking is a good starting point.
for a repeating loop in Python
Of all the rings mentioned above, Python follows only the last ring, the ring based on a set of objects. Implementing an iteration loop in Python is diverse, and you will always find a new way to do so.
You will learn more about the iteration loop in Python and the structure of the for loop in Python, but for now, we will only become a little more familiar with it through the early examples.
The structure of for as an iterative loop in Python is as follows:
1 2 3 | <span style=“font-size: 16px;”>for <var> in <iterable>: <statement(s)> </span> |
Here <iterable> is in the iteration loop in Python as a set of objects or, for example, a list of objects. Statements are executed in the body of the loop for each item of <iterable>. The variable var allocates an iterable value each time the loop is repeated.
Here is an example of a repetition loop in Python:
1 2 3 4 5 6 7 8 9 | <span style=“font-size: 16px;”>>>> >>> a = [‘foo, ‘bar’, ‘baz’] >>> for i in a: ... print(i) ... foo bar baz </span> |
In this loop, the iteration in Python <iterable> is the same as list a, and the variable <var> is i. Each time the loop of variable i is repeated, it continuously assigns some of the items in list a, then the print () function displays the values ’foo, ‘bar’, and ‘baz’, respectively. For a repeat loop, this is a Python method used to process items in the iteration step.
But what exactly can be an iterable loop in Python? Before we look at most of the loops, it’s a good idea to delve deeper into what is meant by iterable loop loops in Python. In the following article, we suggest that you learn the Python programming language better.
Iterable
In the iteration loop in Python, an iterable is an object that can be used in iteration. The term is used as follows:
- Adjective: An object can be described as a duplicate.
- Name: An object is specified as a duplicate.
If an object is iterable, it can be called in the iter () function in Python, which returns something called an iterator.
Each of the objects in the example above is iterable and returns an iterator or iterator type when passed to the iter() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <span style=“font-size: 16px;”>>>> >>> iter(‘foobar’) # String <str_iterator object at 0x036E2750> >>> iter([‘foo’, ‘bar’, ‘baz’]) # List <list_iterator object at 0x036E27D0> >>> iter((‘foo’, ‘bar’, ‘baz’)) # Tuple <tuple_iterator object at 0x036E27F0> >>> iter({‘foo’, ‘bar’, ‘baz’}) # Set <set_iterator object at 0x036DEA08> >>> iter({‘foo’: 1, ‘bar’: 2, ‘baz’: 3}) # Dict <dict_keyiterator object at 0x036DD990> </span> |
On the other hand, in the following example, these objects returned to the iter function are not iterable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <span style=“font-size: 16px;”>>>> >>> iter(42) # Integer Traceback (most recent call last): File “<pyshell#26>”, line 1, in <module> iter(42) TypeError: ‘int’ object is not iterable >>> iter(3.1) # Float Traceback (most recent call last): File “<pyshell#27>”, line 1, in <module> iter(3.1) TypeError: ‘float’ object is not iterable >>> iter(len) # Built-in function Traceback (most recent call last): File “<pyshell#28>”, line 1, in <module> iter(len) TypeError: ‘builtin_function_or_method’ object is not iterable </span> |
All the data types you have encountered so far that are collection types are iterable. These types of data that are iterable include strings, lists, tuples, dictionaries, sets, and fixed lists.
But this data is not the only type that you can use in the iteration loop in Python. There are many objects designed and defined in Python that you can use as an iterable in the Python loop. For example:
An open File in the iteration loop in Python can be used as an iterable. You can use an open File as an iterable and read objects from it, and use it in your iterations after you have learned enough about the iteration loop in Python.
Virtually any object in Python can be used as an iterator in the iteration loop. Even user-defined objects can be designed to be used instead of iterators (in this article, you will learn how we can benefit from object-oriented programming in Python).
Iterators in the for loop in Python
You now have a good idea of what data types are known as iterable, and you understand how to use the iter () function to get an iterator. Now that we have access to an iterator, what can we do with it?
In the iteration loop in Python, an iterator is essentially a value generator that assigns sequential values to the corresponding iterable object.
The constructor function next() in the iteration loop in Python is used to assign the next value to the iterator.
Here is an example using the same list above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <span style=“font-size: 16px;”>>>> >>> a = [foo, ‘bar’, ‘baz’] >>> itr = iter(a) >>> it <list_iterator object at 0x031EFD10> >>> next(itr) ‘foo’ >>> next(itr) ‘bar’ >>> next(itr) ‘baz’ </span> |
In this example, a is an iterable list, and itr is an iterator that was first converted to an iterator by the iter() function. Each of the following commands (itr) returns the next value in this list in order.
Notice how a repeater maintains its inner state. The repeater realizes that the values are predetermined, so when you call the following function in the iteration loop in Python, the repeater realizes that it must return the next value.