+ 1
How to understand "for", "in", "range", "i" in simple way? (Provide with examples)
I am new to programming and curious about that things, there things looks like complicated like "i:i+1" or range( bla, bla, bla).
4 Antworten
+ 9
Learn python course from sololearn then you can easily understand for loop concept
+ 4
For/in and range are specific to Python, and for works a bit differently in other languages, so just complete the Python course for now and come back if you have any more specific questions about it.
0
It's quite simple.
Let's say, you have 10 sticks, then you want to take each stick in order then put it in your pocket.When you open it, it still have 10 sticks. Which means, inside that pocket, has 10 sticks.
Let's rewrite this example in Python:
pocket = 10
for sticks in range(0,pocket):
print(sticks)
This code will check how many sticks are there in pocket, then print out how many sticks it found immediately after it found one, repeats this condition ten times.
Yes, I know this one is complex for you. Now, let's directly break down the code:
for: repeats the loop range(condition) times. Unlike while, it can't be used for infinite loops.(loops: repeat multiple times)
(var) in range(cond): after for, it needs a var(or str for str stuff),range() allows the var to loop through the condition inside () (It needs to be used within the for loop to work)