
Online Python Tutor: Learn programming by visualizing code execution - ColinWright
http://pythontutor.com/
======
pgbovine
Oh wow, woke up this morning to some great emails. Thanks, everyone!

Here is some context behind the current incarnation of Online Python Tutor:

<https://plus.google.com/+ResearchatGoogle/posts/cseo9qi7LWq>

------
nicpottier
Un-fucking-believable!

This is exactly what I needed to help people visualize their code. I can't
tell you how many beginners have a hard time figuring out how a program steps
through their code and this is a huge win there.

Huge thanks!

------
upthedale
Looks good.

However, my big criticism of these sorts of visualisations is they seem to be
entirely geared towards imperative programming. Has anyone seen similar
visualisations for more functionally structured code?

Oh, I see you can write your own code to be visualised. Here's a simple list
comprehension:
[http://pythontutor.com/visualize.html#code=myList+%3D+range(...](http://pythontutor.com/visualize.html#code=myList+%3D+range\(10\)%0Atotal+%3D+%5B2+*+x+for+x+in+myList+if+x+%25+2+%3D%3D+0%5D&mode=display&cumulative=true&py=2&curInstr=0)

Not the most enlightening visual.

~~~
agentultra
Generators are similarly confusing to visualize...
[http://pythontutor.com/visualize.html#code=def+fib(n)%3A%0A+...](http://pythontutor.com/visualize.html#code=def+fib\(n\)%3A%0A++++i+%3D+0%0A++++a,+b+%3D+\(0,+1\)%0A++++while+i+%3C%3D+n%3A%0A++++++++yield+b%0A++++++++i+%2B%3D+1%0A++++++++a,+b+%3D+\(b,+b%2Ba\)%0A%0A++++++++%0Afor+x+in+fib\(8\)%3A%0A++++print+x&mode=display&cumulative=false&py=2&curInstr=0)

~~~
anonymous
It does look a bit confusing, but it does step through the program in the
order it executes. Unlike list comprehension, where it completely misses some
instructions.

------
TeMPOraL
Just 5 days ago HN was all so negative about Bret Victor's visions[1] of how
programming should be taught. And yet here we are, looking at a working
example of one of the ideas he wrote about.

[1] - <http://news.ycombinator.com/item?id=4577133>

~~~
pgbovine
Bret has certainly been an inspiration to lots of us who work in this field,
but I still don't think this is an implementation of his vision. To truly
implement his vision, someone would need to design a new programming language
and environment.

My motivation has been to ease the learning path for an existing popular
language (Python).

~~~
TuringTest
I have been thinking of such a language for several years. Now that life
programming environments are getting traction, I might be able to bootstrap
the process and program my ideated language on top of one of them.

See this 2003 PhD thesis at MIT for two examples of existing languages
designed to take full advantage of live programming environments. (Flogo I
-graphical- and Flogo II -textual): <http://llk.media.mit.edu/papers/ch-
phd.pdf> Particularly interesting is Flogo II capability to mix the imperative
and functional-reactive programming paradigms in the same code block, as
parallel runtime processes; I hadn't seen that before.

~~~
seanmcdirmid
Christopher Hancock's thesis is pretty good; flogo II plays around with this
feature called "live text" where it embeds code exucution visualizations
directly in the text of the code. I still haven't seen anyone pull that off
again.

------
DanielRibeiro
Former discussion: <http://news.ycombinator.com/item?id=3609263>

------
johncoltrane
I saw it some months ago (1) but it didn't get much traction. And I failed to
upvote it, shame on me. This is really good work.

(1) <http://news.ycombinator.com/item?id=3759858>

------
slykat
This would have been really useful when I was learning Python on Udacity and
was confused by the behavior of = and += on lists and tuples.

This should definitely be integrated into Udacity and other learning
platforms! Most instructors have to do this by hand in CS 101 lectures
anyways.

------
oellegaard
Too bad it stops at 300 steps, I would have loved to use it for my hand-ins at
the university - still, pretty cool! I checked how it looked with MergeSort
and it was pretty awesome, even though it didn't get far with 300 steps.

------
pkandathil
Hey guys, Isn't this the same as a debugger. Can people not learn from that?

~~~
btown
Even now, after almost 10 years of programming, I still wish my debuggers had
arrows pointing to data structures in a nice GUI. Sometimes, you don't want to
interrupt your train of thought to recall the gdb or pdb command you need, or
to remember "was 0xAE32F542 the first node in the list or the second node?"
And consider the WebKit debugger for Javascript: just by looking at the Scope
Variables tag, it's not obvious to catch a bug where you did `x = y` rather
than `x = deepClone(y)`.

TL;DR I wish this was the same as a debugger, because then debuggers would be
awesome.

------
scott_s
Philip, this is fantastic stuff. How have I not heard of your project before?
Have I not been paying attention, or have you not shown it here before?

~~~
ronyeh
I don't think Philip has posted this project on here before. His user name is:
<http://news.ycombinator.com/user?id=pgbovine>

Another project he worked on that you may have seen on HN is the PhD Grind
book (about getting a CS PhD from Stanford).

~~~
scott_s
Oh, I know. He's been a regular here for a while. Which is why I was surprised
I did not know about one of his projects.

~~~
pgbovine
I've never actually posted any of my projects in the past. Every so often I
wake up to an inbox flurry and then check HN :)

------
csmatt
I'm sold. Great work! I advocate learning to program to non-programmers all
the time (and typically suggest Python), but visualizing flow is hard to teach
succinctly with words. I really think this type of visualization with
accompanied explanation, where needed, will help quite a bit. Heck, I may use
it for debugging at times in the future.

------
detox
A big plus for most people out there. I'm a visual learner myself. I've always
had trouble in the early stages of learning because I didn't really realize I
was a visual learner until later on. Also, college professors didn't give
simple tips like you should draw out diagrams of what's happening in the code
because that helped me out a lot.

------
maskedinvader
this is awesome, learning from visualizing how the code is executed IMO is a
great way to learn to code. To add to that, its teaching python which I
believe is already very easy to read and learn, brilliant. If this doesn't
make it easy for young students learn programming for the first time then I
don't know what will ! great job !

------
njharman
It's not exactly Python (or I'm doing something wrong). Which is very
confusing / "dangerous" for people learning Python (They will learn it wrong
and not have the xp/confidence to question tutor)

locals() and globals() are undefined.

Still crazy slick (if it were the early 90's I'd say "l33t") bit of coding.

~~~
btown
If you're learning Python and are using locals() and globals() as anything
more than a debugging tool... I'd venture to say you are doing something wrong
;)

In seriousness, any programmer will need to know that functions don't just
magically appear... if they don't work, they'll need to know how to pull in
some sort of library, or realize why those functions just _don't make sense_
for the current environment (one could explain to a complete novice why it
would be a security breach to allow I/O functions, for instance). Python is a
language, not a standard library, just as C is C even if you don't #include
<stdlib.h>.

------
jkf
[http://ec2-107-21-160-68.compute-1.amazonaws.com/crs-
ltp/tut...](http://ec2-107-21-160-68.compute-1.amazonaws.com/crs-
ltp/tutor.php#mode=edit) I think the tool of the course in coursera.org is the
same.

------
zerop
I used to browse this tutor on MIT earlier..
<http://people.csail.mit.edu/pgbovine/python/tutor.html>

------
prezjordan
Is there a self-hosted option for this? I think this would be an excellent
resource for my students, but I'm having trouble getting _my_ examples to run.
(memory overload?)

~~~
bbx
Looks like the source is on GitHub:
<https://github.com/pgbovine/OnlinePythonTutor>

------
MathProgramming
Why would they simulate a linked list using tuples? It seems a bit contrived
(or rather, not Pythonic) when lists are a built-in datatype.

~~~
TeMPOraL
A quick way to emulate cons cells[1], which are good for explaining how linked
lists work?

[1] - <http://en.wikipedia.org/wiki/Cons>

~~~
pgbovine
Yep, right on! I wanted to show off linked list recursion and render box-and-
pointer diagrams like those seen in SICP.

------
jftuga
This is really awesome. I am trying to teach my kids Python and this will be
very helpful.

------
flexie
Oh man! This is useful. Thanks.

