Deductions & credits

Because that's the way Congress wrote the tax law.  However, you get a $250,000 exclusion from gains (500,000 if Married Joint).

 

But, you said you took out a 401(k) loan.  That part is not taxable unless you default on it.