Python Forum
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for loop the difference
#1
Hey guys,

I was reading python learn-stuff at https://www.programiz.com/python-programming/for-loop
and I was wondering: is the suggested code logical?

# Program to iterate through a list using indexing

genre = ['pop', 'rock', 'jazz']

# iterate over the list using index
for i in range(len(genre)):
	print("I like", genre[i])
If I had to code it: I would (a newbie at python!):
# Program to iterate through a list using indexing

genre = ['pop', 'rock', 'jazz']

# iterate over the list using index
for i in genre:
	print("I like", i)
I don't understand why range+len is needed. Perhaps someone can clearify?
Reply


Messages In This Thread
for loop the difference - by 3Pinter - Jan-10-2019, 01:59 PM
RE: for loop the difference - by buran - Jan-10-2019, 02:11 PM
RE: for loop the difference - by 3Pinter - Jan-10-2019, 02:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between word: and word[:] in for loop zowhair 2 3,626 Mar-03-2018, 07:24 AM
Last Post: zowhair

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020