Python shells

Source of this lab is hosted at: https://gitlab.com/jans-workshops/python-shells

You can use python in multiple ways, not only writing the code in .py files but also interactively use any of available python shells.

Good practice is to use separate virtual environment for every task, if you do now know what virutal environment is. I recommend you to learn about: pip and virtual environments before you start this lab. So you can keep your python environment clean.

You will learn about

Here you will learn about different python shells and how to use them. Purpose of this lab is to give you the options to choose best tool which suites you, therefore we will briefly demostrate the features of each python shell.

python shell

python shell is default python interactive shell supplied with every version of python.

  1. Open the terminal emulator.

    • Mac OSx: hit [CMD]+[Space] and type terminal.app
    • Windows: hit [WIN-key]+[R] and type powershell (or you can search for powershell in start menu.
  2. Star python shell with command python (or python3)

    • You will enter into interactive shell
    Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  3. type: import this and press [Enter] and you will be presented with the zen of python.

Let’s do something useful.

Type the code from the code block into your shell and press [Enter] after each block of code you write.

  1. Import pi constant from module math
from math import pi
  1. Assign radius variable radius:
radius = 3
  1. Calculate the area of the circle

    pi * radius**2
    
    • Now your result should look like this:
    >>> pi * radius**2
    28.274333882308138
    
  2. In previous step we used direct output from the shell to display the value. This time we display the value with a meaningful message.

    1. Assign the result of previous calculation to a variable:
    area = pi * radius**2
    
    1. Print the message with result
    print(f"The area of circle with radius of {radius} cm is {area} cm^2" )
    
    • You should get the following output:
    The area of circle with radius of 2 cm is 12.566370614359172 cm^2
    
  3. Almost every python it’s good practice to write docstring for your modules, packages, classes and functions. Then one can easily read the help directly in shell instead of searching the documentation on the web. To display the help use built-in help function:

  4. To exit the shell use function:

exit()

ipython shell

Is bit more advanced shell, with command completion [Tab] and colours. To use command completion type word partially and press [Tab]:

  • e.g. Type imp hit [Tab] and result will be import

It’s not installed by default, so you need to install it with pip We will try to calculate the area of the circle again to observe the difference.

  1. Open the terminal emulator.

    • Mac OSx: hit [CMD]+[Space] and type terminal.app
    • Windows: hit [WIN-key]+[R] and type powershell (or you can search for powershell in start menu.
    • Optional step: create new virtual environment
  2. Install ipython

    pip install ipython
    
  3. Once installed, start it by using command: ipython

    • You can already notice a difference just by looking at the image.
    ../_images/ipython_1.png
  4. Import pi constant from module math

    from math import pi
    
  5. Assign radius variable radius:

radius = 3
  1. Calculate the area of the circle

    pi * radius**2
    
    • The output looks different then from standard shell
    • In [6] - marks the input
    • Out[6] - marks the output, the number is tied to input cell
    In [6]: pi * radius**2
    Out[6]: 12.566370614359172
    
  2. In previous step we used direct output from the shell to display the value. This time we display the value with a meaningful message and print it to the standard output.

    1. Assign the result of previous calculation to a variable:
    area = pi * radius**2
    
    1. Print the message with result
    print(f"The area of circle with radius of {radius} cm is {area} cm^2" )
    
    • As you can see, result of print function is not registered as Out[#]
    In [8]: print(f"The area of circle with radius of {radius} cm is {area} cm^2" )
    The area of circle with radius of 2 cm is 12.566370614359172 cm^2
    

ipython has some advantages like magic functions, you can try them bellow

  1. To view the history of your inputs type: %history

You can also manipulate with file system for example.

  1. List current directory %ls

  2. Make directory %mkdir directory

  3. Change current directory with %cd directory

    • Now you current working directory is directory
    • You can check your current working directory in python using following code:
    import os
    
    os.getcwd()
    
  4. To exit the shell use function:

exit()

Now you know how to use built in python shell.

ptpython

Is even more advanced python shell which I usually prefer. You can do everything like in ipython plus something more and it looks better. It also offers integration with ipython. Ptypython supports 2 editor modes emacs and vi. It is also not installed by default. For more information check ptpython Github page.

You can reuse venv from previous task or create new.

  1. Open the terminal emulator.

    • Mac OSx: hit [CMD]+[Space] and type terminal.app
    • Windows: hit [WIN-key]+[R] and type powershell (or you can search for powershell in start menu.
    • Optional step: create new virtual environment
  2. Install ptpython

    pip install ptpython
    
  3. Once installed, start it by using command: ptpython.

  4. Press [F2] to get into the menu

    • Turn on few features: show signature, show docstring
    ../_images/ptpython_1.png
  5. Now press [ENTER] to hide the menu.

Let’s try the calculation from previous example but let’s use pow function.

  1. Type the following code:

    from math import pi
    radius = 3
    
    • You may notice that ptpython is giving you tips while you are typing, which is pretty handy right?
  2. Let’s calculate the area and print the result.

    • Type the code and hit [Enter] after each line.
    area = pi * pow(radius, 2)
    print(f"The area of circle with radius of {radius} cm is {area} cm^2" )
    
  3. To display your input history hit [F3]

    • In history window you can select inputs to send as a bulk using [Space] and [Enter].
  4. Exit ptpython shell using function exit().

As you may noticed, there is more going on your screen. ptpython helps you with code completion, shows method signatures and also displays documentation embedded in python code. So you do not need to use help() function.

../_images/ptpython_2.png

Now you know how to use ptpython!

Jupyter notebook

Jupyter notebook is python shell running as a web app in your browser. Project jupyter is used by many Engineers or Scientists and supports more languages than just python. Jupyter is built on top of the ipython so it has many things in common.

Jupyter offers a lot of keyboard shortcuts to speed up the work. But these are essential you can use during this task.

  • [Tab] - code completion
  • [Shift]+[Enter] - execute cell
  • [Shift]+[Tab] - display docstring and signature

To learn more about shortcuts, check help!

Let’s try Jupyter notebook. If you have never worked with notebook style app before, here is a brief info:

  • Each notebook is running it’s own kernel.
  • All code is written into cell blocks, which you can execute.
  • You can print the output, or let jupyter to handle the output for you.
  • Executing the cell block writes data into a notebook memory, which means that you can access results from other cells.
  • If you want to cleanup this memory, simply restart the kernel.

Again I advice to start in a new folder and clean virtual env.

  1. Open the terminal emulator.

    • Mac OSx: hit [CMD]+[Space] and type terminal.app
    • Windows: hit [WIN-key]+[R] and type powershell (or you can search for powershell in start menu.
    • Optional step: create new virtual environment
  2. Install jupyter

    pip install jupyter
    
    • If you have pipenv installed, check pipenv graph to see the dependency list.
  3. Once installed run command: jupyter notebook to open the app.

    • Command above starts local web server (watch the output in console) on address localhost:8888
    • If everything went well, your browser should open on a landing page.
    ../_images/jupyter_1.png
  4. Click new (upper right corner)

  5. Select Python 3

    • This has openned you window with your notebook
    ../_images/jupyter_2.png
  6. Change the name of this notebook to something like Circle Area

  7. Now change the cell type to Markdown and insert following code:

# Circlle area
$A = \pi r^2$
  1. Hit [Shift]+[Enter] to execute the cell and observe the result.

    Result has just been translated into markdown and LaTeX!

Let’s write some python code now.

  1. In next cell enter following code (if you are typing the code, you can try keyboard shortcuts listed above)
 from math import pi, pow

 radius = 3
 area = pi * pow(radius, 2)
 print(f"The area of circle with radius of {radius} cm is {area} cm^2" )

* Jupyter notebook is also capable of displaying output directly from variable or function as Ipython.
  It only displays the output of the last function or variable in cell
  1. Hit [Shift]+[Enter] to run the cell.

You have successfully printed the output.

  1. In next cell type area and hit [Shift]+[Enter] to see the output.

    • The output of a variable area is the same as in previous cell. Variables from all cells are shared in notebook memory.
  2. Display all active variables with magic command: %whos and hit [Shift]+[Enter] to execute.

  3. Jupyter notebook is also capable of prettifying the output for you. Next task will show Write the following code and execute the cell.

    from string import ascii_uppercase
    # create alphabet dictionary {letter:index}
    alphabet = {ltr:idx for idx,ltr in enumerate(ascii_uppercase)}
    print(alphabet)
    
  4. In next cell type this and execute the cell with [Shift]+[Enter]

    alphabet
    
    • Jupyter has handled the output for you and tried to optimize it for better readability.

Congratulations, you have finished all the tasks and learned about different python interpreters you can use.