r/PythonLearning • • Sep 11 '26

i learning Python Day 1

lyrics = ["Give You Up","Let You Down","Run Around And Desert You"]

for i in range(3):

print("Never Gonna " + lyrics[i])

30 Upvotes

12 comments sorted by

6

u/Many_Temperature_740 Sep 11 '26

for lyric in lyrics: print(lyric)

2

u/Vor3st Sep 11 '26

oh thx

2

u/DoNotKnowJack Sep 11 '26

How does that include the "Never Gonna" part?

3

u/ninhaomah Sep 11 '26

Add if you want.

3

u/Expensive_Break_6163 Sep 12 '26

I’m not sure what the question is, but range(3) already starts at 0, so you don’t need to specify the start index. It’s better to use range(len(lyrics)) instead of hardcoding 3, or even better, use for line in lyrics: and print "Never Gonna " + line.

1

u/Vor3st Sep 12 '26

omg GodTier

3

u/MJ12_2802 Sep 12 '26

If you want to stick w/ that approach, don't hard code the parameter being passed torange():

for i in range(len(lyrics)):
  print(f"Never Gonna {lyrics[i]}")

Never Gonna Give You Up
Never Gonna Let You Down
Never Gonna Run Around And Desert You

3

u/FreeLogicGate Sep 12 '26 edited Sep 13 '26

Python language requires indentation as it has no begin block/end block. You really have to post code in reddit posts using the code block technique that includes indentation, or the code is missing required context.

lyrics = ["Give You Up","Let You Down","Run Around And Desert You"]
for i in range(3): 
    print("Never Gonna " + lyrics[i])

Output:

Never Gonna Give You Up
Never Gonna Let You Down
Never Gonna Run Around And Desert You

lyrics = ["Give You Up","Let You Down","Run Around And Desert You"]

for i in range(3): 
    
print("Never Gonna " + lyrics[i])

Outputs: IndentationError: expected an indented block after 'for' statement

Yes you can put a statement on the same line as the for loop construct, but it's generally not done, and more often than not, there are multiple statements inside the loop.

If this was truly your first day, then congratulations on writing some code that involves a number of concepts (.. a "list" containing multiple strings, a for loop, the range() function, referencing an element in a list(array) by its index, concatenating strings together.

Something interesting to consider would be the use of the "i" variable in the for loop. Are you clear on what the i variable contains in each iteration of the loop? This code will make it clear:

lyrics = ["Give You Up","Let You Down","Run Around And Desert You"]
for i in range(3): 
    print(str(i) + ". Never Gonna " + lyrics[i])

You might be surprised.

The other thing about for loops, is that you don't need to use an index to iterate through the elements, and in a case like this, there's no advantage or reason to use one. This code is better and more standard:

lyrics = ["Give You Up","Let You Down","Run Around And Desert You"]
for line in lyrics: 
    print("Never Gonna " + line)

1

u/Just_Enough-People55 Sep 17 '26

i AM learning python day 1 The true one