[go: up one dir, main page]

0% found this document useful (0 votes)
36 views36 pages

Introduction To Python Lecture 2: Introduction To Jupyter: Pavlos Antoniou

This document provides an introduction to using Jupyter notebooks for Python programming. It discusses key concepts like Python interpreters, launching the interpreter, .py files for Python programs, and .ipynb files for Jupyter notebooks. It also covers the JupyterLab interface including the left sidebar, launcher tab, notebooks, cells, markdown cells, and toolbar. Important keyboard shortcuts are also listed.

Uploaded by

Kris Test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views36 pages

Introduction To Python Lecture 2: Introduction To Jupyter: Pavlos Antoniou

This document provides an introduction to using Jupyter notebooks for Python programming. It discusses key concepts like Python interpreters, launching the interpreter, .py files for Python programs, and .ipynb files for Jupyter notebooks. It also covers the JupyterLab interface including the left sidebar, launcher tab, notebooks, cells, markdown cells, and toolbar. Important keyboard shortcuts are also listed.

Uploaded by

Kris Test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to Python

Lecture 2: Introduction to
University of Cyprus
Jupyter Department of
Computer Science

Pavlos Antoniou
The Python Interpreter
• Python is an interpreted language: program code is read, translated
to machine language and executed line by line by the Python
interpreter
– This stands in contrast to compiled languages (such as C, and C++)
– In compiled languages, entire program code is translated into machine
language before you ever run it by the compiler (executable file is created).
– Executable file is read and executed by the target machine as many times
we want (source code file and compiler are not involved during execution).

• So, whenever you want your Python code to run, you give it to the
Python interpreter
Launching the Python Interpreter
• There are many ways to launch the Python interpreter

• One way is to type python on the command line of Anaconda


prompt (terminal on Linux or macOS)
– We will never really use this in this course

• We prefer an interactive and enhanced Python experience, either


using a notebook, launchable within JupyterLab (or Jupyter
Notebook) or using a python file created and executed within
Spyder IDE
.py files
• Within Spyder IDE, a python program is written into a .py file
– .py is a plain text file containing python commands (not results, graphs, etc.)
• .py files are executed by Python interpreter when you click the Run
button of Spyder IDE; results are outputted in Console
– .py files cannot be executed by double clicking on them

hello.py
.ipynb files
• Within JupyterLab (or Jupyter Notebook), a python program is
written into a .ipynb file called notebook
• .ipynb files combine Python code with narrative text, mathematical
equations, visualizations, interactive controls, and other rich output
– .ipynb can be edited and executed only via JupyterLab or Jupyter Notebook

MyFirstAnacondaNotebook.ipynb
Jupyter
• Official website: https://jupyter.org/

• Jupyter is a free, web-based software (runs within a browser e.g.


Chrome), for interactive program writing for over 40 programming
languages (not only Python)

• Jupyter provides two notebook-based applications:


– Jupyter Notebook (classic interface)
– JupyterLab (next-generation interface) – recommended and used throughout
this course
Jupyter Notebook: Classic Interface
• The original web application for creating and sharing computational
documents: code + output results + graphs/figures
JupyterLab: Next-Generation Interface
• The latest web-based interactive development environment for
notebooks, code, and data
• Allows users to configure and arrange workflows in data science,
scientific computing and machine learning environments
JupyterLab left sidebar
• The left sidebar contains a number of commonly-used tabs
including:
– a file browser,
– a list of tabs in the main work and a list of
running kernels and terminals, selected directory
– the table of contents,
– the extension manager.

The file browser enables you to work with files and


directories on your system. This includes opening,
files
creating, deleting, renaming, downloading,
copying, and sharing files and directories.
JupyterLab left sidebar
• The left sidebar contains a number of commonly-used tabs
including:
– a file browser,
– a list of tabs in the main work and a list of
running kernels and terminals, selected directory
– the table of contents,
– the extension manager.

Create new files or activities by clicking the +


button at the top of the file browser. This will open
files
a new Launcher tab in the main work area, which
enables you to create a document or activity (see
next slide)
JupyterLab launcher tab
JupyterLab left sidebar
• The left sidebar contains a number of commonly-used tabs
including:
– a file browser,
– a list of tabs in the main work and a list of open
running kernels and terminals, notebook(s)

– the table of contents,


– the extension manager.
open
An open notebook is connected to a kernel, which will kernel(s)
execute code sent by the user and send back results. This
kernel remains active if the web browser window is closed,
and reopening the same notebook from the dashboard will
reconnect the web application to the same kernel.
JupyterLab left sidebar
• The left sidebar contains a number of commonly-used tabs
including:
– a file browser,
– a list of tabs in the main work and of running
kernels and terminals,
– the table of contents,
– the extension manager.

The table of contents makes it easy to see and navigate the


structure of a document. A table of contents is auto-
generated when you have a notebook opened. The entries
are clickable and scroll the document to the heading in
question.
JupyterLab left sidebar
• The left sidebar contains a number of commonly-used tabs
including:
– a file browser,
– a list of tabs in the main work and of running
kernels and terminals,
– the table of contents,
– the extension manager.

JupyterLab extensions can customize or enhance any part


of JupyterLab. They can provide new themes, file viewers
and editors, or renderers for rich outputs in notebooks.
Extensions can add items to the menu or command palette,
keyboard shortcuts, or settings in the settings system
Notebooks
• Central to Jupyter are notebooks
• To launch a new Python 3 Jupyter notebook click on the Python 3
icon under Notebook of the JupyterLab launcher tab
Notebooks
• You can also launch a new notebook using the File menu:
Notebooks
• The current working directory of a
new notebook will be the directory
listed in the file browser

• A new notebook is created with a


default name. Rename a file by right-
clicking on its name in the file
browser and selecting “Rename”
from the context menu
Notebooks
• If you want to open an existing notebook from file browser of
JupyterLab, right click on it 1 and open it 2

1
Cells
• Jupyter notebook consists of cells
• Τwo main cells types:
– code cells (default cell type)
– markdown cells
• You can alter the cell type using the pulldown menu in the toolbar of
your Jupyter notebook.
Code cells
• A code cell contains actual code that you want to run
• Execute the code in a code cell by selecting it and:
– clicking on the Run triangle or
– hitting the Enter while holding down the Shift key (denoted Shift + Enter).
• Code cells are executed in the order you shift-enter them
• If you did not explicitly
execute a cell early in
the document, its
results are not known This cell was not executed by user
x is not defined; y cannot be evaluated
to Python interpreter
and cannot be used in
the next cells
Code cells: Examples
• Below is an example of a code cell printing hello, world.
• Notice that the output of the print statement appears in the same
cell, though separate from the code block

• If you evaluate a Python expression that returns a value, that value


is displayed as output of the code cell. This only happens, however,
for the last line of the code cell.
Code cells: Examples
• If the last line of a cell does not return a value, such as if we
assigned value to a variable, there is no visible output from the code
cell.

• The number in the square brackets to the left of each cell indicates
the execution order of the cell
• While a cell is being executed, an asterisk (*) appears within the
square brackets to the left of the cell [*]. Once the execution
terminates, a number (execution order) replaces the asterisk.
Markdown cells
• Markdown cells contain text (such as titles, subtitles, paragraphs,
etc.), links (internal or external), or other graphics (such as images)

• The content is written in markdown, a lightweight markup language

• See APPENDIX for language’s syntax, see next slide for examples
Markdown cells

STEP 1: Type the content of the cell

STEP 2: Select Markdown

STEP 3: Click the Run button to execute


JupyterLab Toolbar

• Quick access buttons for • Quick access buttons for the


manipulating the cells kernel
– Save – Switch kernel
– Insert a cell below selected cell
– Cut the selected cells
– Copy the selected cells
– Paste cells from the clipboard
– Run the selected cells and advance
– Interrupt (stop) the kernel
– Restart the kernel
– Restart the kernel and Run all cells – Kernel status: idle (when circle
– Select the cell type is empty), busy (circle is filled)
Quick keys
• Some keyboard shortcuts are convenient to use in JupyterLab
• By pressing Esc brings you into command mode in which you are
no more editing the contents of a cell
– Below are some useful quick keys. If two keys are separated by a + sign,
they are pressed simultaneously, and if they are separated by a - sign, they
are pressed in succession.
Quick keys mode action
Esc - m command switch cell to Markdown cell
Esc - y command switch cell to code cell
Esc - a command insert cell above
Esc - b command insert cell below
Esc - d - d command delete cell
Alt + Enter edit execute cell and insert a cell below
Magic commands
• Magics are specific to and provided by the IPython kernel
• Magics are not built into Python itself
– For example, the IPython kernel uses the % syntax element for Magics as %
is not a valid unary operator in Python
• Examples:
– %time magic command prints the execution time of a Python statement or
expression
• This function can be used both as a line and cell magic:
– In line mode you can time
a single-line statement

– In cell mode, you can time


the cell body (use %%time
instead of %time)

• See more here


Google colab or Colaboratory
• Freemium product by Google research, based on Jupyter
• Accessed via browser (connection to Internet is needed)
• No need to install anything on your machine (e.g. Anaconda,
JupyterLab, etc.) – runs on Google servers
• Free computation power (GPU and TPU)
• Almost all important libraries are pre-installed
• Colab’s notebook files are stored in your google drive, so they can
be accessed from anywhere
• Allows for sharing notebooks with other people without even
downloading them
• Start using Google colab: https://colab.research.google.com/
Google colab
Google colab Notebook
Hands on
• Open JupyterLab and create a notebook with the following
specifications:
– Two markdown cells
• In the first cell, insert a title and subtitle
• In the second cell, insert one paragraph, a mathematical expression (in a different line)
and three bullet points
– Two code cells
• In the first cell, define two variables length, width and set them to 10 and 5 respectively
• In the second cell, calculate the area and the perimeter of a rectangle with length and
width given in the previous cell. Print both area and perimeter.
APPENDIX – Markdown
• Headings: Use #s followed by a blank space for notebook titles and
section headings:
– # title
– ## major headings
– ### subheadings
– #### 4th level subheadings

• Emphasis: Use this code: Bold text: __string__ or **string**


Italic text: _string_ or *string*

• Mathematical symbols: Surround mathematical expressions with a


dollar sign ($), for example: $ mathematical expressions $
APPENDIX – Markdown
• Bullets: To create a circular bullet point, use one of the following
methods. Each bullet point must be on its own line.
– A hyphen (- ) followed by 1 or 2 spaces, for example:- Bulleted item
– A space, a hyphen ( - ) and a space, for example: - Bulleted item
– An asterisk (*) followed by 1 or 2 spaces, for example:* Bulleted item

• Numbered lists: Start with 1. followed by a space, then it starts


numbering for you. Start each line with some number and a period,
then a space. Tab to indent to get subnumbering.
• Indented quoting: Use a greater than sign (>) and then a space, then
type the text. The text is indented and has a gray horizontal line to the
left of it until the next carriage return.
APPENDIX – Markdown
• Monospace font: Surround text with a back single quotation mark (`),
for example `string` You can use monospace for file paths and file
names and message text that users see, or text that users enter.

• Line breaks: Sometimes markdown doesn’t make line breaks when


you want them. To force a linebreak, use the following code: <br>

• Colors: Use: <font color=blue|red|green|pink|yellow>


Text </font> Not all markdown code works within a font tag, so
review your colored text carefully!
APPENDIX – Markdown
• Graphics:
– You can attach image files directly to a notebook in Markdown cells by
dragging and dropping it into the cell.
– To add images to other cell types, use graphics that are hosted on the web
with this code, substituting url/name with the full URL and name of the image:

<img src="url/filename.gif" alt="Alt text that describes


the graphic" title="Title text" />

• Horizontal lines: Use three asterisks: ***


APPENDIX – Markdown
• Internal links: Within the same notebook, can be implemented with
a mixture of Markdown and HTML. Just add the anchor to the
Markdown cell you want to refer to, and link to it using standard
Markdown syntax.
– Add an ID above the destination (anchor) section (you want to link to):
<a id="section_1"></a>
## Section 1
Make sure that the section_1 id is unique within the notebook
– The reference can be like:
For more details [see Section 1](#section_1)
where the phrase see Section 1 is linkable

• External links: Use this code: [link text](http://url)

You might also like