Please help me understand this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Please help me understand this code

for i in range(5): print(“hello!”) Result: hello! hello! hello! hello! hello! Can Someone break down each component of this code and explain It to me, also explaining how It gives the result It does please. Thank you

15th Aug 2019, 6:55 AM
Tanner
Tanner - avatar
13 Answers
+ 12
The for loop performs the specified functions (in our case, print ("hello")) for each element in the range from 0 to 5 (0, 1,2,3,4). Thus, we get the word "hello" 5 times.
15th Aug 2019, 7:04 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
+ 10
Do it: for i in range(5): print(i)
15th Aug 2019, 7:11 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
+ 9
In this case, yes
15th Aug 2019, 7:21 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
+ 7
Błack Jesus😣 that's not correct. In python, range() starts from 0 if you do not add a starting point argument. The way it works: for i in range(5): #i will be 0,1,2,3,4 so 5 times print("hello!") #Output: hello! hello! hello! hello! hello!
15th Aug 2019, 7:02 AM
Paul Grasser
Paul Grasser - avatar
+ 6
Tanner No, it would be 3, 4 The 2nd argument is exclusive.
15th Aug 2019, 7:07 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
Tanner no it would be 3,4
15th Aug 2019, 7:06 AM
Paul Grasser
Paul Grasser - avatar
+ 3
Because for i in range(3,4) i will be 3 then 4
15th Aug 2019, 7:11 AM
Paul Grasser
Paul Grasser - avatar
+ 2
but i got 5 hello,and i got this off the course for python.
15th Aug 2019, 7:02 AM
Tanner
Tanner - avatar
+ 2
so range( 3, 5) would be 3, 4, 5
15th Aug 2019, 7:05 AM
Tanner
Tanner - avatar
+ 2
i tried it and why is i 4
15th Aug 2019, 7:10 AM
Tanner
Tanner - avatar
+ 2
so range() is what defines i
15th Aug 2019, 7:20 AM
Tanner
Tanner - avatar
+ 2
ok i get it now thank you guys so much
15th Aug 2019, 7:28 AM
Tanner
Tanner - avatar
+ 2
FOR every entry of RANGE(5) (0, 1, 2, 3, 4)... Do the following... In this case you PRINT the words "hello!" To the screen for each entry in range. The variable "i" would be your identifying key for any iteration or the value itself depending on circumstance. In this instance you don't use it but it will hold the values of RANGE(5)
16th Aug 2019, 5:24 PM
Joshua Sledden
Joshua Sledden - avatar