First Python Project
First Python Project
3. Choose the project location. Click button next to the Location field and specify the
directory for your project.
4. Also, deselect the Create a main.py welcome script checkbox because you will create a
new Python file for this tutorial.
5. Python 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 environment.
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 details.
2. Select the option Python File from the context menu, and then type the new filename.
PyCharm creates a new Python file and opens it for editing.
Edit Python code
Let's start editing the Python file you've just created.
1. Start 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, Car.
2. PyCharm 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 window.
3. Let'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 indentation.
4. If 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:
5. Let's copy and paste the entire code sample. Hover the mouse cursor over the upper-right
corner of the code block below, click the copy icon, and then paste the code into the
PyCharm editor replacing the content of the Car.py file:
This application is intended for Python 3
class Car:
def say_state(self):
print("I'm going {} kph!".format(self.speed))
def accelerate(self):
self.speed += 5
def brake(self):
if self.speed < 5:
self.speed = 0
else:
self.speed -= 5
def step(self):
self.odometer += self.speed
self.time += 1
def average_speed(self):
if self.time != 0:
return self.odometer / self.time
else:
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?").upper()
if action not in "ABOS" or len(action) != 1:
print("I don't know how to do that")
continue
if action == 'A':
my_car.accelerate()
elif action == 'B':
my_car.brake()
elif action == 'O':
print("The car has driven {} kilometers".format(my_car.odometer))
elif action == 'S':
print("The car's average speed was {}
kph".format(my_car.average_speed()))
my_car.step()
my_car.say_state()
At this point, you're ready to run your first Python application in PyCharm.
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 window.
Here you can enter the expected values and preview the script output.
Summary
Congratulations on completing your first script in PyCharm! Let's repeat what you've done with
the help of PyCharm:
Created a project.
Created a file in the project.
Created the source code.
Ran this source code.