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

Create notebook.ipynb #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
154 changes: 154 additions & 0 deletions class-5/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# Python do Início\n",
"\n",
"## Erros e Exceptions\n",
"\n",
"### Links úteis\n",
"- https://docs.python.org/pt-br/3/tutorial/errors.html\n",
"- https://realpython.com/python-lbyl-vs-eafp/"
],
"metadata": {
"id": "NZ7PwDdltgb-"
}
},
{
"cell_type": "code",
"source": [
"# '1' + 1\n",
"\n",
"a = '1'\n",
"b = 1\n",
"\n",
"print('INÍCIO')\n",
"\n",
"print(a + b)\n",
"\n",
"print('DEU')\n",
"\n",
"if isinstance(a, int):\n",
" print('a é inteiro')\n",
" print(a + b)\n",
"else:\n",
" print('a precisa ser inteiro')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 254
},
"id": "vrOc6gbut8xl",
"outputId": "8a6a15d8-1484-436d-cf9d-5bb6add16c96"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"INÍCIO\n"
]
},
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-21-6e68acd8a900>\u001b[0m in \u001b[0;36m<cell line: 8>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'INÍCIO'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'DEU'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
]
}
]
},
{
"cell_type": "code",
"source": [
"int('1') + 1"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ky0ZvF05yG1h",
"outputId": "a00ac8cb-662d-45f5-8795-3562e6740c67"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"operando_1 = '1'\n",
"operando_2 = 1\n",
"\n",
"try:\n",
" # 1 / 0\n",
" # ... não executa\n",
" soma = operando_1 + operando_2\n",
" print(operando_1 + operando_2)\n",
" # ...\n",
"except TypeError:\n",
" print('TypeError')\n",
" operando_1 = int(operando_1)\n",
" operando_2 = int(operando_2)\n",
" print(operando_1 + operando_2)\n",
"# ...\n",
"except ZeroDivisionError:\n",
" print('Dã, não divida por zero!')\n",
"except Exception as e:\n",
" print(f'Erro desconhecido: {e.__class__.__name__}')\n",
"\n",
"print('DEU')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JUvbgismv7uY",
"outputId": "43316888-499c-40e1-b32e-0a7ff156e2fe"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"TypeError\n",
"2\n",
"DEU\n"
]
}
]
}
]
}