10.1 Treads & Variables
While debugging the program using a break point
or stepping through the code, you can always check the values of variables known at that point in the program.
Let's write a program that fills an array of 10 elements with numbers from 100 to 109.
Example:
![](https://cdn.javarush.com/images/article/c3fdf475-4bf9-4baf-9030-70af795ff7af/512.jpeg)
PyCharm's smart system shows the values of important variables right above the code. In our case, it's the list variable data
.
Also, at the bottom of the screenshot, we have the Threads & Variables
tab open (instead of Console
), displaying all known variables (with their values) at this point in the program.
If you press F8 10 times, you'll run 5 iterations of the loop (one press for the loop header and one for the loop body). You'll get the following result:
![](https://cdn.javarush.com/images/article/17bc749a-f9f8-4a97-bebd-b5f1168c6081/512.jpeg)
5 iterations of the loop out of 10 have run, and you can see that the array data
already has 5 values: 100, 101, 102, 103, and 104.
By the way, if you pay attention to the variables panel, you can see a few more useful variables there:
![](https://cdn.javarush.com/images/article/8b13ad0c-3992-4ee3-8cd1-133e34f90af4/512.jpeg)
10.2 Changing Variable Values
If you want to test how your program behaves with certain variable values, you can simply change the values of any variables right during the program's execution (in debug mode).
To do this, right-click on the variable name or press F2:
![](https://cdn.javarush.com/images/article/cd72e45d-9429-47ae-b5b7-28242dd0e07f/512.jpeg)
Then you just type the new value of the variable and press Enter — and that's it:
![](https://cdn.javarush.com/images/article/831064fc-407d-4967-88d9-9e2120fbdca4/512.jpeg)
Or even like this:
![](https://cdn.javarush.com/images/article/e9070af1-afa2-4c89-b08d-549e13787719/512.jpeg)
Press Enter — and that's it, now the program uses the new value of your variable.
Changing variable values during debugging allows you to test different scenarios of the program's behavior, which is especially useful when working with complex logic.
![](https://cdn.javarush.com/images/article/d3dc8346-cc0e-4fbe-9469-e8851f06d902/512.jpeg)
10.3 Executing Code Fragments
At any point during the program's execution, you can run arbitrary code. This is done with the Alt+F8 (Option+F8) shortcut or from the context menu:
![](https://cdn.javarush.com/images/article/3039dc25-fe2a-415c-ba90-4be6b3ea2995/512.jpeg)
You'll get a special window where you can write any code, and it can use variables known at the current point in the program's execution!
You can call any methods: say, make the program print some text on the screen without interrupting its work! Example:
![](https://cdn.javarush.com/images/article/5aebfce2-24da-41a4-863a-efb7badc376d/512.jpeg)
You just learned probably 5% of all the features of PyCharm. Once you master them, we'll talk about the rest.
GO TO FULL VERSION