Edit - warning: the following only works for this particular case, not for the general case - see below.
int value = 0; d.TryGetValue(p, out value); d[p] = value + 1;
this is equivalent to the following Python snippet (which is better than the one you are showing):
d[p] = d.get(p, 0) + 1
setdefault is similar to get (fetching if present, otherwise using a different value) plus the side effect of injecting a key / other value pair into a dict if the key was not there; but here this side effect is useless, since you are still going to assign d[p] , so using setdefault in this case is just stupid (complicates the situation and slows down to the right goal).
In C #, TryGetValue , as the name suggests, tries to get the value corresponding to the key in its out parameter, but if the key is missing, then it ( warning: the following phrase is incorrect:) just leaves the said value on its own ( edit:) . What he actually does if the key is not present is not to "leave the value alone" (it cannot, because it has a value of out , see comments), but to set its default value for the type is here, since 0 (default value) is what we want, we are fine, but this does not make TryGetValue a general purpose replacement for Python dict.get .
TryGetValue also returns a logical result, telling you whether it managed to get the value or not, but in this case it is not needed (simply because the default behavior suits us). To create the general Python equivalent of dict.get , you need one more idiom:
if (!TryGetValue(d, k)) { k = whatyouwant; }
Now this idiom is really equivalent to the universal Python equivalent k = d.get(k, whatyouwant) .