Can I have a further explanation of the "for" loop?


#1

I don't think I really understand "for" loops. Codecademy's explanation isn't good enough. I honestly have no clue how they work. Codecademy says: "A variable name follows the for keyword; it will be assigned the value of each list item in turn." I don't understand it. Could I have some further clarification?


11. Making a Purchase
Using strings in lists in functions
#2

These are some resources you can go through..

https://learnpythonthehardway.org/book/
https://python.swaroopch.com/

also when you open the front page of Python course,they suggest some pages to read ..

I hope it helps you!


#3

Uh generally I think you're one of the most helpful people here, but the index isn't that friendly. Maybe include a link for the second part. People use this place to learn because it doesn't require streaming. Please don't be classist. Thank you and good wishes.


#4

@epicconnundrum
Okay I'm giving you exact indices..

https://python.swaroopch.com/control_flow.html
https://learnpythonthehardway.org/book/ex32.html
https://wiki.python.org/moin/ForLoop

This topic has several different aspects and I believe no one can actually create a post that covers almost all aspect of for loop..

But I give a take..
for loops--

When we have some task that need to be done repeatedly then we use our programming tool called 'loops'

For loop is a loop that is usually used to do a task if we already know how many times we have to repeat it.

For loop and string

x = "GreenTree"
and I want to loop over it,I'd do this..

for i in x:
    print i

It will print..

G
r
e
e
n
T
r
e
e

Now understand how it happens..
for - It is a keyword in python that indicates loop is about to start.
i is a general variable that takes one character from string and stores it temporarily at every repetition
.
How many repetition it will does?
Same as number of characters of string. (in this case 9 times)

at first looping -- i has value of 'G'
at second looping -- i has value of 'r'
at third looping -- i has value of 'e'
at fourth looping -- i has value of 'e'
at fifth looping -- i has value of 'n'
at sixth looping -- i has value of 'T'
at seventh looping -- i has value of 'r'
at eighth looping -- i has value of 'e'
at ninth looping -- i has value of 'e'

so value of i dynamically changes,You can see it,It takes one value, prints it,then in next loop it forgets the last value and stores new value again and prints it and so on until it prints all the characters of string

You can use any name instead of i, It can be j, k or green.. anything(but valid variable's name).

in - in is a keyword that binds every single character(at every looping) of string with our iteration variable i.

The loops iterates(looping) till it goes through each character of our string.

Same will be true for list,tuple or even dictionary,range(a,b,c) (Its obviously a list in Python 2.0)

For loops and list..

name = ["G", "r" , "e" , "e", "n"]

for n in name:
	print n

It will print..

G
r
e
e
n

Also check these links.,
http://pythoncentral.io/pythons-range-function-explained/
https://www.programiz.com/python-programming/for-loop

I hope it helps!
(Tho I'd strongly suggest you to search about it online,there're lots of cool video tutorials and separate articles on it)


Printing out works, but response said it didn't
#5

I fail to see any signs of discrimination in his post. Those were helpful links. The second part cannot be linked to since it is in his dashboard. If you do not have a Pro account they may not be in your dashboard.

Here are the external links:

https://stackoverflow.com/questions/743164/emulate-a-do-while-loop-in-python

http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html

Segue to BASIC:

LET A = 1
LET B = 10
FOR A TO B STEP 1
PRINT A * B
NEXT A

Output

10
20
30
40
50
60
70
80
90
100

To demonstrate this in Python:

b = 10
for a in range(b):
    print (a + 1) * b

Output

10
20
30
40
50
60
70
80
90
100

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.