# Why do I need to create a new variable (like count?)

When creating a function and extract a particular integer or string from the list, why is it necessary to create a new variable? Below is the code:

def divisible_by_ten(nums):
count = 0
for number in nums:
if (number % 10 == 0):
count += 1
return count

#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))

1 Like

count = 0

thatās where i get stuck

The loop is iterating values in the list. `count` is tracking their positions in the list, by index. Recall that lists are zero-indexed using positive integers (and zero) in sequence. The highest index is always one less than the length of the list.

4 Likes

Oh ok. I got it! Yes thank you

1 Like

Is there any difference if I run my function on a (list) instead of (nums)? Under what circumstances could it matter? Thank you.
def divisible_by_ten(nums): or def divisible_by_ten(list):

1 Like

`nums` is the local variable referencing the list literal that is passed into the function. The same would apply if a list object is passed in by name. What name we give it locally is arbitrary, but it should describe the list, rather than just call it `list`. At least `nums` tells us something about the expected contents of the input.

2 Likes

Hello,
Given what the lessons are, how are we supposed to figure out that weāre supposed to create a new variable ācountā, to use ānumberā in nums etcā¦
I feel like switching from being over protected to being thrown into the wild without a noticeā¦

Not sure I follow your question, though I sense some vexation. When it becomes necessary to calm down, nothing does more good than a kilometer walk.

I always have music going through my head so donāt need a walkman, just the beat of my steps. Steps are taken one at a time, and are generally quite in sync with one another, hence a beat.

The general sameness of each step (or pair of steps to arrive at the same foot) can be thought of as a unit measure of time, and of distance. Once we know the length of each step we may count off the number of steps and compute the overall distance. Ditto for time. Elapsed time divided by number of steps gives time per step. Iām around allegretto/allegro time (~120 steps a minute over which I cover 100 m).

The act of counting is really just a finite number of additions of `1` to arrive at a total count. Bring on the variable, `count`, which by the above, says what it represents. The number of items counted.

Counting generally starts from a position of neutrality; that is from a point that has no value that may affect the outcome. In arithmetic we call this the additive identity and its value is 0.

If we construct a sequence of additions,

``````0 + 1 + 1 + 1 + 1 + 1 + 1 + 1
``````

with the first term set to zero, the final outcome will be the same as when that term is not included.

What we see above is counting, so `count` would be the logical variable to reference its present value.

A variable name `nums` is very suggestive of a list of numbers by the plurality inferred. One with the name `number` will infer singularity. This is what is meant by semantic naming convention; we give concise and explicit meaning to our variable names.

`count` implies a number, as weāve discussed, but `number` does not imply a count. Itās just number plucked out of somewhere. We get that meaning from its name.

Now given a list of numbers at random, this exercise expects us to count the number of them that are divisible by 10. We start with a `count` of zero, as discussed, and then iterate over the list, dividing each by 10 and seeing if there is a remainder or not. If not, we add `1` to our `count`.

From here one expects youāll do smashingly on the exercise, and have some food for thought to carry forward. Happy coding!

P. S.

In case you were wondering, the number that we ADD to a number that gives us the additive identity is called the additive inverse. Can you guess what it is?

Hint: Above we have a count of `7`. What number will take us back to zero if we add it?

4 Likes

Thanks for the explanation on the count = 0. For purposes of memory retention what specifically should I use to help me remember this, for example is it because the word āforā is used and typically or I guess always?? precedes a loop and the function contains a variable name ānumsā? Iām just having a hard time coding from scratch without looking answers up. The rest of the code makes sense to me. Why doesnāt python program have a build in a default understanding that index positions start at 0ā¦? Ughā¦

def divisible_by_ten(nums):
count = 0
for number in nums:
if (number % 10 == 0):
count += 1
return count

That is absolutely the default!

The need for count in this function has nothing to do with indexing, nor specifically with the fact that a for loop is being used. count is simply a variable whose job it is to keep track of how many times something happens, in this case, how many numbers are divisible by ten.

In some versions of C, I believe, (and maybe in other languages), simply declaring an int variable sets its value at zero, but that is a different issue than indexing, and in Python, we donāt declare variables, we just jump right in with them. In this case we wanted count to be initialized at zero, but it could have been any number, perhaps a value passed by another function.

For future reference, if you are going to perform an operation on a variable, the variable must have been previously defined (assigned a value.) Otherwise, youāll get a NameError.

`count` is not an index, just a running tally. The `for` loop above is not looking at indexes, but values. I like to call it a read-only loop since we are not manipulating the data, only polling it and checking its parity. `count` is how many even numbers there are in the list, `nums`.

Whenever we use `for` it is on an iterable such as a list, a range, or one of the other data structures, including strings. We can access the iterable by index, or by value. Above, `number` is one value at a time from `nums`.

Hello everyone

I generally have to agree with motud

Following every exercise so far I really had trouble doing the lists so far. Nowhere have I learned how I am supposed to find out where to add ācountā and what exactely it means. I gave up this exercise becauce I simply was not able to figure out how I am supposed to add what line of code.

To be more simple: How do I know when to add ācountā and all that? Did I miss something during the learning sessions?
The learning phase was fairly OK and easy to get through but right after that I felt like I was learning to swim in the childrens pool a moment ago and now I am thrown into a shark tank with a livewest made out of ham.

If someone can explain this whole thing to me like you would explain to a child that fire is bad thing to touche I would be really gratefull.

`count()` is an attribute of all class of objects that are iterable, meaning they can be iterated (taken one element at a time). Anything that can be written with an index, is iterable.

``````"string"
['list']
('tuple')
{'set'}
``````

Each of these belong to a class of object in Python, `str`, `list`, `tuple`, `set` respectively, and they all have a `count` method.

When we wish to apply a method, we write the name of the object, followed by a dot, followed by the name of the method which is invoked with an argument list.

``````    my_string = "supercalifragilisticexpialadocious" (sp?)
num_of_s = my_string.count('s')
#            object.method(argument)
print (num_of_s) = 3
``````

OK. So far so good.

Now let me ask you another thing. In this line of code ācountā indicates the start of theā¦ well, count Iād say. With the line ācount = 0ā we tell the loop to start counting from 0.

Or did I get it wrong?

In that example, `count` is a variable, not a method as shown earlier. The idea here would be to track the number of times a given condition exists or a given operation is carried out.

``````count = 0
for x in my_string:
if x == 's':
count += 1
print (count)    # 3``````
1 Like

O.K.

I think I get it. Thanks for your time by the way. Really appreciate it.

1 Like

Hi,
So, I did this one in a slightly different way, I do realize there is more then 1 solution to any problem, but, is this code ok?

def divisible_by_ten(nums):
num =
for n in nums:
if n%10==0:
num.append(n)
return(len(num))