Skip to content
← Back to Community
How to count in python?
Profile icon
Richard14

How can I count in python please help me

Voters
Profile icon
Khan-deboer
Profile icon
JannatulChowdhu
Profile icon
ellaeo
Profile icon
John_WardWard
Profile icon
Richard14
Comments
hotnewtop
Profile icon
Geocube101

What do you mean 'count'? Like a for loop?

Profile icon
WikingGaffa

You need to be more specific. What is it you want to count?
There is propably better ways to do it, but here is two examples

mylist = ["item1", "item2", "item3"] mytext = "item1 item2 item3" #count instance in text print(mytext.count("item1", 0, len(mytext))) #count instance in list counter = 0 for i in mylist: if i == "item2": counter += 1 print(counter)
Profile icon
27eschwarz

How to set count to 0 at the start of your program?

Profile icon
DakshilGiridhar

yes

Profile icon
John_WardWard

@Richard14
There is an easy way to do this: create a variable called count_up_to(10 in my piece of code at first:
count_up_to=10

Then create a for loop which will print x, as shown below.

for x in range(count_up_to): print(x)

If you would like it to count backwards from 0 , multiply x by -1 shown below:

for x in range(count_up_to): print(x*-1)

Remember to add 1 to the variable count_up_to more than what you would like the console to count up to , as it will count from 0 inclusive.

Here is an example repl if you would like to know more.