How To Create, Save And Run A Python Program – Full Details

How to create, save and run a Python program:

How to create, save and run a Python program
Python Program

This lesson will show you how to create, save and run a Python program. 

Through this article, you will learn how to select an editor for writing Python code and how to use them to create a Python program, save a Python program to a file, and execute the program written to a file.

How to write a python program

To write a Python program, first, open an editor, write the Python code, and then save the file. 

Then save the file containing the Python program with the extension .py. After saving, you can also execute or edit the program written in the file.

The following describes how to create, save, and execute a Python program using Notepad ++.

Launch an editor for Python and create a Python program

After launching Notepad ++, let’s write the program code directly to the software screen.

print ( “Your name” ) 

yourname = input ( “>>” ) 

print ( “Hello!” , Yourname, “san” )

The Notepad ++ screen looks like this:

Notepad ++

Notepad ++

Set the programming language with Notepad ++

As mentioned above, just writing the source code does not recognize which language you are using, Notepad ++. Therefore, useful functions of Notepad ++ such as automatic color adjustment are not available at this stage.

To use that feature, you need to set the programming language you are using. You can use either of the following methods, so let’s set the programming language.

  • Method 1: Click * [Language> P> Python] *
  • Method 2: Press the * [Alt + L> P> Python] * key combination
  • Set the programming language with Notepad ++
  • Set the programming language with Notepad ++
  • The font color will then change automatically, making it easier to distinguish the commands as shown below.
  • Set the programming language with Notepad ++
  • Set the programming language with Notepad ++

Save the python program to a file

By default, Python programs are .pywritten and saved in files with extensions. For example, [check_id.py] or [sample.py].

filename..py

After saving the program with a file extension, you can either run the python file or do not edit the recorded program.

For example, save the python file you wrote above as [sample.py].

Here’s how to save a Python file in Notepad ++.

  • Click File> Save All and enter Filename sample.py.
  • In the Save as type box, select the file format as a Python file (.py: .pyw).
  • In the Save to box, select the path and choose the location where you want to save the file. In this example, suppose you save the file to [D: code python sample.py].
  • Finally, click Save to finish saving the file.

Save the python program to a file

How to run python

After saving your program to a Python file, there are various ways to execute the Python code stored in that file. There are usually two methods:

Run python with cmd

To execute a python file with cmd, there is a condition that the python path is added to the environment variable PATH. If you haven’t set it yet, refer to the following article and add the python path to the environment variable PATH.

Set the environment variable PATH

After adding the python path to the environment variable PATH,

you can run the python file with cmd as follows :

For example, suppose you execute the prepared [sample.py] with cmd. Let’s do it in the following order.

Start DOS or PowerShell. Then you will see a cmd screen with the default directory as shown below.

C: Users Aoudumber>

cd/d folder_pathExecute the command with to change the current directory to the folder where python is stored. 

C: Users Aoudumber> cd / d D: code python 

D: code python>

python sample.pyExecute the Python file with the command.

python sample.py

Then, the Python file is processed, and the result of the Python program written in it is displayed as shown in the following screen.

Run python with cmd

How to run Python files with Anaconda

Running a Python file at the anaconda prompt is similar to running a Python file at the cmd. 

Use the command to move your working directory to the directory that contains the python file you want to run, then run the python file.

The difference is that instead of typing the command on the cmd screen, you start an Anaconda prompt and execute the command directly on this screen.

C: Users Kiyoshi> cd / d D: code python 

D: code python> python sample.py

Then, the Python file is processed, and the result of the Python program written in it is displayed as shown in the following screen.

How to run Python files with Anaconda

So, to run a Python file, you need to do two things:

  • cd/d folder_pathThe command will move you to the folder where the Python files are stored.
  • python filename.Pyrus the python file with the command.

summary

Above Aoudumber explained how to create, save and run a Python program. Practice using each example sentence to better understand the content of the lesson.

And let’s learn more about Python knowledge in the next lesson.

I'm a BCA student formed an obsession with Blogging, And I Will Provide You With Content Related To Health, Tech, Learning, Gaming, SEO, To Build Amazing Knowledge.

Leave a Comment

Sharing Is Caring: