I've been reading 'How to Prove It' by Velleman, in the book it states that:
$\{x\,\in\,U\,|\,P(x)\}$; this is read 'the set of all $x$ in $U$ such that $P(x)$'.
It then goes on to give $\{x\,\in\,\mathbb R\,|\,x^2<9\}$ as an example of such a set which would have the real numbers between $-3$ and $3$ as elements.
However, in the next chapter the book gives an example of the set of all perfect squares $S$ by specifying the universe of discourse on the right:
$S=\{n^2|\,n\,\in\,\mathbb N\}$
So is it acceptable to switch what is on the left side of the vertical bar with what is on the right?
The Wikipedia entry on set builder notation, under 'Specifying the domain' uses the former notation, yet under 'More complex expressions on the left side of the notation' they use the latter. From the book and Wikipedia I'd assume it would be valid to use either. That being said, I've seen people claim that by switching them one would be creating a different set.
TL;DR: NO!
The general structure of a set-builder notation is $\{\text{term}(x)\mid\text{condition on }x\}$ or $\{\text{term}(x)\in X\mid\text{condition on }x\}$ when we want to bound the terms we allow.
The left side is some term defined from $x$. This can be $n^2$, or it can be $f(x)$ or whatever. But it is a term. It is a mathematical object in our mathematical universe, which might be bounded by a certain set. On the right side, we look at the condition on $x$. This is a formula which should have one free variable: $x$. And then we know that we take all the terms build from $x$'s satisfying the condition.
What you are allowed to do, however, is retranslate the term and the conditions. You can always arrive at something of the form $\{x\mid\text{condition on }x\}$, because you can always put the bound $x\in X$ in the condition, and you can always say something like "There exist $y$ such that $\text{term}(y)=x$".
So writing $\{n^2\mid n\in\Bbb N\}$ can be written as $\{n\mid\exists k\in\Bbb N:k^2=n\}$.
But translating one thing to the other is not the same as switching sides. The set builder notation $\{n\in\Bbb N\mid n^2\}$ is meaningless, since $n^2$ is a term, not a formula. It does not evaluate to a true/false condition which tells us in which case we put $n$ into our set and in which case we do not.
So really the only thing we can move around is the bounding set. Namely, $\{x^3\in\Bbb N\mid x+1\text{ is even}\}$ and $\{x^3\mid x\in\Bbb N\text{ is odd}\}$ form the same set, although they look differently. Why do we do that?
The answer is readability. You want to write your set in the way that will be the most readable to your audience, and will maximize usability later, when you refer to properties of the elements in the set you defined.
So when you want to take the set of squares in the natural numbers, it is easier to write $n^2$, and not "There is some $k$ such that $k^2=n$". So making the term more complex makes the condition way simpler. But now we are left without any condition, as we want to take all the squares. So we move the bounding set into the condition, then we get that $n\in\Bbb N$ is our condition.
One extremely important caveat here is to note that sometimes the term moves you from one set to another, e.g $f\colon X\to Y$ and $A\subseteq X$, then $\{f(x)\mid x\in A\}$ is in fact a subset of $Y$, so it would be written as $\{y\in Y\mid\exists x\in A: f(x)=y\}$. So when you switch the sides of the bounding conditions, it is important to make sure that the sets you care about are changed if necessary.