Python for loop , Python range function | Just enough Python

Publié le: 23 September 2019
sur la chaîne: Ajay Tech
79
1

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...


Sur cette page du site, vous pouvez voir la vidéo en ligne Python for loop , Python range function | Just enough Python durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Ajay Tech 23 September 2019, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 79 fois et il a aimé 1 téléspectateurs. Bon visionnage!