diff --git a/jupyter-lab-files/Glasgow.jpg b/jupyter-lab-files/Glasgow.jpg new file mode 100644 index 0000000000..59af79e689 Binary files /dev/null and b/jupyter-lab-files/Glasgow.jpg differ diff --git a/jupyter-lab-files/Handling Exceptions.md b/jupyter-lab-files/Handling Exceptions.md new file mode 100644 index 0000000000..61b033e904 --- /dev/null +++ b/jupyter-lab-files/Handling Exceptions.md @@ -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") +``` \ No newline at end of file diff --git a/jupyter-lab-files/Movies.json b/jupyter-lab-files/Movies.json new file mode 100644 index 0000000000..4d3293f2c2 --- /dev/null +++ b/jupyter-lab-files/Movies.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/jupyter-lab-files/Population Changes.ipynb b/jupyter-lab-files/Population Changes.ipynb new file mode 100644 index 0000000000..ca44f522ac --- /dev/null +++ b/jupyter-lab-files/Population Changes.ipynb @@ -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 +} diff --git a/jupyter-lab-files/Population Data.ipynb b/jupyter-lab-files/Population Data.ipynb new file mode 100644 index 0000000000..babb96c75f --- /dev/null +++ b/jupyter-lab-files/Population Data.ipynb @@ -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 +} diff --git a/jupyter-lab-files/README.md b/jupyter-lab-files/README.md new file mode 100644 index 0000000000..1462755f5c --- /dev/null +++ b/jupyter-lab-files/README.md @@ -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. diff --git a/jupyter-lab-files/Ten_Bond_Movies.csv b/jupyter-lab-files/Ten_Bond_Movies.csv new file mode 100644 index 0000000000..1d50e08580 --- /dev/null +++ b/jupyter-lab-files/Ten_Bond_Movies.csv @@ -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 diff --git a/jupyter-lab-files/howto-regex.pdf b/jupyter-lab-files/howto-regex.pdf new file mode 100644 index 0000000000..b5872d3738 Binary files /dev/null and b/jupyter-lab-files/howto-regex.pdf differ diff --git a/jupyter-lab-files/range type.md b/jupyter-lab-files/range type.md new file mode 100644 index 0000000000..4c4d65892c --- /dev/null +++ b/jupyter-lab-files/range type.md @@ -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))) +```