Input datatype error
Question: I have a program which takes a string and converts it into list which looks
like this - `['CTTC', 'CGCT', 'TTTA', 'CATG']`. (its actually a lot longer
than this). Now I need to find how many of these list elements have a `C or A
or T or G` as its 1st letter. This needs to be taken from the terminal i.e.
using the `input` function.

Now as far i know, in python 3.2 the datatype of input function is by default
taken as a string (`str`) and not like an integer (`int`) (can be seen by
using `isinstance`). However since I am using a college server, the python
version is older (i think 2.7 or later but below 3.0). In this case, when I
use the input function to ask the user to choose a letter- `initial =
input("Choose a letter:")`, and when I enter any letter (A,T,G,or C) it gives
me an error `NameError: name 'C' is not defined`. When i checked the datatype
using `isinstance`, i realized that the python version takes the datatype of
input as an `int` . When i try to convert it into a string, it gives the same
error. Is it the problem of the version or is it something i am doing wrong.
My code is below.

    
    
    import sys
    #import random
    
    file = open(sys.argv[1], 'r')
    string = ''
    for line in file:
        if line.startswith(">"):
            pass
        else:
            string = string + line.strip()
    
    
    w = input("Please enter window size:")
    test = [string[i:i+w] for i in range (0,len(string),w)]
    #seq = input("Please enter the number of sequences you wish to read:")
    #first = random.sample((test), seq)
    print test
    l = input("Enter letter for which you wish to find the probability:")
    lin = str(l)
    print lin
    


Answer: Use `raw_input`, not `input`. In Python 2.x, `input` expects valid Python
code, where as `raw_input` will convert the input to string. In Python 3.x
`input` works the same as `raw_input`.

To solve your _actual_ problem, which is counting the number of first letters,
you can use either a
[`defaultdict`](http://docs.python.org/2/library/collections.html#collections.defaultdict)
or a
[`Counter`](http://docs.python.org/2/library/collections.html#collections.Counter).
`Counter` is only available if your Python version is 2.7 and above.
`defaultdict` was added in 2.5.

    
    
    >>> from collections import Counter
    >>> i = ['CTTC','CGCT','TTTA','CATG','ABCD']
    >>> c = Counter(x[0] for x in i)
    >>> c['C']
    3
    

Here is the `defaultdict` approach:

    
    
    >>> from collections import defaultdict
    >>> d = defaultdict(int)
    >>> for x in i:
    ...    d[x[0]] += 1
    ...
    >>> d['C']
    3
    

