PyCharm Tutorial: Getting Started

PyCharm, created by the Czech company JetBrains, is a popular Integrated Development Environment (IDE) used in programming, particularly for the Python programming language. It is written in Java and Python, and its initial release was in February of 2010. PyCharm works with Windows, macOS, and Linux versions.

It provides a graphical debugger, an integrated unit tester, coding assistance, support for web development with Django, and integration with Anaconda’s data science platform.

Learn the essentials of object-oriented programming, web development with Django, and more with the Python Training Course. Enroll now!

This tutorial on PyCharm will help you learn how to create new projects, add files to projects, customize the UI, run and debug Python code, and explore a lot of other features.

You can download PyCharm from JetBrains website.

Pycharm

Once you click on download, it will give you two options: Professional or Community. The professional edition of PyCharm requires a subscription, while the community edition is free.

download-pycharm

For this tutorial, we will use the community edition. This will start downloading the .exe file. After the download is complete, run the file to install PyCharm.

open-file

edition-setup

Click on Finish to complete the PyCharm Community edition setup. 

completing-edition-setup

In the PyCharm UI, your options include File, Edit, View, Navigate, Code, Tools, VCS, Window, and Help.

pycharm-ui

If you want to make changes to the environment and the interface, click on File and go to Settings.

clickonfile

settings

Now, to write your Python script, go to File and select New Scratch File.

new-scratch-file

Next, let's create a new project by going to File and selecting New Project. Choose a  name for the project and select create. You will see a pop-up asking how you want to open the project. I’ll choose This Window.

open-project 

It will take a while to create a virtual environment. Once it’s ready, start by naming your project, and then, right-click it, go to New and select Python file. Name the Python file you have created. In our example, we’ve named it pycharm.

pycharm-tutorial-1

pycharmtutorial-2

At the bottom of the PyCharm window, you will find the Python Console, Terminal, and TODO options. 

Let us perform some basic mathematical operations by importing the math module. It uses a full function (abs) to return the total value of a number.

math

You can see the Add Configuration option has changed to the name of the Python file. Next, click the green run button to execute your code. The Add Configuration will be reset every time you create a new project. There is a Run tab that comes up once your code executes successfully to display the output.

In the Python console, you can run one line of code, which will generate output beneath it.

python-console

PyCharm has a Refractor option, which allows you to rename a variable throughout the code. To do this, select the variable name, right-click and choose Refractor, and then click Rename.

refractor

Now, you will see how to import the NumPy module in PyCharm. However, if I use the following lines of code to import NumPy, it will throw an error.

numpy

To install NumPy on PyCharm, click on File and go to the Settings. Under Settings, choose your Python project and select Python Interpreter. Then, search for the NumPy package and click Install Package.

appearance-and-behavior

available-packages

Now, let's rerun the code, and you can see this time that it executed successfully.

run-the-code

You can close your project by going to the File menu and selecting the Close Project.

If you want to open any file in PyCharm, select Open from the File menu and browse to the correct one. It will ask, “How would you like to open the project?”.  Select the method that works for you. 

You can edit the configuration of a project by selecting the name of the file and clicking on Edit Configurations.

/edit-configurations

This allows you to edit the Environment variables, choose the type of Python interpreter you need, select the Working directory, and so on.

working-directory

PyCharm offers multiple ways to run the code:

  1. Click on the Run tab on the menu bar.
  2. Select the green run button.
  3. Hit Shift+F10.
  4. Right-click and select Run.

You can select each line of code, and it will show a red-colored point, which refers to the breakpoint.

breakpoint

To debug a program, select the bug option present on the top right or click on Run from the menu bar and choose Debug. It will execute until the breakpoint, which can be continued until completion by pressing F8.

debug

Below is a small script that can help you understand more about how PyCharm works. Once you run this program, it will prompt you for inputs in the console.

st = input(“Enter X and Y”)

X, Y=[int(x) for x in st.split(‘,’)]

throw-an-error

If you try to give anything other than integer as inputs, it will throw an error.

console

Let’s create a list and see how it works:

st = input(“Enter X and Y”)

X, Y=[int(x) for x in st.split(‘,’)]

lst = [[0 for col in range(Y)] for row in range(X)]

print(lst)

how-it-works

It results in a nested list with four zeros in each list.

Let's look at another example to see how you can loop through a list:

st = input(“Enter X and Y”)

X, Y=[int(x) for x in st.split(‘,’)]

lst = [[0 for col in range(Y)] for row in range(X)]

for row in range(X):
    for col in range(Y):

        lst[row][col] = row*col

print(lst) loop through

The following code will print the elements in a list horizontally:

st = input(“Enter X and Y”)

X, Y=[int(x) for x in st.split(‘,’)]

lst = [[0 for col in range(Y)] for row in range(X)]

for row in range(X):
    for col in range(Y):

        lst[row][col] = row*col

for row in range(X):

    for col in range(Y):

        print(lst[row][col])

following-code

The code below allows users to print each row with one underneath the other.

st = input(“Enter X and Y”)

X, Y=[int(x) for x in st.split(‘,’)]

lst = [[0 for col in range(Y)] for row in range(X)]

for row in range(X):
    for col in range(Y):

        lst[row][col] = row*col

for row in range(X):

    print(lst[row])

underneath-row

Python has wonderful data visualization libraries, such as Matplotlib, that help you build interactive graphs and charts. Go to Settings and in Python Interpreter, search for Matplotlib, and click on Install Package.

Here is a simple straight-line graph plotted using Matplotlib:

import matplotlib.pyplot as plt

plt.plot([1,2,3,4],[1,2,3,4])

plt.show() 

matplotlib

Pandas is a widely popular library used for data analysis and manipulation in Python. You need to import the pandas package to use it. To do so, go to File menu >> Settings >> Python Interpreter >> Search for pandas >> install package.

The program below will help you import a CSV file onto PyCharm and store it as a dataframe using the pandas library.

import pandas as pd

path = “D:/cars.csv”

df = pd.read_csv(path)

print(df)

import-pandas

If you want to see the first five records of the dataframe, use the head() function.

dataframe

To learn more on PyCharm, refer to the following video:

Conclusion

We hope this blog helped you understand how to work with the Python language using the PyCharm IDE. You would've learned how to download and set up the PyCharm IDE, create a project and Python file, configure the settings, write simple code in Python and run it in various ways. You got an idea of how to install packages, such as NumPy, Matplotlib, and pandas. Finally, you imported a csv file onto PyCharm using pandas. That’s just some basics. If you want to learn more complex Python coding, enroll in our Python Training Course today!

About the Author

Avijeet BiswalAvijeet Biswal

Avijeet is a Senior Research Analyst at Simplilearn. Passionate about Data Analytics, Machine Learning, and Deep Learning, Avijeet is also interested in politics, cricket, and football.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.