Why is work path dependent for gas expansion?

In my early physics courses we learned about how work is a state function (path independent) when a conservative force (such as gravity) is acting upon the system and a path function (path dependent) when a non-conservative force (such as friction) is acting upon the system instead.

A nonconservative force is called such because mechanical energy (kinetic and potential energy) is not conserved between the initial and final states of the system because the energy was lost as "heat." Unlike mechanical energy, heat is not a "reversible" form of energy.

Now in thermodynamics, we were told that $P_{ext}\Delta V$ work is path dependent. I don't understand why that is true. For example, let's say I put a weight onto a piston at equilibrium. The force of gravity will now act on the system. Why is the force (gravity I think) of a piston slowly compressing an ideal gas not conservative? The "potential energy" of the weight should be being transferred into the gas molecules somehow because the internal energy (I think) of the system went up because we see that the pressure of the gas has gone up.

Does it matter if the process is reversible or irreversible?

Note: Some of the potential energy also turned into kinetic energy of the weight as it moved. But perhaps we can assume that the weight moves so slowly that it gains an infinitesimal in kinetic energy.

It does matter if the process is reversible or irreversible because reversible processes give maximum work. It can be verified by a PV diagram of a reversible process. These processes produce maximum work because the value of pressure is infinitesimally greater (in case of compression) than the pressure of the gas in the container.

As we know, $W_{ext}=-P_{ext}\Delta V$, if Pressure was maximum for each infinitesimal change in volume then the work done (corresponding to that change) will also be maximum. This can only happen if the external pressure is infinitesimally greater than the pressure of the gas in the container and compression will take place very slowly.

In other words, the PV diagrams of reversible processes give maximum area under the PV curve and the volume axis.

These processes, ideally speaking, never reach completion and are very slow and obviously, cannot be realized in real life. A reversible process is a ideal process.

Work done in path dependent. This can also be verified by the PV diagram. Take this diagram for example.

There are infinitely many paths that can be taken to move the system from state A to state B and each path will give a different value of work done.

The work done in path A-B is greater than the work done in path A-C-B. (Work done is give by the area under the PV curve and V axis).

The work done by the system in a cyclic transformation is equal to the heat absorbed by the system. Since $\Delta U=0$, if the system work is done by the system $(\Delta V=+ve)$ then the heat has to be absorbed by the system $(q=+ve)$ in order for $\Delta U$ to be $0$. And if work is done on the system then the energy will be released from the system.

In the case you cited where a weight is added on top of the piston, and the system is allowed to equilibrate adiabatically, if the weight is substantial, the piston will compress the gas rapidly (not slowly), and the compression process will be irreversible. The combination of piston and weight will attain kinetic energy, and the piston will even osciallate up and down (about the final equilibrium position) until its motion is damped out by viscous forces in the gas. In the end, the cumulative amount of work done by the piston and weight will just be the change in potential energy, as you indicated. But there is no adiabatic reversible process that can take the gas from the same initial state to the same final state (as attained in this irreversible compression). To get from the initial state to the final state reversibly, there would have to be heat added to the system. This would enable the reversible process to match the increase in entropy of the irreversible adiabatic process.