Range function is a built-in function to generate a sequence of numbers. For example, the following will generate a sequence of numbers from 0 to 10
numbers = range(10)
How do you extract these numbers though ? For that you need some kind of an iterator. An iterator is something which can go over a sequence. That is where the for loop comes in. We will see some more examples of the range ( ) function in the next section.
for loop
The syntax for for loop is just as simple as the while loop. It is specifically designed for going over sequences – like a range for example.
for number in numbers :
print ( number )
1
2
3
4
5
6
7
8
9
10
This is consuming a lot of vertical space, right ? By default the print statement always ends with a new line statement ( escape character ‘\n’ ). If you do not want that, you can specify a specific character that the print statement should end with. For example, if we want the print statement to end with a space, do this.
1
2
for number in numbers :
print ( number , end=" ")
1 2 3 4 5 6 7 8 9 10
Ranges are not the only examples of sequences in Python. There are many more examples like Python Lists, Python Strings, Python Tuples, Python Dictionary, Python sets etc. We will see examples of these in the upcoming sections.
https://ajaytech.co/python-loops/#ran...
https://ajaytech.co/python-loops/#for...
На этой странице сайта вы можете посмотреть видео онлайн Python for loop , Python range function | Just enough Python длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Ajay Tech 23 September 2019, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 79 раз и оно понравилось 1 зрителям. Приятного просмотра!