# Running Python
## Getting Started Before we begin, please ensure that you have successfully followed the **installation** steps and miniforge is installed on your machine. Now navigate to the folder `ip_python` that we created during the **installation** session and create a new folder called `introduction` under it. We will use `ip_python/introduction` as our working directory for this session. Open the command-line terminal (or the `Miniforge prompt` on windows) and then navigate to the folder `ip_python/introduction` using the `cd` command. Finally, activate the **ipp_analysis** environment: ```bash conda activate ipp_analysis ``` At this point, the command-line terminal should look something like this: ```console (ipp_analysis) C:\ip_python\introduction> ``` We're all set for executing Python code. ## Executing Python There are several ways for executing Python code, each having its own merits. We begin with the simplest approach i.e. the Python Shell. ### Shell To open Python Shell, simply type the command `python` in the command-line terminal. You will observe something like this: ```console Python 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:24:40) [GCC 13.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ``` The command-line terminal has successfully launched the Python Shell and it is ready to execute Python code. Let's execute the `print` function: ```bash >>> print("This is executed in the python shell") ``` The Python interpreter executes this statement and prints the following message on the command-line terminal as instructed: ```console This is executed in the python shell ``` We can try a few more statements in the Python Shell: ```bash >>> x = 2 >>> y = 4 >>> print(x + y) >>> x * y ``` Although the Python Shell is great for quickly testing a small number of Python statements, it can be a bit cumbersome to use when dealing with a large number of statements. For this reason, it's helpful to explore Python code execution in a script. Before proceeding, we must exit the Python Shell by executing the following statement: ```bash >>> exit() ``` The command-line terminal returns to the earlier state: ```console (ipp_analysis) C:\ip_python\introduction> ``` ### Script A Python script is a collection of Python statements stored in a file and the entire script can be executed using a single command. Let's try to put the same statements from the Python Shell exercise in a script. Open any text editor (e.g. notepad) and copy-paste the following statements: ```console x = 2 y = 4 print(x + y) x * y ``` Save the file under `ip_python/introduction` and name it `script.py` By convention, files with `.py` extension are Python scripts. Now, in the command-line terminal, execute the following command: ```bash python script.py ``` Python reads the file `script.py` statement-by-statement from the top to the bottom and executes them. The output from the script is shown on the command-line terminal: ```console 6 ``` Note that only the command with `print` is shown. ## IPython [IPython](https://ipython.readthedocs.io/en/stable/) is a feature-rich interactive shell built on top of the basic Python Shell that we discussed in the previous section. IPython will run your Python code just like the basic Python Shell, however you will have access to extra features when executing your code. In short, it's more "interactive" than the basic Python Shell - hence the name I(nteractive)Python. To run IPython, type the following command on your command-line terminal: ```bash ipython ``` You will observe something like this: ```console Python 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:24:40) [GCC 13.3.0] Type 'copyright', 'credits' or 'license' for more information IPython 8.31.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: ``` You can compare this console output with the one that you observed when running the basic Python Shell. You'll notice that IPython is also using the same Python Shell in the backend. In addition, it's presenting you an enhanced interface. As a start, we could begin with the same statements that we executed earlier in the basic Python Shell: ```ipython In [1]: x = 2 In [2]: y = 4 In [3]: print(x + y) In [4]: x * y ``` We can also run the script file from previous section inside the IPython shell: ```ipython In [5]: %run script.py ``` It is important to note that any variable that is created inside the script file is retained in the IPython Shell after the script has finished its execution. You can also check detailed information about a particular variable (or object as they call it in Python) that you've already created. For example, we've already created an object called **'x'** and we'd like to see some information about it: ```ipython In [6]: x? ``` You can also see the history of statements that you've executed before: ```ipython In [7]: %hist ``` To see more on what's possible with IPython: ```ipython In [8]: ? ``` To terminate IPython: ```ipython In [9]: quit ``` IPython can execute anything that can be executed in the basic Python Shell. In addition, IPython extends the basic Python Shell with a rich set of features and makes it more powerful. To know more about IPython, visit their [official website](https://ipython.readthedocs.io/en/stable/). ## JupyterLab So far we've seen how Python code can be executed via the command-line terminal interface. Next, we'll explore how [JupyterLab](https://jupyterlab.readthedocs.io/) allows us to execute Python code via a user-friendly web-based interface. ### Running JupyterLab - Open a terminal (or the `Miniforge prompt` on windows) and run the following command: ```bash jupyter lab ``` You'll notice that a web application opens up in your default web-browser and the IPython kernel from the `ipp_analysis` conda environment is visible in JupyterLab: