-1

Please explain to me how the variable, "count", automatically associates itself with each index of the string, "Hello!"

greeting = 'Hello!'
count = 0

for letter in greeting:
    count += 1
    if count % 2 == 0:
        print(letter)
    print(letter)

print('done')

Basically, the following questions ask about the amount of times each letter of the string prints. After checking the discussion board, I found out that the logic is that the output is H = [1], e = [2], l = [3], l = [4], o = [5], ! = [6]. The thing is, I don't understand why this happens.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Sean D
  • 107
  • 1
  • 2
  • 7
  • 3
    What do you mean by "automatically associates"??? It simply counts up from 0. – juanpa.arrivillaga Jun 13 '17 at 23:24
  • `H = [1]` ??? What? Again, each iteration of the loop the `count` variable gets incremented by 1. Simply by adding 1 to it the first line of the loop body. – juanpa.arrivillaga Jun 13 '17 at 23:29
  • `for ltr in greeting[::2]:print(ltr)` would print every oher letter ... also ... its not very clear what the point of this program is – Joran Beasley Jun 13 '17 at 23:31
  • So, a Python `for` loop is like a Java `for-each` loop, if that helps you understand. – juanpa.arrivillaga Jun 13 '17 at 23:32
  • Could you please edit your question to state what exactly your asking. Your true question is confusing because you are saying things like "automatically associates" and "H = [1]". – Christian Dean Jun 13 '17 at 23:37
  • @juanpa.arrivillaga a for loop is a standard concept in progamming generally; the logic behind any for loop [regardless of the language] is the same. – Rachel Gallen Jun 14 '17 at 00:55
  • @SeanD Sean, there is documentation in the beta docs about python, and loops in general. The docs are a good place to start if you're starting out.. – Rachel Gallen Jun 14 '17 at 00:57
  • @RachelGallen I would agree that the idea is the same. But [a distinction](https://en.wikipedia.org/wiki/For_loop#Kinds_of_for-loops) is typically made between "traditional" C-like for-loops and iterator-based "for-each" loops. A C-style loop is basically a convenient way to write while-loops. A for-each loop involves another layer of abstraction, and requires the concepts of iterables and iterators. Some languages live [Java support both constructs](https://stackoverflow.com/questions/5851025/why-are-there-two-different-kinds-of-for-loops-in-java), and they considered are distinct. – juanpa.arrivillaga Jun 14 '17 at 05:37
  • @RachelGallen I don't mean to imply that you require tuition. I merely point to wikipedia as evidence that a distinction exists, the same with my link to the other stack-overflow question. I often see on the Python tag people come from Java who don't quite grok the difference between the the two styles of for-loops. Sometimes, explaining that the Python construction uses the equivalent to a Java "for-each" loop, sometimes called an "enhanced for-loop," helps in creating understanding. – juanpa.arrivillaga Jun 14 '17 at 05:52
  • @SeanD fyi, here are some better online articles that cover for loops, and the numerous variations of for loops. The last link is in a javascript guide, but the concepts are well explained. Not all of the constructs outlined in the last link are applicable in Python, but they're good to know..http://www.cs.utah.edu/~germain/PPS/Topics/for_loops.html , http://www.python-course.eu/python3_for_loop.php ,https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration – Rachel Gallen Jun 14 '17 at 06:32
  • @RachelGallen thanks for the links. Despite my vaguely explained and misunderstood question (due to my inexperience), I appreciate your effort of providing me some references – Sean D Jun 15 '17 at 01:29

2 Answers2

1

Count does not associate with each index of the string.

'Hello' is a string that is composed of multiple characters at various indices:

`'Hello!'[0] = 'H'`
`'Hello!'[1] = 'e'`
`'Hello!'[2] = 'l'`
`'Hello!'[3] = 'l'`
`'Hello!'[4] = 'o'`
`'Hello!'[5] = '!'`

In the for loop, you are incrementing the variable count each time. Thus, in the first iteration, count=0. In the second iteration, count=1, and so on. Your loop is only checking to see if count is divisible by 2. If it is, then it prints out the letter corresponding to its value a second time. Thus, your code would print out

H
e
e
l
l
l
o
!
!
done
victor
  • 1,573
  • 11
  • 23
  • 2
    _"Thus, in the first iteration, count=0[...]"_ - No, that is not correct. He immediately increments `count` at the start of his `for` loop. So by the time `count` is used, its value is `1` not `0`. And on the second iteration the value is `2` not `1`, ad infinitum. – Christian Dean Jun 13 '17 at 23:39
  • @ChristianDean That is true, but I was trying to simplify the explanation by explaining the value of `count` before the loop starts, or at the beginning of the loop. But yes, thank you for the clarification! Because he increments `count` at the beginning of each loop iteration, it starts with a value of 1 in the first iteration, 2 in the second, and so on. – victor Jun 13 '17 at 23:41
0

You asked:

Please explain to me how the variable, "count", automatically associates itself with each index of the string, "Hello!"

But in your codes, there's no need to use an if statement. And you should add the index or count to near of the item of the string. Simply the codes should be something like:

greeting = 'Hello!'
count = 0
for item in greeting:
    print("item={}, index={}, count={:d}".format(item,greeting.index(item),count))
    count += 1

This will print out:

item=H, index=0, count=0
item=e, index=1, count=1
item=l, index=2, count=2
item=l, index=2, count=3
item=o, index=4, count=4
item=!, index=5, count=5

With the above codes you can see that the count is automatically associates with the each index of the string "Hello!". However when you set the count value for example to 1, the 1'st index (Index0) string associates with when the count=1 then multiply it's value until the end of the index with the for loop.

In "Hello!" string there is 6 items. The first item index always starts with 0. However in order to print a more beautiful display, like the 'first item, second item, third item...' you can add a count variable or you can use enumerate function like the below examples:

greeting = 'Hello!'
count = 1
for item in greeting:
    print("item={}, index={}, count={:d}".format(item,greeting.index(item),count))
    count += 1

greeting = 'Hello!'
for count,item in enumerate(greeting,1):
    print("item={}, index={}, count={:d}".format(item,greeting.index(item),count)) 

The last two codes will give you the same results which are:

item=H, index=0, count=1
item=e, index=1, count=2
item=l, index=2, count=3
item=l, index=2, count=4
item=o, index=4, count=5
item=!, index=5, count=6
dildeolupbiten
  • 1,314
  • 1
  • 15
  • 27