How to Run Python Code?

How to Run Python Code?


Python may be a flexible computer programming language. There are a number of ways to use it counting on our particular task. One thing that differentiates Python from other programming languages is that it’s interpreted instead of compiled. This proposes that it’s executed line by line that permits programming to be interactive in a way that’s indirectly possible with compiled languages like Fortran, C, or Java. In this post I will describe four main ways we’ll run Python code:

  • The Python interpreter,
  • The IPython interpreter,
  • Via Self-contained Scripts,
  • Within the Jupyter notebook.

The Python Interpreter

The most basic thanks to executing Python code is line by line within the Python interpreter. The Python interpreter is often started by installing the Python language and typing python at the prompt. Look for the Terminal on Mac OS X and UNIX or Linux systems or the prompt application in Windows.

$ python

Python 3.5.1 |Continuum Analytics, Inc.|

Type "help", "copyright", "credits" or "license"


We’ll begin to type and execute code snippets with the interpreter running. At this point we’ll use the interpreter as an easy calculator, carrying out calculations and assigning values to variables:

>>> 1 + 1


>>> x = 5

>>> x * 3


The interpreter makes it very suitable to undertake out small snippets of Python code and to experiment with short sequences of operations.

The IPython interpreter

We will find that it lacks many of the features of a full-fledged interactive development environment if we spend much time with the vital Python interpreter. Another interpreter called IPython for Interactive Python is bundled with the Anaconda distribution and includes a number of convenient improvements to the vital Python interpreter. It is frequently started by typing ipython at the command prompt:

$ ipython

Python 3.5.1 |Continuum Analytics, Inc.|

Type “copyright”, “credits” or “license”

IPython 4.0.0 — An enhanced Interactive Python.

? -> Introduction and overview of IPython’s features.

%quickref -> Quick reference.

help -> Python’s own help system.

object? -> Details about ‘object’, use ‘object??’ for extra details.

In [1]:

The key aesthetic difference between the Python interpreter and therefore the improved IPython interpreter lies within the command prompt:

Python uses>>> by default, whereas IPython uses numbered commands (e.g. In [1]). Irrespective, we will execute code line by line even as we did before:

In [1]: 1 + 1

Out[1]: 2

In [2]: x = 5

In [3]: x * 3

Out[3]: 15

Note that even by way the input is numbered, the output of every command is numbered similarly. IPython makes available a good array of useful features as for a few proposals on where to read more.

Self-contained Python scripts

Running Python snippets line by line is helpful in particular cases. It’s additional convenient to save lots of code to file, and execute it all directly except for more complicated programs. By agreement, Python scripts are saved in files with an extension. For instance, let’s make a script called which covers the following:

  • file:

x = 5

print("Result is", 3 * x)

To run this file, we confirm it’s within the current directory and sort the python filename at the command prompt:

$ python


Result is 15

For more difficult programs, making self-contained scripts like this one may be a must.

The Jupyter notebook

The Jupyter notebook is a self-contained script and a very useful hybrid of the interactive terminal. This allows the following;

Executable code

Formatted text

Interactive structures to be combined into one document

However the notebook started as a Python-only format, it’s meanwhile been made compatible by an outsized number of programming languages. This is now an important part of the Jupyter Project. The notebook is helpful both as a development environment and as a way of sharing work via rich computational and data-driven narratives. That is blend together code, figures, data, and text.

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top