First have you read the thread on why not to buy the D40? Not saying it is a horrible camera buy it is definitely making me not consider it. Be glad you're not in my position I think the K10D is clearly the superior camera in my price range I'm looking at BUT my dad has a small but expensive setup of Nikon lenses sadly the body is just a small portion of your consideration of a dSLR. Lenses cost as much and often MORE than the body.
Reviews I've seen put the pentax pretty much on par with the Nikon. As Garbz said, head to a camera store and get some hands on, get the one that feels best, and is easiest for you to navigate through the menus, as performance will be similar for either of them.
Check out reviews at DPReview and Steves Digicams too for pros and cons of each.