KL-divergence is often defined as 
$$D_{KL}(P||Q) = E_{x\sim P} \left[ \log\left(\frac{P(X)}{Q(X)}\right) \right] = \int_{-\infty}^{\infty} P(x) \log\left(\frac{P(X)}{Q(X)}\right)dx$$

**I don't quite understand the meaning of the notation of the second term** and how you can get the third term from there. The fact that there is the subscript $x \sim P$ under the expectation seems to indicate that there are several random variables in the Expectation bracket and the subscript is here to clarify the variable with respect to which we're integrating. In such cases, the notation I'm used to is $E_X[h(X,Y)]$ (eg in [this answer][1]). But here, I'm confused by the fact that there seems to be a single random variable $X$ following two different distributions. So could this expression be rewritten with two separate random variables $X$ and $Y$ ?

Regarding the derivation of the third term, do you get it via the LOTUS, setting $g(X)$ as $\log\left(\frac{P(X)}{Q(X)}\right)$ ? I've already seen functions of random variables, but I find it strange that in this case, the $g$ function would the probability distributions $P(X)$ and $Q(X)$ of the random variable(s)... All this makes me think that I'm misunderstanding a basic notation. Can someone walk me through the explanation of the meaning of the second term and the derivation of the third one ?


  [1]: https://stats.stackexchange.com/a/72614/201218