We define \(\displaystyle{f:\mathbb{R}\longrightarrow \mathbb{R}}\) by
$$f(x)=\begin{cases}
1\,\,,x\in\mathbb{Q}\\
0\,\,,x\in\mathbb{R}-\mathbb{Q}
\end{cases}$$
which is better known as $\rm Dirichlet$ function. Here is a graph:
If \(\displaystyle{T\in\mathbb{Q}}\), then : \(\displaystyle{f(x+T)=f(x)\,\,,\forall\,x\in\mathbb{R}}\) since :
$$x\in\mathbb{Q}\implies x+T\in\mathbb{Q}\implies f(x+T)=1=f(x)$$ and
$$x\in\mathbb{R}-\mathbb{Q}\implies x+T\in\mathbb{R}-\mathbb{Q}\implies f(x+T)=0=f(x)$$
Now, if \(\displaystyle{K\in\mathbb{R}-\mathbb{Q}}\), then :\(\displaystyle{f(1+K)=0\neq 1=f(1)}\) .
For the second part, suppose that there exists a function \(\displaystyle{g:\mathbb{R}\longrightarrow \mathbb{R}}\) such that any irrational is its period and any rational is not. Since \(\displaystyle{\pi}\) and \(\displaystyle{1-\pi}\) are irrational numbers, we get:
$$\forall\,x\in\mathbb{R}: g(x+1)=g((x+1-\pi)+\pi)=g(x+1-\pi)=g(x)$$
which means that the rational number \(\displaystyle{q=1}\) is a period of \(\displaystyle{g}\), a contradiction.