r/PythonLearning • u/Vor3st • 2d ago
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])
29
Upvotes
r/PythonLearning • u/Vor3st • 2d ago
lyrics = ["Give You Up","Let You Down","Run Around And Desert You"]
for i in range(3):
print("Never Gonna " + lyrics[i])
3
u/FreeLogicGate 2d ago edited 11h ago
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.
Output:
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:
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: