Work done by a gas In the expression for work done by a gas, 
$$W=\int P \,\mathrm{d}V,$$
aren't we supposed to use internal pressure?
Moreover work done by gas is the work done by the force exerted by the gas, but everywhere I find people using external pressure instead of internal pressure.
 A: At the interface with the surroundings, by Newton's third law, the force per unit area exerted the gas on its surroundings is equal to the pressure of the surroundings on the gas.  But, in an irreversible expansion or compression process, the pressure of the gas may not be uniform within the cylinder.  So the pressures match only at the interface.  In addition, viscous stresses contribute to the force per unit area exerted by the gas at the interface (as well as throughout the cylinder), so the equation of state (e.g., ideal gas law) cannot be used to establish the gas pressure within the cylinder or at the interface.  The ideal gas law applies only if the gas is at thermodynamic equilibrium.
A: The work done by the gas on the piston is
$$W_1 = \int P_{\text{int}} \, dV$$
where $P_{\text{int}}$ is the pressure of the gas right next to the piston. This is just a mild rephrasing of the definition of work. The work done by the piston on the outside is
$$W_2 = \int P_{\text{ext}} \, dV$$
where $P_{\text{ext}}$ is the pressure of the external air right next to the piston. These two pressures may be different, so we may have $W_1 \neq W_2$.
For example, the two may differ if the piston has friction, with the difference $W_1 - W_2$ dissipated into heat. (Friction exists no matter how slowly the piston is moving, so this also holds for a quasistatic process.) Or the piston may be accelerating, in which case $W_1 - W_2$ goes into the piston's kinetic energy.
In high school physics, $P_{\text{int}}$ and $P_{\text{ext}}$ are always assumed to be the same, to keep things simple.
A: The work done by an expanding gas is the energy transferred to its surroundings. In effect, as the gas expands it is compressing its surroundings so the work done is the force exerted on the surroundings (i.e. the pressure of the surroundings times the area) times the distance moved.
The extreme case of this is a Joule expansion where a gas expands into a vacuum i.e. the pressure of the surroundings is zero. In this case the expanding gas does no work regardless of the initial pressure of the gas.
A: The answer to the question by the OP is that the work done by a system can always be evaluated considering movement against the external force (pressure).  For a quasi-equilibrium process this work can also be evaluated considering changes in the internal pressure and volume of the gas; but in general for a non quasi-equilibrium, irreversible, process the work cannot be evaluated using changes in the internal pressure and volume in the gas, because the gas is not in a definite state.
A detailed evaluation of work for a gas expanding and pushing a piston follows.
The detailed evaluation also answers a question by @ATHARVA in an earlier comment for a gas expanding and moving a piston.  Specifically, why is the weight of the piston not included in the work done by the gas?  Based on comments and answers by others, there is confusion about the answer to this question. And, if there is also an external pressure on the piston (e.g., from the atmosphere), and from other forces on the piston due to external loads driven by the piston, why are they also not included in the work done by the gas? The answer is: we are interested in the work done by a system on its surroundings; for the piston example, this is the work done by the gas (the system) on the piston (the surroundings), but this is not the total work done on the piston.
The answers to both these questions follow, using a detailed evaluation of the work done on the piston by a single force, from the pressure of the gas, contrasted with the net work done by the total force on the piston, the vector sum of all forces on the piston: the force from the gas pressure, the weight of the piston, the external atmospheric pressure on the piston, and other forces on the piston due to external loads driven by the piston.

Let the system be defined as the gas in a container, a closed thermodynamic system of constant mass, with a moveable boundary, that being the interface of the gas with a moveable piston on top of the gas.  The piston is treated as a rigid body, and a rigid body can have no change in its internal energy (e.g., a rigid body cannot be compressed.)  The bottom of the piston is exposed to the gas and the top is exposed to the atmosphere. Initially, the piston at rest. An amount of heat $Q_{gas}$ is added to the gas to move piston upwards in the $\vec x$ direction.   We are interested in the work $W_{gas}$ done by the system, the gas, on its surroundings, the piston. $W_{gas}$ is positive due to movement of the piston upwards. From the first law of thermodynamics,
$Q_{gas} - W_{gas} = \Delta U$ where $\Delta U$ is the change in the internal energy $U$ of the gas.

Let $F_{ext}$ (here called the external force) be the total force on the piston from the surroundings external to the system (the gas). $F_{ext}\hat i = -(mg + P_{atm}A + F_{other})\hat i$ where $g$ is the acceleration of gravity, $P_{atm}$ is the atmospheric pressure, $A$ is the area of the piston of mass $m$, $F_{other}$ is the reaction force on the piston from external loads driven by the piston, and $\hat i$ is a unit vector positive upwards. $V$ is the volume of the gas.
In a quasi-equilibrium (slow) process, the piston moves slowly and has zero acceleration; the gas is always in a definite thermodynamic state, and the state changes slowly as the piston moves. Since the piston has zero acceleration, the sum of the all the forces (the total force) on the piston is zero: $F_{ext} \hat i + P_{gas}A \hat i = 0$ where $P_{gas}$ is the gas pressure. (Actually, $P_{gas}A \hat i$ is infinitesimally greater (or less) than $F_{ext} \hat i$ to cause slow movement of the piston upwards (or downwards).)   The total work done on the piston is the work done by the gas plus the work done by the external force. Since the total force on the piston is zero, the total work done on the piston is zero. Let $V$ denote the volume of the gas and let $x$ denote the displacement of the piston, taken positive upwards. For movement of the piston $dx$, $dV = Adx$.  The total work done on the piston is $\int_{V_1}^{V_2} P_{gas}(V) dV + \int_{x_1}^{x_2} F_{ext} dx = 0$.  The work done by the gas on the piston $W_{gas}$ is  $\int_{V_1}^{V_2} P_{gas}(V) dV$ and the work done by the external force is   $\int_{x_1}^{x_2} F_{ext} dx$. $\int_{V_1}^{V_2} P_{gas}(V) dV$ = $-\int_{x_1}^{x_2} F_{ext} dx$. $\int_{V_1}^{V_2} P_{gas}(V) dV$ can be evaluated if you know the gas pressure as a function of volume.  The mass of the piston and the atmospheric pressure do not appear in the work done by the gas $\int_{V_1}^{V_2} P_{gas}(V) dV$, but are accounted for in the work done by the external force $\int_{x_1}^{x_2} F_{ext} dx$. In many problems and examples addressed in basic thermodynamics textbooks, $P_{gas}(V)$ and the change in $V$ are given and the work is evaluated as $\int_{V_1}^{V_2} P_{gas}(V) dV$. This may leave the impression that the $F_{ext}$ is not considered, but since
$\int_{V_1}^{V_2} P_{gas}(V) dV = -\int_{x_1}^{x_2} F_{ext} dx$, $P_{gas}(V)$ and the change in $V$ are implicitly affected by $F_{ext}$. $\int_{V_1}^{V_2} P_{gas}(V) dV$ is the work done by the gas on the piston, not the total work done on the piston, and the work done by the gas on the piston is also equal to $-\int_{x_1}^{x_2} F_{ext} dx$ with magnitude $\int_{V_1}^{V_2} P_{ext}(V) dV$, where $P_{ext} = |F_{ext}|/A$ is the magnitude of the external pressure from the external force on the piston. In summary, for a quasi-equilibrium process, the work done by the gas on the piston can be evaluated using either $\int_{V_1}^{V_2} P_{gas}(V) dV$ or  $\int_{V_1}^{V_2} P_{ext}(V) dV$ since the pressures $P_{gas}$ and $P_{ext}$ are equal.
Here is a simple analogy. Consider an applied force pushing a mass (a rigid body) slowly upwards against gravity, where the applied force is only infinitesimally greater than the force of gravity.  This is a quasi-static process in mechanics textbooks.  The applied force and the force of gravity are equal in magnitude but opposite in direction, so the total force on the mass is zero. The mass has zero acceleration since the net force is zero, there is no total work done on the mass, and there is no change in the kinetic energy of the mass.  However, the work done by the applied force $W_{applied}$ is not zero, it is the product of the applied force times the distance the object slowly moves upward.  The work done by the applied force here is analogous to the work done by the gas in the gas/piston discussion, and the work done by gravity is analogous to the work done by the external force; each of these forces separately does work on the mass but the net force (zero force) does no work.  The change in potential energy $\Delta PE$ is defined as the negative of the work done by gravity, resulting in the standard relationship of elementary mechanics for this example $W_{applied} + \Delta PE = 0$.
For a non quasi-equilibrium process, such as very rapid expansion of the gas, all the gas is not in equilibrium (not in a definite state), and the movement of the piston cannot be ignored. The pressure is non-uniform throughout the gas, the process is irreversible, and the work done by the gas is not $\int_{V_1}^{V_2} P_{gas}(V) dV$ since the gas has no specific state; the work done by the gas on the piston is $\int_{x_1}^{x_2} P_{gassurface}Adx = \int_{V_1}^{V_2} P_{gassurface}dV$ where $P_{gassurface}$ is the average pressure of the gas on the piston surface.  The acceleration of the piston is given by $ma \hat i = P_{gassurface}A \hat i + F_{ext} \hat i$ where $a \hat i$ is the acceleration of the piston. For a practical application, $ma \hat i$ of the piston is small, so $P_{gassurface}$ is approximately equal to $|F_{ext}|/A$ in magnitude. $|F_{ext}|/A =P_{ext}$. Therefore, the work done by the gas is approximately $\int_{V_1}^{V_2} P_{ext} dV$ in magnitude. This issue was also addressed by @Chet Miller in his earlier response.
A: Work is done by the gas against the external pressure.If there is a case of free expansion of the gas (as in vacuum) the work done by the gas is zero as no opposing forces are present to prevent expansion of the gas, hence it is evident that work done by the gas is only due to external pressure. If the process is quasistatic(that is infinitesimally slow) then outside pressure is almost equal to internal pressure hence work done may be evaluated by considering the pressure of the gas, but if the process is not quasistatic then we must consider external pressure only.
A: If it is free expansion then one molecule hitting the piston will let piston fly away (assuming it is massless). Then the remaining gas molecules expand and we say no work is done by gas. Let's take another example. Piston has mass with gas on one side and vacuum on other side. Suppose after sometime we see piston moving with some speed. Who gave energy to it? Obviously gas. So work done by gas in this case is not zero inspite of external pressure being zero. So in calculating work done by gas we have to take gas pressure and not external pressure. 
A: Usually piston moves slowly we take change in kinetic energy of piston as zero which is equivalent that work done by internal pressure is equal to work done by external pressure
