I am trying to understand the following:
Theorem: Let $(X,d_X)$ and $Y(d_Y)$ be metric spaces. A function $f:X\rightarrow Y$ is continuous if and only if for every open $U\subset Y$, then $$ f^{-1}(U) = \{x\in X : f(x)\in U\} $$ is open in $X$.
If I want to show that the function $f:[0,1]\cup[2,4]\subset \mathbb R \rightarrow \mathbb R$ defined by $$ f(x) =\begin{cases} 1, & x\in [0,1];\\ 2, & x\in [2,4]. \end{cases} $$ is continuous, then I need to consider any open set $U\subset \mathbb R$ and show that $$f^{-1}(U) = \{x\in [0,1]\cup[2,4] : f(x)\in U\}$$ is open.
I understand that if $1 \notin U$ and $2 \notin U$, then $f^{-1}(U) = \emptyset$. But I don't understand what happens when only $1\in U$, or only $2\in U$, or $1,2\in U$.
If only $1\in U$, then is it correct to say that $f^{-1}(U)=[0,1]$? If only $2\in U$, then is it correct to say that $f^{-1}(U)=[2,4]$? But I am really confused since these are closed intervals. I am lost.
Can someone help me to find $f^{-1}(U)$ for those cases? Any clues or hints will be appreciated.
Thanks in advanced.