In this tutorial, we will be creating a python project, python file and runs it using PyCharm.

Prerequisites :

  • You have already installed PyCharm Community or Professional. Visit PyCharm installation guideline
  • If you’re using macOS or Linux, your computer already has python installed or else you can get it from python.org.

Creating a python project

PyCharm opens a welcome page and you can click on new project.

Pycharm Open window

A new window opens as we select new project :

New project creation in pycharm

These can be kept as default, or you can change as you wish.

You can choose project location, for that click on location field and you can specify the directory of your project.

Set new environment as Virtualenv.Specify the location used for new environment. For base interpreter, specify the path to the python executable file.Check the boxes if necessary.Deselect the ‘create a main.py welcome script’ box.

Click the create option.If PyCharm detects no Python on your machine, it will provide two options :

  • To download the latest Python versions from python.org
  • To specify a path to the Python executable. 

Here Python 3.8 version is downloading.

python downloading inside pycharm

Creating a python file

A new window opens, select the project root (here pythonproject1).

Python project creation in pycharm

Select File -> New.. -> File or press alt + insert.

new python files

Type the new file name with extension .py

adding .py extension to python file created in pycharm

A new python file is created and is open for editing.

window opened for pycharm

Now lets write some code.

Given below is a code for finding area when dimensions are given.

You can either copy or type it yourself.

def area_calc():
    _input_ = input("Enter the shape you want to calculate area of: ")
    area = 0
    pie = 3.14
    if _input_ == "Square":
        side = int(input("Enter the value of side: "))
        area = area + (side ** 2)
    elif _input_ == "Circle":
        radius = int(input("Enter the value of radius: "))
        area = area + (2 * pie * radius)
    elif _input_ == "Rectangle":
        length = int(input("Enter the value of length: "))
        width = int(input("Enter the value of breadth: "))
        area = area + (length * width)
    elif _input_ == "Triangle":
        base = int(input("Enter the value of base: "))
        height = int(input("Enter the value of height: "))
        area = area + (0.5 * base * height)
    else:
        print("Select a valid shape")
    print("%.2f" % area)


area_calc()

If you’re typing, you can see PyCharm helps to complete your lines, informs you about the missing semicolons, indentations etc.

You can notice the inspection indications on the right side –

  • Green – everything is OK and you can continue coding
  • Yellow – some minor errors but won’t affect while compilation
  • Red – you have some serious errors

As you get a tick mark, you can go for running your code.

Running the application

To run the code, we can right-click on editor and choose run ‘filename’ or press shift + ctrl + f10.

A console appears in the bottom where errors appear (if any) and referring to the line numbers you can correct the code.

When all the errors are corrected the output is displayed.

And now your first python file is executed.

Happy coding ..!!

Author

Write A Comment