The K10D and the D40 aren't really in the same class. If your looking at the K10D, then the Nikon D80 is more comparable.
I've been a long time Nikon user (going back to the late 80's) and was not happy when Nikon introduced the D40. I thought it wasn't really a great idea to not include an internal focus motor in the body. I still don't like the idea, however, sales are saying the people really don't mind it. I've come to like it more as time has gone by.
The D40 takes great images. There are many many happy users out there. There are approximately 30-40 AF-S Nikkor lenses (including 5 that were just announced, though they are on the expensive side) that will auto focus with the D40 and another 20 or so 3rd party lenses (Sigma, Tamron & Tokina). Any other Nikon mount lens will fit on the D40 and you can take pictures with it. Some, like the AF lenses, will not auto focus, but will manual focus and will still be able to use the in camera meter system. Others, like the much older AI lenses, will not auto focus or use the meter system. Though with the AI lenses (AI designates them as manual focus) only the very expensive dSLR's will be able to have the meter system work. You can use these lenses, but will have to manual focus them.
If your looking to get your foot in the door and think your going to make the commitment to one company for long term useage and have upgrades over the coming years, then I like the Nikon.
Not that Pentax isn't any good. Far from it. They have a great selection of lenses that have been made over the years. They make great camera's. Overall, I prefer Nikon.