Lagrange[
        n_] := (
p = xx - x[0]; 
For[
          i = 1, i ≤ n, 
          i = i + 1, 
p  =  
            p (xx - x[i])]; 
L = 
          0; 
For[j = 0, j ≤ n, 
          j = j + 1, 
q = 
            Factor[p/(xx - x[j])]; 
r = 
            q /. xx -> x[j]; 
L  =  
            L +  y[j] q/r])

Lagrange[2]

L

x[0] = 0; x[1] = 1; x[2] = 2; x[3] = 4;
y[0] = 0; y[1] = 0; y[2] = 0 ; y[3] = 2;

Lagrange[3]

L

Plot[L, {xx, -1, 5}]