Now that you are quite familiar with the user interface of PyCharm, it is time for some real task. Installing a PyCharm is of no use until you start writing the code in it. So, let’s try writing a Python code and run it.
When you click on New Project and set the project environment, the editor opens up infront of you with pre-written default code for printing ‘Hi, PyCharm’. You can try running the code using the shortcut Alt + Shift + F10 on Windows (Ctrl + Shift +R on MacOS) or directly clicking on the ‘Run’ option from the ‘Run’ tab.
Using the previously stated Keymap options, you can modify your shortcuts. This option allows you to run a file repeatedly because it repeats the last one that was ran. However, executing multiple files prevents this from happening. The development process is greatly simplified by PyCharm, and what we have seen so far is just the tip of what this potent IDE can accomplish for you as a data scientist or Python developer. We'll go into more depth on how to use it to increase productivity in the following section.
Right-click on the project in the left side and select ‘New’ and then ‘Python File’ to add a new file to your project as shown below. You can create a variety of file formats, but for now we'll stick with Python.
It will open up the following pop-up to name the new file as shown in the following snap.
Now, you can write your code in the new ‘tryit’ file now. Let’s write a short program using the concept of inheritance and run it. Following code for generating table of any number using inheritance concept has been used.
class Try(): |
Click on Run ‘tryit’ from the ‘Run’ tab and you will get the following output.
Now, when you click on the ‘Run’ tab, you will also see an option like Debug ‘tryit’. This debug option might not be of much use for such short and simple codes, but it is of great importance when dealing with complex and huge codes.
Python-specific rules for authoring code with appropriate indentations are included in PyCharm. Hence, one of the advantages of using PyCharm is that it suggests the next piece of code for completing the code construct. It also prevents you from making indentation errors.
- Code Debugging in PyCharm
As the term itself suggest, ‘debug’ means to remove the bugs from a code. The purpose of debugging is to identify the root causes of defects that were discovered in your software during testing and then modify your code to produce the precise and intended outcome. When debugging the code for any software, a data scientist, developer or Python programmer keeps an eye on how the variables interact and change. By observing how each single method, function, or even line of code affects the value and usefulness of the variables, you can look into the potential reasons for the faults in your software.
For debugging a code in PyCharm, you can either use shortcut keys or you can choose the needed ‘Debug’ option from the ‘Run’ tab. You can click ‘Shift + F9’ or ‘Alt + Shift + 9’ on Windows and ‘Ctrl + D’ on MacOS.