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 can greatly help us better understand how Python runs and debugs code.
Official website: https://pythontutor.com/#
This tool is called Python Tutor, and it is used to visualize the execution of code. 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 step through its execution.
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 3 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 speaking, 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.
For example, we have the code above. Let's see how it is executed.
The entire program execution process is vividly presented in front of you. Clear enough, right?