Friday, December 1, 2023
HomeFun With ProgrammingHow To Run Python Scripts Step By Step?

How To Run Python Scripts Step By Step?

As you know if you are plan to enter the world of python programming the first and most important thing you should understand is how to run Python Scripts or code.

So In this article, we are going to discuss this thing. But before then that letโ€™s understand python. Nowadays python is being one of the leading programming languages. It has a very easy syntax which makes it easier for those who are their initial stage of learning. It is also very useful to handle large databases and data science projects.

letsโ€™s understand the difference between code and scripts

  • Code: Code is a language that is converted from human language into, machine language that computers can understand.
  • Scripts: Script is a simple program stored in plain file text that contains python code.

how to run python scripts

Step To Run Python Scripts

Python Interpreter :

The interpreter is a layer of software that works as a bridge between program and hardware to run the code. A python interpreter is an application that is responsible for a handle or runs the python scripts.

The python interpreter work on REPL ( Read Eval Print Loop ) environment.

  1. Read the command
  2. Evaluates the command
  3. Print the result
  4. Loopback and process the repeatedly.

Starting the Python Interpreter

  • The simplest and easy way to start the interpreter is to first open the terminal from the command line.
  • To open the command-line interpreter
  • In window to go to the command prompt go to start menu -> Run(Press Windows + R) and type cmd
  • For Linux It is accessible by many applications like xterm Gnome Terminal or Konsole.
  • For Mac OS X it is accessible through Applications โ†’ Utilities โ†’ Terminal

Running Python Code Interactively

To run a python by an interactive session is a broadly used way. It is an excellent development tool with the language that allows you to test every piece of code at a time on the go.

To start the interactive session type python in the command line or terminal and press ENTER key from the keyboard.

The image has shown below how to do this on windows

run python

The example has shown below how to execute code in interactive session

run python scripts

The only disadvantage of that is when you close the interactive session the code will no longer exist.

The PEM (Python Execution Model) is given the multi step process to run python scripts.

  1. First of all the statement or the expression of your scripts are processed in a sequential manner by the interpreter.
  2. After than code is compiled into a set of instructions called byte code.
  3. Bytecode is an intermediate machine-independent code that optimizes the process of code execution.
  4. Thatโ€™s why the interpreter ignores the compilation step when executing the code for the next time.
  5. At last, the interpreter transfer the code for execution.
  6. The Python Virtual Machine(PVM) is a part of the python environment that is installed in your system.
  7. PVM loads the code in python runtime and reads each and every operation and it is the component that actually runs your scripts.

Run Python Scripts Using Command Line

  • You can also run your code or scripts by using a plain text editor.
  • As we said above the code written in interactive session has lost when the session is closed.
  • If you are at the learning stage of python you can use an editor like sublime or Notepad++ which is very easy to use
  • Now you need to open any editor which you like and write the following code: print(โ€˜Hello World!!!โ€™)
  • After writing the code save the file with the extension .py.
  • Only you need to give a py extension.

Run Using Python Command

  • The most basic and easy way to run python scripts is by using python command.
  • Open the command line end type the python followed with the file name like this
  • python

So this is the various way step by step how you can run python scripts.

Read More:–

Suhashini Mishra
Suhashini Mishra
Suhasini Mishra from India passionate UI UX Developer | Content Writer | Content Researcher | Ionic Developer


Please enter your comment!
Please enter your name here

- Advertisment -

Stay Connected


Most Popular

Recent Comments