# Counting Nucleotides

We are going to jump the regular expression explanation that you can find in the book, and we will go directly to the section that introduces string/list/array manipulation and adapt it to Python. We are going to see two different methods: a “long” and a “short” one. In many places and computer languages you will see that there are different ways of doing the same thing, with advantages and disadvantages. It is up to you to define which methods are better or worse, as this is a very personal matter. Some people prefer the longer way because it might be clearer and easier to understand, or it might be necessary to use it due to code maintainability. Other people would rather use the shorter path because they want to generate code faster. It does not matter the path you select, as long as you get your task done. That’s the key: focus on the end product not on how exactly got there. If you used 10 lines of code more, or 10 less, that’s irrelevant as long as you did what you wanted. Live and learn and someday you will use the even shorter way. So we start with the long way. We want to count the individual number of nucleotides in a sequence, determining they relative frequency. Remember that we read the lines of a sequence file into a list, using `readlines`. But on the long method we will read the file and store the data into a string like

This way it will be easier to “explode” the sequence in separated items. After the “explosion” we can check each item in the list and get our result. The code is below, I will be back after it.

Most of the above code was already covered before. Let’s look at the different stuff, like the “explosion line”

Here we basically transform our string `sequence` into a list, by putting the object type we want before the object we want converted, like we do here

This construction `type_to_convert(to_be_converted)` tells Python to get whatever is inside the parentheses and transform into whatever is outside. Converting the string to a list will get

and transform it into

After “exploding”, we use a `for` loop to iterate over every item in the list and use conditional statements to do the counts. Regarding the counts, we use this operator

that, in C/C++, tells the interpreter to get the value of `totalT` and add 1 to it.