Where does the c come from in this excerise?

Screen Link:

Hey there folks :slight_smile:
I am struggling to figure out where the “c” comes from in this excercise?

‘’'new_columns =
for c in laptops.columns:
clean_c = c.strip()
new_columns.append(clean_c)

laptops.columns = new_columns’’’

Hi @lindamacdonalde: c is an iterator in this exercise, it can be arbitrarily defined. It basically represents every element which is iterated through in the list (or dataframe column) laptop.columns. I suppose you are trying to clean data from a column of a dataframe. If not, please provide a screenshot of the question and a link to the question, your code and your output for us in the community to be able to better assist you.

Thanks!

Hi there

thanks for your expedient reply :slight_smile: yes you are correct, I am doing the cleaning excerise. please excuse my ignorance, but as cleaning data is important I just want to wrap my head around it fully - how on earth does Python know what I want it to do if I call the iteration a random name which I haven’t defined before?

HI @lindamacdonalde, I assume you come from a coding background where you learn that iterators do need to be initialised. However, this is automatically done so in python (starts at index 0), unless specified at the range() function.

For example, in the C family of languages, a for loop may look like this:

for (i = 0; i < numbers.length; i ++) {
    Console.WriteLine(numbers[i]);
}

which I took from this website

JavaScript, C, C++, Java, PHP, and a whole bunch of other programming languages all have this kind of for loop. But Python does not .

Python does not have traditional C-style for loops. We do have something that we call a for loop in Python, but it works like a foreach loop.

This is Python’s flavor of for loop:
numbers = [1, 2, 3, 5, 7]
for n in numbers:
print(n)

The website also says that

Do note that there is an option to do so in python as described in this documentation

I quote:

range([start], stop[, step])

  • start : Starting number of the sequence.
  • stop : Generate numbers up to, but not including this number.
  • step : Difference between each number in the sequence.

Also do note that the count of the iterator in python autoincrements everytime the loop executes to move to the next position/index.

Hope this helps!

Hey there masterryan.prof :slight_smile:

My apologies for getting back to late with my reply. Thanks for your thorough reply. In conclusion the “c” can basically just be anything I want?

thanks in advance
Linda

1 Like

Yes @lindamacdonalde. In this case c is used as an iterator. If you do not mind, please mark my previous reply as the solution.

Thanks and Happy Learning!

thanks masteryryan.prof :slight_smile: - basically c (or whatever you choose) is an “on the fly” variable? :slight_smile:

Hi @lindamacdonalde: it is chosen on the fly. Usually I use the variable i for a single loop. If another inner loop is required, I usually use j (cannot be the same iterator variable as the outer loop). But that being said it is totally up to you, the programmer to decide what you want to name the iterator.

Sometimes, to make it more meaningful when others read you code, you might want to name the iterator something that is easy to understand and obvious. For example, if you are iterating through a dataset you may use row in place of c, so that your co-workers and colleagues immediately understand what you are referring to and need not second guess your code.

E.g.

for row in dataset:
    #do this...

Hope this clarifies and happy pythoning!

ahh yes, makes more sense to me to use something meaningful like row thanks a mil :slight_smile:

No worries! Anytime! Enjoy learning Python!