Doing live code demos in class can help in engaging students in their learning. Students get to see the effects of code changes right away rather than just hearing about the concepts through a lecture presentation. However, at least for physics, students who observe demonstrations perform only slightly better, but not statistically significant, than those who do not. Without active participation in the demonstration, students are not engaging with their learning, just like listening passively in a lecture. The only difference is that the demonstration may be a bit more entertaining and may have some affective effects on the students.
To make the most out of classroom demonstrations, one simple strategy is to simply ask the students to predict what will happen before doing the demonstration. Adding one or two minutes in having the students think about the topic under discussion, and predict what would happen if there is a change, turns out to have dramatic effects on student learning. If students are also required to discuss their predictions with their peers, their learning can be improved even more.
Crouch, C., Fagen, A., Callan, J., Mazur, E. (June 2004). Classroom demonstration: Learning tools or entertainment?. American Association of Physics Teachers. 72(6), p 835 - 838.