Python Pycharm Tutorial
Step 1. Create and run your first Python project | PyCharm
Before you startEnsure that the following prerequisites are met:You are working with PyCharm Community or ProfessionalYou have installed Python itself. If you’re using macOS or Linux, your computer already has Python installed. You can get Python from get started with PyCharm, let’s write a Python a Python project If you’re on the Welcome screen, click New Project. If you’ve already got any project open, choose from the main though you can create projects of various types in PyCharm, in this tutorial let’s create a simple Pure Python project. This template will create an empty the project location. Click button next to the Location field and specify the directory for your, deselect the Create a welcome script checkbox because you will create a new Python file for this best practice is to create a virtualenv for each project. In most cases, PyCharm create a new virtual environment automatically and you don’t need to configure anything. Still, you can preview and modify the venv options. Expand the Python Interpreter: New Virtualenv Environment node and select a tool used to create a new virtual environment. Let’s choose Virtualenv tool, and specify the location of the environment and the base Python interpreter used for the new virtual configuring the base interpreter, you need to specify the path to the Python executable. If PyCharm detects no Python on your machine, it provides two options: to download the latest Python versions from or to specify a path to the Python executable (in case of non-standard installation) to Configure a Python interpreter for more click the Create button at the bottom of the New Project dialog. If you’ve already got a project open, after clicking Create PyCharm will ask you whether to open a new project in the current window or in a new one. Choose Open in current window- this will close the current project, but you’ll be able to reopen it later. See the page Open, reopen, and close projects for a Python file In the Project tool window, select the project root (typically, it is the root node in the project tree), right-click it, and select the option Python File from the context menu, and then type the new Charm creates a new Python file and opens it for editing. Edit Python code Let’s start editing the Python file you’ve just with declaring a class. Immediately as you start typing, PyCharm suggests how to complete your line:Choose the keyword class and type the class name, Charm informs you about the missing colon, then expected indentation:Note that PyCharm analyses your code on-the-fly, the results are immediately shown in the inspection indicator in the upper-right corner of the editor. This inspection indication works like a traffic light: when it is green, everything is OK, and you can go on with your code; a yellow light means some minor problems that however will not affect compilation; but when the light is red, it means that you have some serious errors. Click it to preview the details in the Problems tool ‘s continue creating the function __init__: when you just type the opening brace, PyCharm creates the entire code construct (mandatory parameter self, closing brace and colon), and provides proper you notice any inspection warnings as you’re editing your code, click the bulb symbol to preview the list of possible fixes and recommended actions:Let’s copy and paste the entire code sample. Click the copy button in the upper-right corner of the code block here in the help page, then paste it into the PyCharm editor replacing the content of the file:
class Car:
def __init__(self, speed=0):
= speed
self. odometer = 0
= 0
def say_state(self):
print(“I’m going {} kph! “())
def accelerate(self):
+= 5
def brake(self):
if < 5:
else:
-= 5
def step(self):
self. odometer +=
+= 1
def average_speed(self):
if! = 0:
return self. odometer /
pass
if __name__ == '__main__':
my_car = Car()
print("I'm a car! ")
while True:
action = input("What should I do? [A]ccelerate, [B]rake, "
"show [O]dometer, or show average [S]peed? ")()
if action not in "ABOS" or len(action)! = 1:
print("I don't know how to do that")
continue
if action == 'A':
celerate()
elif action == 'B':
()
elif action == 'O':
print("The car has driven {} kilometers"(my_car. odometer))
elif action == 'S':
print("The car's average speed was {} kph"(my_car. average_speed()))
y_state()
At this point, you're ready to run your first Python application in your application Use either of the following ways to run your code:Right-click the editor and select from the context menu this Python script contains a main function, you can click an icon in the gutter. If you hover your mouse pointer over it, the available commands show up:If you click this icon, you'll see the popup menu of the available commands. Choose Run 'Car': PyCharm executes your code in the Run tool you can enter the expected values and preview the script that PyCharm has created a temporary run/debug configuration for the Car run/debug configuration defines the way PyCharm executes your code. You can save it to make it a permanent configuration or modify its parameters. See Run/debug configurations for more details about running Python mmaryCongratulations on completing your first script in PyCharm! Let's repeat what you've done with the help of PyCharm:Created a eated a file in the eated the source this source the next step, learn how to debug your programs in PyCharm. Last modified: 31 August 2021
Python IDE: PyCharm Tutorial for Beginners | LearnPython.com
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.
The New Python file window 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.
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 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!
What is PyCharm IDE? – Medium
Many programmers nowadays opt for Python to build software applications with concise, clean, and readable code base. They can even accelerate custom software application development by taking advantage of a number of integrated development environments (IDEs) for Python. PyCharm is one of the most widely used IDEs for Python programming language. At present, the Python IDE is being used by large enterprises like Twitter, Pinterest, HP, Symantec and tBrains has developed PyCharm as a cross-platform IDE for Python. In addition to supporting versions 2. x and 3. x of Python, PyCharm is also compatible with Windows, Linux, and macOS. At the same time, the tools and features provided by PyCharm help programmers to write a variety of software applications in Python quickly and efficiently. The developers can even customize the PyCharm UI according to their specific needs and preferences. Also, they can extend the IDE by choosing from over 50 plug-ins to meet complex project requirements. Overview of Important Features and Tools Provided by PyCharmCode EditorThe intelligent code editor provided by PyCharm enables programmers to write high quality Python code. The editor enables programmers to read code easily through colour schemes, insert indents on new lines automatically, pick the appropriate coding style, and avail context-aware code completion suggestions. At the same time, the programmers can also use the editor to expand a code block to an expression or logical block, avail code snippets, format the code base, identify errors and misspellings, detect duplicate code, and auto-generate code. Also, the editor makes it easier for developers to analyze the code and identify the errors while writing NavigationThe smart code navigation options provided by PyCharm help programmers to edit and improve code without putting extra time and effort. The IDE makes it easier for programmers to go to a class, file and symbols, along with the go to declarations invoked from a reference. The user can even find an item in the source code, code snippet, UI element, or user action almost immediately. They can further locate usage of various symbols, and set bookmarks in the code. At the same time, the developers can even take advantage of the code navigation feature to scrutinize the code thoroughly in the lens factoringPyCharm makes it easier for developers to implement both local and global changes quickly and efficiently. The developers can even take advantage of the refactoring options provided by the IDE while writing plain Python code and working with Python frameworks. They can avail the rename and move refactoring for files, classes, functions, methods, properties, parameters, and local/global variables. Likewise, they can improve code quality by extracting variables, fields, constants, and parameters. Also, PyCharm allows programmers to break up longer classes and methods through extract pport for Popular Web TechnologiesPyCharm makes it easier for programmers to write various web applications in Python supporting widely used web technologies like HTML, CSS, JavaScript, TypeScript and CoffeeScript. The web developers can use the live editing preview option provided by the IDE to view a single web page simultaneously in the editor and browser. At the same time, the live edit feature provided by the IDE enables programmers to see the changes made to the code instantaneously on a web browser. PyCharm further allows developers to avail a JavaScript debugger as well as CoffeeScript and TypeScript editors. It even simplifies isomorphic web application development by supporting both AngularJS and pport for Popular Python Web FrameworksIn addition to supporting commonly used web technologies, PyCharm also provides first-class support for a robust Python web framework like Django. The developers can use the IDE to avail code completion suggestions for Django tags, filters, parameters, and template variables. Also, they can gather additional information about tags and filters by referring to the quick documentation. The Python IDE even helps web developers to debug Django templates, format the code, verify the code, and manage consoles. At the same time, PyCharm also supports widely used Python web frameworks like Pyramid and Web2Py. It provides code completion and navigation options specific to Pyramid. Likewise, it allows web developers to avail code completion and navigation options while working with pport for Python Scientific LibrariesPyCharm further helps programmers to use Python more efficiently in big data and data science projects. It supports some of the widely used scientific libraries for Python — NumPy, Anaconda and Metplotlib. The developers can work efficiently with these scientific libraries by availing the interactive graphs, deep code insight, and array viewers provided by the IDE. They can even run the REPL Python console provided by PyCharm to avail robust features like on-the-fly syntax check and code inspection. At the same time, the programmers can also integrate the IDE seamlessly with IPython Notebook to create innovative solutions without putting extra time and base ToolsIn addition to supporting various Python libraries and frameworks, PyCharm allows developers to work with a number of relational databases including Oracle, SQL Server, MySQL and PostgreSQL. The developers can further use the IDE to run queries, edit SQL code, browse data, alter table data, and alter/analyze schemas. PyCharm further supports SQLAlchemy library and inject SQL code into code written in various programming languages. The professional edition of the IDE further makes it easier for developers to handle large volumes of data efficiently through data DebuggerThe visual debugger provided by the IDE helps programmers to debug Python, JavaScript, and Django code. The developers can use the inline debugger to see live debugging data directly on the editor. Likewise, they can debug multiple Python processes simultaneously and step through the code bypassing libraries. PyCharm further creates reusable and customizable configuration for each test script or debugger execution. The users even have option to facilitate remote debugging by integrating the visual debugger with remote TerminalPyCharm comes with local terminals for Windows, Linux, and macOS. The built-in terminal enables programmers to continue coding and testing without leaving the IDE. Also, the programmers can use the IDE to run Python files and configure custom Python environments according to precise project requirements. At the same time, they can run interactive Python or Django consoled directly in the IDE. The console provides useful features like code completion, automatic braces matching, and dynamic syntax change. The programmers even have option to integrate the console with both local and remote pport for Major Version Control SystemsPyCharm allows developers to work with widely used version control systems like Git, Mercurial, Perforce and SVN. It even performs complex tasks like adding, removing, and deleting files automatically. The developers even have option to avail a number of features provided by the IDE regardless of their choice of version control system — grouping individual changes into multiple change lists, setting aside the changes to be restored, monitor changes made to the code repository by various users, and check the changes made to the code before being integrated into the local ftware TestingLike other IDEs, PyCharm also comes with features and tools to simplify Python application testing. It allows developers to perform unit testing through popular Python testing frameworks like Nose, Attest and Doctests. The testers even have option to run individual or multiple test files and test classes. They can further integrate the IDE with to measure code coverage while testing the applications. While testing multi-threaded applications, the testers can use the thread concurrency visualization option provided by the IDE to control the application fully and efficiently. At the same time, PyCharm enables users to deliver high quality software by implementing behavior-driven development (BDD) Development CapabilitiesPyCharm allows developers to connect with various machines and build software applications remotely. The programmers can avail the built-in SSH console provided by the IDE to connect to machines and perform various development tasks remotely through SSH. They can even run, debug, and profile the Python applications in a remote environment by replacing the local interpreter with a remote interpreter. Also, PyCharm enables programmers to create reproducible development environments through a robust tool like Vagrant, and simplify distributed application development through Docker. The users even have option to integrate PyCharm seamlessly with issue tracking the whole, PyCharm is one of the most popular IDEs for Python. The Python programmers can use PyCharm as licensed software. However, JetBrains allows developers to choose from three distinct editions of the IDE — community, professional and educational. The developers can always curtail software development cost by opting for the community edition of PyCharm. The community edition still lacks some of the advanced features provided by the professional tBrains has been updating both editions of the Python IDE to make programmers more productive and simplify custom software development. For instance, the professional edition of PyCharm 2017. 3 makes it easier for developers to test functionality of APIs by providing a built-in REST client. Likewise, it accelerates web application development by allowing programmers to avail the new features provided by Django 2. 0. Also, it allows developers to visualize and analyze data more efficiently through a data science mode.
Frequently Asked Questions about python pycharm tutorial
Is PyCharm good for Python beginners?
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.Apr 18, 2019
How do I learn Python with PyCharm?
PyCharm is one of the most widely used IDEs for Python programming language. … x of Python, PyCharm is also compatible with Windows, Linux, and macOS. At the same time, the tools and features provided by PyCharm help programmers to write a variety of software applications in Python quickly and efficiently.