-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"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(zipped, columns=(\"Decade\", \"Population(Bn)\", \"Change\"))\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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, howevre 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. They can then be accessed from its File Browser when needed. | ||
|
||
## Usage | ||
|
||
After creating and activating your virtual environment, and installing the dependencies, you can load any file into JupyterLab by double-clicking on it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
``` |