If I may, Harold is correct that punishment can serve as a "deterrent," but I think that the larger point is that punishment does not work as well as reinforcement (whether positive or negative).
With punishment, there are a few limiting factors... Primarily, the longer the duration between the action and the punishment, the less the lesson is retained... and I'm talking on the order of microseconds here. If we engage in an act, and are shocked within ~400ms, then we will likely refrain from engaging in that same behavior in the future... However, if we engage in an act, and punishment doesn't come for minutes, hours, days, or even months... Then the association between the act and the punishment is practically nonexistent... or, at least severely limited.
Also, punishment doesn't tend to teach people not to engage in certain behaviors, but instead teaches people how to engage in those behaviors without getting caught. So, as a general rule, the lesson we learn from the punishment approach is NOT to avoid the action, but to learn new ways to do the behavior in secret and with stealth. I cite now the invention of radar detectors.
The best way to achieve lasting behavioral change is with variably scheduled reinforcement... Positive reinforcement where the "right" or the "good" behaviors are rewarded, and negative reinforcement where the reward comes from the removal of a negative stimulus (like how the buzzing goes away in your car once you click your seatbelt... that's negative reinforcement). If you reinforce using a variable schedule (sometimes the behavior is reinforced, other times it is not), then you get lasting change. By avoiding giving the reward every single time, the person learns to internalize the behavior, and not do it solely for the reward. That way, when the reward goes away, the "good" or "right" behavior remains.
However, punishment CAN be a deterrent, it's just incredibly limited.