How to write and execute Python code in 2022

1 min

To run Python code, you will need to execute the script from your terminal using the Python Interpreter.

python my_python_script.py
How to execute a python script from your terminal

The latest version of the Python interpreter can be installed from the main Python website here.

Python can be written with any text editor out there.

However, the vast majority of natively installed text editors won't be the best solution for you to write code.

There are many great text editors made especially for coding.

Those "made for coding" text editors are called integrated development environments or IDE.

Here is a list of the few that are great for coding in Python as of today:

  1. Visual Studio Code (Free) - Made by Microsoft
  2. Atom (Free) - Made by Google
  3. Sublime Text (Free / Paid)
  4. PyCharm (Free / Paid)

Visual Studio Code is great and the IDE that a lot of beginner falls back to.

Write .py files

What are .py files? It's basically the same thing as .txt files but ends with .py.

It gives the information to your operating system that those files contain Python code and that they can be run by the Python interpreter.

We usually call a .py file a Python script.

Here is an example of Python script

my_name = "james"

print(my_name)
Here is the content of a my_python_script.py

How to execute a Python script

Once we have our Python script ready, we execute it in terminal like so:

python my_python_script.py
How to execute a python script from your terminal

Here you are! You now know how to write Python code and how to execute it.

More on fundamentals

If you want to know more about Python fundamentals without headaches... check out the other articles I wrote by clicking just here:

Fundamentals - The Python You Need
We gathered the only Python essentials that you will probably ever need.