Counter | Sololearn: Learn to code for FREE!

+2

Counter

What counter do?

5/27/2018 12:07:28 PM

Scipacto

5 Answers

New Answer

+20

counter counts the elements (no. of times it is present) in the list ... it returns values in form of dictionary in key : value form just that instead of value it shows the number of times that key is present in the list... check this code u will understand 👇 https://code.sololearn.com/cdMsYGoqfEnF/?ref=app

+16

No that's not the same... it will just print the elements in the list.... U can write that code in code playground and see the output... I guess I miss understood your question... you were asking about counter variable while I explained about counter function... counter in your code is used as a variable to access the elements of the list words... when for loop runs it start from 0 and gets incremented till last element's index... index in list start from 0 ... so for counter = 0 , words[counter] = words[0] = hello and so on till, words[3] = eggs Scipacto

+1

words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1 sure its the same thing?

0

Counters are simple or temporary variables which are used in looping. ex.- printing 10 nos is easy with 10 var's, but printing a series of 10K prime or even or odd, etc. nos is very difficult. To simplify this process counter variables are used. counters may be incremented or decremented according to our need. see this code for more https://code.sololearn.com/ce0v73JDU4nO/?ref=app

0

this is c... not python