How to print sequence number in python

59 views
Skip to first unread message

paidjoo indo

unread,
Nov 14, 2020, 11:57:58 AM11/14/20
to django...@googlegroups.com
Hello I'm newbie in python, I'm learning looping in python, how to print like in bellow;
Input:5
12345 
2.      4
3       3
4       2
54321
 Thanks guys

mike vickers

unread,
Nov 14, 2020, 2:26:04 PM11/14/20
to django...@googlegroups.com
Python has the enumerate function for these situations.

for i, item in enumerate(items):
     #your print statement here

'i' will give you the index of the list/iterable, and item will give you the item.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAO%3D2VJmeOF5SyMZUFox_hHivTr%3DE%3Dnekj_n7FLOYjGOkPHQrbA%40mail.gmail.com.

Derek

unread,
Nov 15, 2020, 12:08:24 PM11/15/20
to Django users
You can also use range;

>>> for i in range(0, 5):
...     print(i + 1)
... 
1
2
3
4
5
>>>

or in reverse:

>>> for i in range(5, 0, -1):
...     print(i)
... 
5
4
3
2
1
>>> 

MAHA RAJA

unread,
Nov 15, 2020, 2:22:50 PM11/15/20
to django...@googlegroups.com
ordered print
i=1
while (i>=5){
print (i)
i=i+1
}


unordered print

i=5
while (i<=1){
print (i)
i=i-1
  
}





Agripreneur Updates

unread,
Nov 16, 2020, 9:30:17 AM11/16/20
to django...@googlegroups.com
for i in range (5, 0,  -1):
         print (i)

moustapa diongue

unread,
Nov 16, 2020, 11:37:56 AM11/16/20
to django...@googlegroups.com
Merci beaucoup c'est intéressant 

Reply all
Reply to author
Forward
0 new messages