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.
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.
The New Python file window appears.Enter the name of the file in the Name field.
Click OK and you are ready to code in the PyCharm editor.
Copy this example code into the editor:
def hello(): print(“Hello!”) hello() |
Save the file by clicking on the title tab,
then pressing Ctrl+S or by clicking File › Save All.
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:
Click the name of your script.
At the bottom of the editor the Output window with the result will be shown.
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.
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.
The Settings window appears. From the left pane, expand the Editor and Color Scheme menus.
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.
Click the OK button.
If the Change PyCharm Theme window appears, click the Yes button. You will see a new theme like this below.
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.
If you click on the symbol or word, all occurences of this word or symbol will be highlighted.
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.
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.
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 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.
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.
You can also choose Quick Documentation.
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.
Of course you can also use the keyboard shortcut Ctrl+Alt+P. In a popup window you see the data type.
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.
The selected code will be folded.
If you click the plus symbol (“+”), you expand the block of code.
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.
Then you can choose from the menu Code › Surround With or press Ctrl+Alt+T.
The list of statements appears.
If you choose if expr, your selected code will be inserted as the condition of an if expression.
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 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.
The Specify Inspection Scope window appears, where you can choose to inspect all of a project or one file.
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.
If there are not any errors or warnings, you see a message about it.
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:
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.
You see the new package in the list.
Click the OK button.You are ready to use the package in your project! 🙂