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: To prove that $g$ is continuous on $\mathbb R \setminus \mathbb Q$, fix $x\in\mathbb R \setminus \mathbb Q$ and $\epsilon > 0$. Let $N = \lceil \frac1\epsilon \rceil$.
Let $a$ be such that $x\in [\frac a{M}, \frac{a+1}{M}] =: I$, chosing $M > N$ such that there is no $q = \frac nm\in\mathbb Q$ with $\gcd(m,n) = 1$ and $m < N$, therefore any
$q\in I$ has the reduced form $\frac nm$ with $m \ge N$ so $g(q) \le \frac1N \le \epsilon$ thus if be chose $\delta > 0$ such that $B_\delta(x_0) \subset I$, we have found $\delta$ with
$$|g(x) - g(x_0)| = g(x) \le \epsilon \qquad \forall x: |x-x_0| < \delta$$
qed.
Note that $x_0$ must be irrational for the existence of such an $I$ wich doesn't contain any fractions with "small" denominator.
A: 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.
