PyCharm

PyCharm – Improving and Writing Code

PyCharm – Improving and Writing Code

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():
    def __init__(self):
        print("Try has been invoked! - Parent class")
    def gen_table(self, n):
        for i in range(1,11):
            print(n, " * ", i, " = ", n*i)

class child(Try):
    def __init__(self):
        print("This is child class")
        var = Try()
    def take(self, num):
        self.gen_table(num)

ob = child()
ob.take(9)

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.

 

Top course recommendations for you

    Docker Swarm
    1 hrs
    Beginner
    1.8K+ Learners
    4.5  (126)
    Factorial Program in C
    2 hrs
    Beginner
    5.3K+ Learners
    4.46  (556)
    Jenkins Tutorial
    1 hrs
    Beginner
    8.7K+ Learners
    4.53  (710)
    Dockerize Spring Boot Application
    1 hrs
    Intermediate
    4.1K+ Learners
    4.46  (325)
    Python Data Structures
    1 hrs
    Beginner
    31.8K+ Learners
    4.5  (2546)
    Fibonacci Series in Java
    2 hrs
    Beginner
    2.8K+ Learners
    4.39  (69)
    Priority Queue in C++
    1 hrs
    Beginner
    2.2K+ Learners
    4.38  (110)
    Introduction to MATLAB
    2 hrs
    Beginner
    22.5K+ Learners
    4.41  (1798)
    Packages in Python
    1 hrs
    Beginner
    7.4K+ Learners
    4.33  (568)
    Palindrome in Python
    2 hrs
    Beginner
    2.9K+ Learners
    4.65  (92)