For many beginners, learning Python can be a difficult task. Today, I will introduce you to a tool that can help you understand what happens when each line of code is executed on a computer. This tool is very helpful for us to better understand Python execution and debugging.
Official website: https://pythontutor.com/#
This tool is called Python Tutor, and it is used to visualize code execution. Whether you are learning Python, Java, C, C++, JavaScript, or Ruby, Python Tutor can help us overcome a basic obstacle in learning programming: understanding what happens when each line of code is executed on a computer.
You can use it to write Python, Java, C, C++, JavaScript, and Ruby code in a web browser and see its execution step by step.
Python Tutor has been used by over 10 million people in more than 180 countries/regions to visualize over 100 million lines of code, often as a supplement to textbooks, lectures, and online tutorials. As far as we know, it is the most widely used program visualization tool in computer education.
How to use it
First, you can choose the programming language you want to execute. I am using Python.
From the above image, you can see that the interface is super simple, with three parts explained below:
① Language selection;
② Code input box;
③ Two modes;
Everyone can understand ① and ② at a glance. Now let's talk about the two modes: Visualize Execution and Live Programming Mode.
Visualize Execution mode: After writing the program, you need to manually execute it to see what happens at each step of the program execution.
Live Programming Mode: You don't need to manually execute the program. The system will automatically execute and visualize it as you write each line of code.
Generally, it is better to use Visualize Execution mode. Since we are using this tool, we want to see the execution process of the code clearly, so it is better to execute it step by step and observe the execution results.
Actions speak louder than words. Let's do a demonstration for you directly.
For example, we have the code above. Let's see how it is executed.
The entire program execution process is presented vividly in front of you. Clear enough, right?