Continuity almost everywhere Let 
$$f(x) = \left\{\begin{array}{ll}
1 & \text{if }x\in\mathbb{Q},\\
0 & \text{if }x\notin\mathbb{Q}.
\end{array}\right.$$
$$g(x) = \left\{\begin{array}{ll}
\frac{1}{n} & \text{if }x = \frac{m}{n}\in\mathbb{Q},\\
0 & \text{if }x\notin\mathbb{Q}.
\end{array}\right.$$
I have some difficulties with understanding of continuity almost everywhere.
Honestly, I thought that continuity almost everywhere means that I could take any continuous function and change (or even make them undefined) its values for countable set of points from domain, say, for $\mathbb{Q}$. Based on such thinking, both $f(x), g(x)$ are continuous almost everywhere, because I could get them as a result of change of countable values of $h(x)  \equiv 0$
The fact is that only $g(x)$ is continuous almost everywhere, while $f(x)$ is discontinuous. Could you explain what went wrong in my understanding? 

A function $f:X\to Y$ is continuous almost everywhere if it is only discontinuous on a set of measure $0$. Informally, this means that if we choose a random point on the function, the probability that it is continuous is exactly $1$.
For example,  any countable set, such as $\mathbb Q$, will be of measure $0$ in an uncountable one, such as $\mathbb R$.

There is an important distinction to make here: a function is continuous almost everywhere if it is continuous on a "large" subset. But it must be continuous on that subset. Your function $g$ is continuous at every irrational point, and hence we can say that it is continuous almost everywhere.
That is not the same as removing a countable set of values from the function, because in doing so,  you can damage the continuity of the remaining points. In the case of $f$, you are left with a function that is nowhere continuous!
This only works if you remove a countable set, but make sure that the remaining points are still continuous.
