The for loop in Python
Let us look at the file for_loop.py
for i in range(1,4):print(i)
python for_loop.py
1
2
3
Everything works fine. Note that only i from 1 to 3 was printed. Python did not print 4.
One-line scripts are inconvenient to read. Usually people try to write each action on a separate line.
for i in range(1,3):
print(i)
python for_loop.py
1
2
3
If you write the code without at least one space before print
for i in range(1,4):
print(i)
then the code will not work. You will get an error
File "/home/andrei/python/for_loop.py", line 2 print(i) ^ IndentationError: expected an indented block
So you need to put a space before print
Example with _
retries=3
retries_ = abs(int(retries))
for _ in range(retries_ + 1):
print(_)
1
2
3
4
Article author: Andrei Olegovich