Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jupyter lab files2 #449

Merged
merged 8 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added jupyter-lab-files/Glasgow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions jupyter-lab-files/Handling Exceptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Dealing With Exceptions

## What is an Exception?

An **Exception** gets **raised** when code encounters an occasional,
but not unexpected, error. Although exceptions are an unavoidable
part of software, because you are aware that exceptions may occur,
you should write code to deal with or **handle** them.
Unhandled exceptions will cause a program to crash.

## How Do You Handle Them?

You implement [*exception handling*](https://realpython.com/python-exceptions/)
to handle exceptions. In its most basic form, this provides both a *try*
and one or more *except* blocks:

* The *try* block contains the code you wish to monitor for exceptions.
Any exceptions raised within *try* will be elligible for handling.

* One or more *except* blocks are where you define code that will run
when exceptions occur and handle them.
This is how you stop the exception from crashing the code.

## What Are Some Common Exceptions

The Python language supports more than sixty common exceptions.
Three of the more common are:


| Exception | Cause |
|:- |:- |
| **ZeroDivisionError** | Raised when you attempt to divide by zero |
| **ValueError** | Raised when you pass an inappropriate value |

## Example

```python
try:
first_number = float(input("Enter your first number"))
second_number = float(input("Enter your second number"))
print(f"{first_number} / {second_number} = {first_number / second_number}")
except ZeroDivisionError:
print("You can't divide by zero")
except ValueError:
print("You must supply a number")
```
22 changes: 22 additions & 0 deletions jupyter-lab-files/Movies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Movies": [
{
"id": 2,
"title": "Dr No",
"release_year": 1962,
"star": "Sean Connery"
},
{
"id": 1,
"title": "Pal Joey",
"release_year": 1953,
"star": "Francis Albert Sinatra"
},
{
"id": 3,
"title": "The Godfather",
"release_year": 1972,
"star": "Marlon Brando"
}
]
}
74 changes: 74 additions & 0 deletions jupyter-lab-files/Population Changes.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5845722b-afb4-47db-99ee-c59217552065",
"metadata": {},
"source": [
"### Population Changes Since 1980\n",
"\n",
"The data shown below shows the decade-on-decade increases in the\n",
"[world's population](https://www.worldometers.info/world-population/world-population-by-year/)\n",
"since 1980."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4db0c4f2-ba67-4854-9f6b-2e2ca724971f",
"metadata": {},
"outputs": [],
"source": [
"def calculate_differences(data_set):\n",
" differences = [\n",
" 0,\n",
" ]\n",
" for index in range(1, len(data_set)):\n",
" differences.append(round(data_set[index] - data_set[index - 1], 1))\n",
" return differences\n",
"\n",
"\n",
"population_change = calculate_differences(population)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0822c8a4-829a-4806-b965-b2a88cf8798c",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"zipped = list(zip(decades, population, population_change))\n",
"\n",
"population_df = pd.DataFrame(\n",
" zipped, columns=(\"Decade\", \"Population(Bn)\", \"Change\")\n",
")\n",
"\n",
"print(population_df)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
81 changes: 81 additions & 0 deletions jupyter-lab-files/Population Data.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "8a9df91c-eadc-47f9-bce7-948e38136b07",
"metadata": {},
"source": [
"### Changes in World Population Since 1960\r\n",
"\r\n",
"The data shown below shows how the\r\n",
"[world's population](https://www.worldometers.info/world-population/world-population-by-year/)\r\n",
"has more than doubled since 1960.\r\n",
"\r\n",
"This has [implications](https://ugc.berkeley.edu/background-content/population-growth/)\r\n",
"in a variety of areas including:\r\n",
"\r\n",
"* Increased extraction of environmental resources.\r\n",
"* Increased fossil fuel usage.\r\n",
"* Increased disease transmission.\r\n",
"* Increased transportation of invasive species.\r\n",
"\r\n",
"**The data below shows the population each decade since 1960:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50d0f480-a0fd-4586-8a22-03949a2518f0",
"metadata": {},
"outputs": [],
"source": [
"# decades = [1960, 1970, 1980, 1990, 2000, 2010, 2020]\n",
"\n",
"# World population(billions)\n",
"# population = [3, 3.7, 4.4, 5.3, 6.1, 7.0, 7.8]\n",
"\n",
"decades = [1980, 1990, 2000, 2010, 2020]\n",
"population = [4.4, 5.3, 6.1, 7.0, 7.8]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d399a86-4d1b-48fb-928d-f2860621c90b",
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"\n",
"plt.plot(decades, population)\n",
"\n",
"plt.xlabel(\"Year\")\n",
"plt.ylabel(\"Population (Billion)\")\n",
"plt.title(\"World Population\")\n",
"\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
13 changes: 13 additions & 0 deletions jupyter-lab-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Using JupyterLab

This folder contains completed notebooks and other files used in the Real Python tutorial on [using JupyterLab](https://realpython.com/using-jupyterlab/).

None of the files are mandatory to complete the tutorial. However, you may find them of use to continue your learning by experimenting more with them.

## Setup

The easiest way to incorporate these files into JupyterLab is to copy them into their own `Samples/` folder as instructed in the tutorial. You can then access them from JupyterLab's _file browser_ when needed.

## Usage

After creating and activating your virtual environment, installing the dependencies, and starting the JupyterLab server, you can load any file by double-clicking on it from the _file browser_ in JupyterLab.
11 changes: 11 additions & 0 deletions jupyter-lab-files/Ten_Bond_Movies.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Movie,Bond,Director,Year
Dr. No,Sean Connery,Terence Young,1962
From Russia with Love,Sean Connery,Terence Young,1963
Goldfinger,Sean Connery,Guy Hamilton,1964
Thunderball,Sean Connery,Terence Young,1965
You Only Live Twice,Sean Connery,Lewis Gilbert,1967
On Her Majesty's Secret Service,George Lazenby,Peter R. Hunt,1969
Diamonds Are Forever,Sean Connery,Guy Hamilton,1971
Live and Let Die,Roger Moore,Guy Hamilton,1973
The Man with the Golden Gun,Roger Moore,Guy Hamilton,1974
The Spy Who Loved Me,Roger Moore,Lewis Gilbert,1977
Binary file added jupyter-lab-files/howto-regex.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions jupyter-lab-files/range type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## The range() Function
In Python, the *range()* type provides an immutable sequence of numbers.
These numbers can be used to create a list.

```python
# Examples of range()
print(list(range(1, 10)))
print(list(range(1, 10, 2)))
print(list(range(1, 10, 3)))
print(list(range(1, 10, 5)))
```
Loading