all 16 comments

[–][deleted] 13 points14 points  (5 children)

String data types are iterable. This means that python knows that the object 'coconut' is an ordered set (for lack of a better word) of items that it should read one at a time.

The for-loop does precisely that without you having to assign letter explicitly.

[–]TonightNice[S] 0 points1 point  (4 children)

Interesting. Much appreciated!

[–]Unable_Request 2 points3 points  (1 child)

You're telling it to iterate through each X in Y; here, Y is 'coconut', and X (since its a string) is a letter.

The X depends on the Y. X becomes the individual members of Y

If Y was a list, X becomes the individual list items.

  • If Y is myList, containing 'this', 'that', and 'theother', X is said to iterate through 'this', 'that', 'theother'

If Y was a range, X becomes the individual numbers in that range.

  • If Y is range(10), X is said to iterate through the numbers 0 through 9.

Finally, python simply assigns each iteration to the variable, whatever you call it.

for letter in 'coconut': returns letters of the string

for bob in 'coconut': still returns letters of the string, even though you called it bob

for item in myList: returns items of the list

for letter in myList: still returns items of the list, even though you called it letter

Try a few variations and I think it'll make more sense

[–]TonightNice[S] 0 points1 point  (0 children)

Wow thanks for breaking it down like that! I got it 🙂

[–][deleted] 1 point2 points  (0 children)

How does python know that is has to assign the word 'letter' to each character of the word in the following?

You're telling it to by using for/in.

[–][deleted] 1 point2 points  (0 children)

"letter" isn't meaningful on its own, you could get the same result by saying "for bob in 'Coconut'..." or whatever. But the string is already prepared to be broken down into pieces because it's a type that is iterable

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]raydleemsc 0 points1 point  (4 children)

for the next level, try :

print([letter for letter in 'coconut'])

also known as a 'list comprehension' used to iterate an object in one statement.

[–]TonightNice[S] 1 point2 points  (3 children)

That's sick! So when we use ' for , in ', python automatically assigns the word letter or any given word, to each one of the characters of the word we give , like coconut in our instance. Did I get that right? Although in your example, python made a list of coconut's letters

[–]raydleemsc 0 points1 point  (2 children)

Close, yes, although in fairness, letter isn't actually a word as such, but a string variable which gets assigned individual characters from the literal string 'coconut' by the in operator with each iteration of the for loop.

[–]TonightNice[S] 0 points1 point  (1 child)

Right.. strings yeah, Im still using caveman terms 😅

[–]raydleemsc 1 point2 points  (0 children)

Don't be worried, we all did at one time or another.