Beginners get started with Python and can debug code. This "artifact" is really awesome!

For many novice friends, it is difficult to learn Python.

Today, Huang will introduce an artifact that can help you understand what happens when the computer runs each line of code (as shown in the figure below). This is really helpful for us to better understand Python running and debugging code.
insert image description here

See what the official website says

Official website: https://pythontutor.com/

This artifact is called Python tutor, and it is used to visually execute code.

Whether you are learning Python, Java, C, C++, JavaScript or Ruby. Python Tutor can help us overcome a fundamental obstacle to learning to program: understanding what happens when the computer runs each line of code.

You can use it to write Python, Java, C, C++, JavaScript, and Ruby code in your web browser and see how it performs.

More than 10 million people in more than 180 countries have used Python Tutor to visualize more than 100 million pieces of code, often in addition to textbooks, lectures, and online tutorials. As far as we know, it is the most widely used program visualization tool in computing education.

how to use it

First, you can choose the language of the code you want to execute, I am using Python.
insert image description here
As can be seen from the above figure, this interface is super simple, with a total of 3 parts, which are described as follows:

  • ① Select language;
  • ② Code input box;
  • ③ Two modes Visualize Execution and Live Programming Mode;

About ① and ②, you can see at a glance. Let's talk about these two modes.

  • Visualize Execution mode: After writing the program, you need to manually execute it yourself, and then see what happens at each step of the program execution;
  • Live Programming Mode: No need to manually execute the program, the system will automatically execute and visualize every time a line of program is written;

Generally speaking, it is better to use Visualize Execution mode. Since we use this tool, we just want to see the execution process of the code, so it is better to execute it step by step by ourselves and observe the execution result.

好记性不如烂笔头, we don't say too much, let's do a demonstration directly for everyone.

def func(x):
    if x < 18:
        print("未成年")
        return "未成年"
    else:
        print("成年")
        return "成年"
        
x = 20
func(x)

For example, with the above piece of code, let's take a look at the execution. Please add image description
The process of changing the number of programs is vividly presented to you, which is clear enough.

Guess you like

Origin blog.csdn.net/weixin_41261833/article/details/120214651