Python IDE: PyCharm Tutorial for Beginners

Flat vector illustration. Man and woman installing PyCharm IDE on the computer.

The PyCharm IDE is one of the most popular editors used by professional Python developers and programmers. But if you are a beginner, don’t worry, it can help you too! Find out how to create Python projects using PyCharm and what basic features can help you write code more efficiently.

The PyCharm IDE is one of the most popular editors used by professional Python developers and programmers. The vast number of PyCharm features doesn’t make this IDE difficult to use–just the opposite. Many of the features help make Pycharm a great Python IDE for beginners.

If you are just starting to learn Python, you should try out different Python IDEs to see if you prefer working with PyCharm or with another editor.

In my previous article, I showed you how to install the PyCharm IDE on Windows and Ubuntu. In this article, you’ll find out how to create a program and run it using PyCharm. You’ll also get to know some of the features that help beginners write code.

After the PyCharm IDE, we’ll move on to other popular Python IDEs and review them. Follow our blog or subscribe at the bottom to get the hang of other IDEs like Spyder, Jupyter, and more!

How to create and run Python scripts in PyCharm

To create a Python script right click on the project name node and choose from the pop-up menu New › Python File.

PyCharm

The New Python file window appears.Enter the name of the file in the Name field.

PyCharm

Click OK and you are ready to code in the PyCharm editor.

PyCharm

Copy this example code into the editor:

def hello():
	print("Hello!")

hello()

Save the file by clicking on the title tab,

PyCharm

then pressing Ctrl+S or by clicking File › Save All.

PyCharm

How to Run Python Code in PyCharm

We can run a project or a single file by selecting the project/file node and choosing from the menu Run › Run (the green Run button) or by using a keyboard combination Alt+Shift+F10.

The Run window appears:

PyCharm

Click the name of your script.

At the bottom of the editor the Output window with the result will be shown.

PyCharm

User Interface of the Editor

In almost all Python IDEs, the working area is divided into several parts that help make working with files and code more convenient. The screen below illustrates these different parts and what they are doing.

PyCharm

On the top is the main menu, which contains commands like open file, open project, create project or file, run, debug code, etc. Under the main menu is a toolbar with the same functionality, but it provides quick access to these functions. At the bottom is a status bar that displays messages about errors, warnings, and the status of a project. The central part of the screen consists of the editor where you read and write code. To the left, right, and bottom of the editor are tool windows. They allow for many different tasks, for example: see the node of the project and quickly open it, search in files, run options, change debugging settings, etc.

How to Customize the PyCharm Theme and Color Scheme

The PyCharm IDE allows you to customize many options. The first custom function is the theme of the IDE. You have a choice among different themes. To access the themes click on File › Settings.

PyCharm

The Settings window appears. From the left pane, expand the Editor and Color Scheme menus.

PyCharm

In the right pane select from the Scheme drop-down list one of the scheme names. For example it may be currently set to the Default scheme. Please note: the default color in PyCharm is Darcula and here, Default means your OS’s default theme.

PyCharm

Click the OK button.

PyCharm

If the Change PyCharm Theme window appears, click the Yes button. You will see a new theme like this below.

PyCharm

You can also change font colors in the scheme. You’ll find all the available options in the Settings window.

PyCharm Features with Shortcuts

Code Highlighting

The main feature that helps developers in writing code is highlighting. The editor highlights symbols and keywords in the code. It helps a developer to find these words in the script fast and it helps to notice errors. For example strings are green in color, integers are in blue, functions are in dark blue, etc. You can also change colors for groups of words in the settings of the IDE. Syntax highlighting improves the readability of the code.

PyCharm

If you click on the symbol or word, all occurences of this word or symbol will be highlighted.

PyCharm

PyCharm also helps you with errors. If you make a mistake in the code, this word will be indicated by red underlining. Hovering over this word with the mouse displays a tooltip with details.

PyCharm

Code Completion

Another very important feature, especially if you are just starting to learn Python, is code completion. While writing code you don’t always remember all the keywords in Python. If you start writing a word, the IDE gives you suggestions in a pop-up list, from which you can choose the appropriate word. It’s very convenient.

You can invoke code completion with the Ctrl+Space command.

If you write code without importing a required package, you’ll see this underlined in red. Select the word and press Alt+Enter or click the red bulb.

PyCharm

The IDE helps by suggesting that you import this package. Click on it and the line containing import will be inserted into the code. Now you can use the code with functions from the package math.

PyCharm

PyCharm also helps you remove unused imports if you remove code that depended on a package. If you see an orange bulb, expand it and choose the Optimize imports option.

PyCharm

You can do it also by pressing Ctrl+Alt+O or by choosing from the menu Code › Optimize imports.

Input Definition

The next helpful feature is the ability to see the definition of a function. If, for example, you type input, but forgot how many and which parameters this function takes, you can display the documentation about this function. Just select the word and choose from the menu View › Quick Definition.

PyCharm

You can also choose Quick Documentation.

PyCharm

Expression Type

Python doesn’t require declaration of data types for variables. However, during writing many lines of code, sometimes you need to find out what data type is assigned to a variable. In PyCharm it is easy. Select the variable and choose from the menu View › Expression type.

PyCharm

Of course you can also use the keyboard shortcut Ctrl+Alt+P. In a popup window you see the data type.

PyCharm

Code Folding

The next useful feature is hiding parts of code. If your code is long, it is very useful to hide sections of code to improve readability. You can select a block of code to hide and choose from the menu Code › Folding › Fold Selection.

PyCharm

The selected code will be folded.

PyCharm

If you click the plus symbol (“+”), you expand the block of code.

PyCharm

Code Constructs with Surround Templates

PyCharm provides adding surrounding fragments of code with constructs based on the language of the source code like if. For example, you can write only the condition for an if statement and select it.

PyCharm

Then you can choose from the menu Code › Surround With or press Ctrl+Alt+T.

PyCharm

The list of statements appears.

PyCharm

If you choose if expr, your selected code will be inserted as the condition of an if expression.

PyCharm

Code Inspection

In PyCharm you can also easily navigate to the declaration of a variable, function, etc. Select the name of an object and press Ctrl+B. In the screen below we selected the a variable.

PyCharm

PyCharm allows for analysis of code by inspection. It detects compiling errors, unused code, memory leaks, and other problems. By default it analyses all open files and highlights all detected problems. Just select from the menu Code › Inspect Code.

PyCharm

The Specify Inspection Scope window appears, where you can choose to inspect all of a project or one file.

PyCharm

Click the OK button.

At the bottom of the IDE you see errors and warnings. In our script there are missing spaces around operators and whitespace before the ‘:’ character.

PyCharm

If there are not any errors or warnings, you see a message about it.

PyCharm

How to Install Packages in the PyCharm IDE

PyCharm is similar to other Python IDEs in that it allows you to install packages and modules. In PyCharm this is a very easy process. First choose from the menu File › Settings › Project: project name › Project Interpreter

You will see the Settings window:

PyCharm

Here is displayed a list of installed packages. To install a new package, click the plus icon, which is next to the names of the columns. The Available Packages window is displayed. Find the package and click the Install Package button at the bottom of this window.

PyCharm

You see the new package in the list.

PyCharm

Click the OK button.You are ready to use the package in your project! :)

Summary

This article is a short beginner overview of using the PyCharm IDE for growing your Python programming skills. I showed you how the PyCharm user interface works, how to run scripts, and what features can help you in writing code.

Please note, this editor contains many more features and tools, but I discussed only the most useful functions available in the Community version of the editor. If you haven’t used an IDE before, maybe PyCharm will turn out to be your favorite for writing Python scripts. Try it out and have fun!

Dorota Wdzięczna

Data Science Writer @ Vertabelo

comments powered by Disqus