Why is pressure-volume work calculated from the external pressure if the work is done by the gas? [duplicate]

Greeting everyone,
The formula for pressure-volume work I have been given in the textbook I am using to learn general chemistry (Chemistry.A.Molecular.Approach.Global.Edition.4th.Edition, Nivaldo J.Tro) is as follows:

w = -PΔV where P is the external pressure. Why is the external pressure used if the force is from the expanding gas, not from the external atmosphere?

N.B: The system is a gas trapped in cylinder between a dead end and a piston. We assume that the piston is massless. We assume that the pressure is constant.

• Think about if the external pressure was $0$. What would the gas be doing work on? – Aaron Stevens Mar 30 at 13:44
• @AaronStevens The piston. – Stooniel Schiffer Mar 30 at 14:00
• @StoonielSchiffer But you said the piston is massless. – Bob D Mar 30 at 14:15
• Yes, indeed. My mistake. Maybe I should learn about gases before diving further into the problem. – Stooniel Schiffer Mar 30 at 14:24
• @AaronStevens Unfortunately the possible duplicate you are indicating contains part of the answer but something is missing. But the way SE works makes not very appealing to add a new answer there. – GiorgioP Mar 30 at 15:58