├── Exercícios
├── Exercício_1_Python_para_ML.ipynb
├── Exercício_2_Python_para_ML.ipynb
├── Exercício_3_Python_para_ML.ipynb
└── Exercício_4_Python_para_ML.ipynb
├── Imagens
├── capa - python para ml 01.jpg
├── capa - python para ml 02.jpg
├── capa - python para ml 03.jpg
├── capa - python para ml 04.jpg
├── capa - python para ml 05.jpg
├── capa - python para ml 06.jpg
├── capa - python para ml 07.jpg
├── capa - python para ml 08.jpg
├── capa - python para ml 09.jpg
├── capa - python para ml 10.jpg
├── capa - python para ml 11.jpg
├── capa - python para ml 12.jpg
└── logo_matplotlib.png
├── Metrica_personalizada_para_ML.ipynb
├── Modelos_de_ML_Social_Ads.ipynb
├── Projeto_Álbum_de_Figurinhas.ipynb
├── Python_para_ML_01.ipynb
├── Python_para_ML_02.ipynb
├── Python_para_ML_03.ipynb
├── Python_para_ML_04.ipynb
├── Python_para_ML_05.ipynb
├── Python_para_ML_06.ipynb
├── Python_para_ML_07.ipynb
├── Python_para_ML_08.ipynb
├── Python_para_ML_09.ipynb
├── Python_para_ML_10.ipynb
├── Python_para_ML_11.ipynb
├── Python_para_ML_12.ipynb
├── README.md
├── Redes_Neurais_no_Python.ipynb
└── Simulacao_da_Copa_de_Futebol_das_Olimpiadas.ipynb
/Exercícios/Exercício_1_Python_para_ML.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Exercício 1 - Python para ML.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyONY2u/B2ggiA3qUCge0nNe",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "r1X1E_zEge7K",
31 | "colab_type": "text"
32 | },
33 | "source": [
34 | "# **Exercício 1** \n",
35 | "Considere um balde cuja base possui raio $r_1$ e altura igual ao diâmetro da base. Considere também uma esfera de raio $r_2$ cheia de água. Faça um programa que verifique se o volume da esfera cabe no balde, dados os valores de $r_1$ e $r_2$. \n"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "metadata": {
41 | "id": "mwVI08PmZ1bC",
42 | "colab_type": "code",
43 | "colab": {}
44 | },
45 | "source": [
46 | "from math import pi\n",
47 | "\n",
48 | "def Verificadora(r_1, r_2):\n",
49 | " if r_1 <= 0:\n",
50 | " print('O valor do raio do balde é inadequado!')\n",
51 | " elif r_2 <= 0:\n",
52 | " print('O valor do raio da esfera é inadequado!')\n",
53 | " else:\n",
54 | " vol_balde = 2*pi*r_1**3 \n",
55 | " vol_esfera = (4/3)*pi*r_2**2\n",
56 | "\n",
57 | " if vol_balde < vol_esfera:\n",
58 | " print('O volume da esfera NÂO cabe no balde')\n",
59 | " else:\n",
60 | " print('O volume da esfera CABE no balde')"
61 | ],
62 | "execution_count": null,
63 | "outputs": []
64 | },
65 | {
66 | "cell_type": "code",
67 | "metadata": {
68 | "id": "8_uKuFo9cjKz",
69 | "colab_type": "code",
70 | "colab": {
71 | "base_uri": "https://localhost:8080/",
72 | "height": 35
73 | },
74 | "outputId": "4938e663-ab05-43c3-c3b4-01068d773692"
75 | },
76 | "source": [
77 | "Verificadora(1,1)"
78 | ],
79 | "execution_count": null,
80 | "outputs": [
81 | {
82 | "output_type": "stream",
83 | "text": [
84 | "O volume da esfera CABE no balde\n"
85 | ],
86 | "name": "stdout"
87 | }
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "metadata": {
93 | "id": "gtaT3ENPcnHy",
94 | "colab_type": "code",
95 | "colab": {}
96 | },
97 | "source": [
98 | ""
99 | ],
100 | "execution_count": null,
101 | "outputs": []
102 | }
103 | ]
104 | }
--------------------------------------------------------------------------------
/Exercícios/Exercício_2_Python_para_ML.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Exercício 2 - Python para ML.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyORmLtqYZ8eAhlK2avxcK/N",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "ZRfHvmZ548aq",
31 | "colab_type": "text"
32 | },
33 | "source": [
34 | "\n",
35 | "# **Exercício 2** \n",
36 | "Crie uma função que simule o jogo do *jokempô*, isto é, dada a entrada de dois jogadores, retorne a indicação de qual deles venceu. "
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "metadata": {
42 | "id": "0AnSsiTIhxMr",
43 | "colab_type": "code",
44 | "colab": {}
45 | },
46 | "source": [
47 | "def Jokenpo(j1, j2):\n",
48 | " jogador1 = j1.lower()\n",
49 | " jogador2 = j2.lower()\n",
50 | "\n",
51 | " simbolos = ['pedra', 'papel', 'tesoura']\n",
52 | "\n",
53 | " if jogador1 not in simbolos or jogador2 not in simbolos:\n",
54 | " print('Entradas Inadequadas!')\n",
55 | " else:\n",
56 | " if jogador1 == 'pedra':\n",
57 | " if jogador2 == 'tesoura':\n",
58 | " print('Jogador 1 Ganhou!')\n",
59 | " elif jogador2 == 'papel':\n",
60 | " print('Jogador 2 Ganhou!')\n",
61 | " else:\n",
62 | " print('Empate')\n",
63 | "\n",
64 | " if jogador1 == 'papel':\n",
65 | " if jogador2 == 'pedra':\n",
66 | " print('Jogador 1 Ganhou!')\n",
67 | " elif jogador2 == 'tesoura':\n",
68 | " print('Jogador 2 Ganhou!')\n",
69 | " else:\n",
70 | " print('Empate')\n",
71 | "\n",
72 | " if jogador1 == 'tesoura':\n",
73 | " if jogador2 == 'papel':\n",
74 | " print('Jogador 1 Ganhou!')\n",
75 | " elif jogador2 == 'pedra':\n",
76 | " print('Jogador 2 Ganhou!')\n",
77 | " else:\n",
78 | " print('Empate') "
79 | ],
80 | "execution_count": null,
81 | "outputs": []
82 | },
83 | {
84 | "cell_type": "code",
85 | "metadata": {
86 | "id": "ddaIT7X5jueH",
87 | "colab_type": "code",
88 | "colab": {
89 | "base_uri": "https://localhost:8080/",
90 | "height": 35
91 | },
92 | "outputId": "339be7d3-a254-4b4b-d646-4b5668dfae70"
93 | },
94 | "source": [
95 | "Jokenpo('TESOURA', 'papeL')"
96 | ],
97 | "execution_count": null,
98 | "outputs": [
99 | {
100 | "output_type": "stream",
101 | "text": [
102 | "Jogador 1 Ganhou!\n"
103 | ],
104 | "name": "stdout"
105 | }
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "metadata": {
111 | "id": "1LjjsAGMjxhe",
112 | "colab_type": "code",
113 | "colab": {}
114 | },
115 | "source": [
116 | ""
117 | ],
118 | "execution_count": null,
119 | "outputs": []
120 | }
121 | ]
122 | }
--------------------------------------------------------------------------------
/Exercícios/Exercício_3_Python_para_ML.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Exercício 3 - Python para ML.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyMmr3ST8UD+pHJoN38vMud2",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "XW7xW90KucKA",
31 | "colab_type": "text"
32 | },
33 | "source": [
34 | "# **Exercício 3** \n",
35 | "\n",
36 | "Faça um programa que simule uma **slot machine**.\n",
37 | "\n",
38 | "**Parte A:** Uma slot machine é uma máquina muito comum em cassinos. A pessoa puxa uma alavanca e aparecem na tela 3 símbolos aleatoriamente, de uma lista com diversos deles. Se os símbolos forem iguais, então a pessoa ganha. A pessoa entrar com um tanto escolhido de fichas, e joga até que acabe. Quando o programa terminar, uma mensagem resumindo os totais que ela ganhou deve ser exibida.\n",
39 | "\n",
40 | "**Parte B:** Estime as probabilidades de acontecer cada tipo de prêmio na slot machine."
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "metadata": {
46 | "id": "2YQB-0JkV92c",
47 | "colab_type": "code",
48 | "colab": {
49 | "base_uri": "https://localhost:8080/",
50 | "height": 34
51 | },
52 | "outputId": "06f7adba-4924-458f-d6c8-b3627366ed68"
53 | },
54 | "source": [
55 | "!pip install emoji "
56 | ],
57 | "execution_count": null,
58 | "outputs": [
59 | {
60 | "output_type": "stream",
61 | "text": [
62 | "Requirement already satisfied: emoji in /usr/local/lib/python3.6/dist-packages (0.5.4)\n"
63 | ],
64 | "name": "stdout"
65 | }
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "metadata": {
71 | "id": "X_CU1LS7WHcF",
72 | "colab_type": "code",
73 | "colab": {}
74 | },
75 | "source": [
76 | "from random import randint\n",
77 | "from time import sleep\n",
78 | "from emoji import emojize "
79 | ],
80 | "execution_count": null,
81 | "outputs": []
82 | },
83 | {
84 | "cell_type": "code",
85 | "metadata": {
86 | "id": "wsNQsBXjWHhe",
87 | "colab_type": "code",
88 | "colab": {}
89 | },
90 | "source": [
91 | "def SimbolosPremios():\n",
92 | " simbolos = [':gem:', ':cherries:', ':banana:', ':poop:']\n",
93 | " premios = [15, 10, 5, 3]\n",
94 | " return simbolos, premios"
95 | ],
96 | "execution_count": null,
97 | "outputs": []
98 | },
99 | {
100 | "cell_type": "code",
101 | "metadata": {
102 | "id": "y-HqXYCkWHlH",
103 | "colab_type": "code",
104 | "colab": {}
105 | },
106 | "source": [
107 | "def Linhas(frase, simbolo = '*', cor = ''):\n",
108 | " print(cor, end = '')\n",
109 | " linha = (len(frase) + 2) * simbolo\n",
110 | " print('\\n' + '\\033[1m' + linha)\n",
111 | " print(' ' + frase)\n",
112 | " print(linha + '\\033[0m') "
113 | ],
114 | "execution_count": null,
115 | "outputs": []
116 | },
117 | {
118 | "cell_type": "code",
119 | "metadata": {
120 | "id": "uAgh9edfWHfr",
121 | "colab_type": "code",
122 | "colab": {}
123 | },
124 | "source": [
125 | "def Suspense(e1, e2, e3):\n",
126 | " lista = ['\\nAlavanca acionada! ', e1, '.', '.', '. ', e2, '.', '.', '. ', e3+'!\\n'] \n",
127 | " for i in lista:\n",
128 | " print(i, end = '')\n",
129 | " sleep(0.5) "
130 | ],
131 | "execution_count": null,
132 | "outputs": []
133 | },
134 | {
135 | "cell_type": "code",
136 | "metadata": {
137 | "id": "DGJOIepHWRVX",
138 | "colab_type": "code",
139 | "colab": {}
140 | },
141 | "source": [
142 | "def Explicacao():\n",
143 | " simbs, premios = SimbolosPremios()\n",
144 | " sleep(1.5)\n",
145 | " print('-' * 70)\n",
146 | " print('\\033[1m' + '\\nRegras:\\n' + '\\033[0m')\n",
147 | " for i in range(len(simbs)): \n",
148 | " print(emojize('Acerte ' + 3*simbs[i] + ' >>>>> e ganhe {} fichas!'.format(premios[i]) , use_aliases = True))\n",
149 | " print('\\033[1m' + '\\nBoa Sorte! Haha\\n' + '\\033[0m') "
150 | ],
151 | "execution_count": null,
152 | "outputs": []
153 | },
154 | {
155 | "cell_type": "code",
156 | "metadata": {
157 | "id": "MRbUOyaGWPd7",
158 | "colab_type": "code",
159 | "colab": {}
160 | },
161 | "source": [
162 | "def Jogo():\n",
163 | " simbs, premios = SimbolosPremios() \n",
164 | " s = len(simbs)\n",
165 | "\n",
166 | " s1 = randint(1, s) - 1\n",
167 | " s2 = randint(1, s) - 1\n",
168 | " s3 = randint(1, s) - 1\n",
169 | "\n",
170 | " Suspense(emojize(simbs[s1], use_aliases = True), \n",
171 | " emojize(simbs[s2], use_aliases = True), \n",
172 | " emojize(simbs[s3], use_aliases = True)) \n",
173 | "\n",
174 | " if simbs[s1] == simbs[s2] == simbs[s3]:\n",
175 | " premio = premios[simbs.index(simbs[s1])]\n",
176 | " Linhas('***** GANHOU! *****', '$', '\\033[92m')\n",
177 | " print('\\n' + '\\033[1m' + 'Seu prêmio é de {} fichas!'.format(premio) + '\\033[0m')\n",
178 | " else:\n",
179 | " print('\\nNão foi dessa vez! :(')\n",
180 | " premio = 0\n",
181 | " \n",
182 | " return premio"
183 | ],
184 | "execution_count": null,
185 | "outputs": []
186 | },
187 | {
188 | "cell_type": "code",
189 | "metadata": {
190 | "id": "O4wjr2GeWTUT",
191 | "colab_type": "code",
192 | "colab": {}
193 | },
194 | "source": [
195 | "def SlotMachine():\n",
196 | " nome = input('- Entre com o seu nome: ') \n",
197 | "\n",
198 | " while True:\n",
199 | " fichas0 = input('- Quantas fichas você gostaria de comprar? ')\n",
200 | " if fichas0.isdigit():\n",
201 | " break\n",
202 | " else:\n",
203 | " print('\\n**Entrada inválida** - Tente novamente.\\n')\n",
204 | "\n",
205 | " fichas = int(fichas0)\n",
206 | "\n",
207 | " f1 = 'Seja muito bem-vindo, {}!'.format(nome)\n",
208 | " Linhas(f1.upper(), '*', '\\033[94m')\n",
209 | " \n",
210 | " Explicacao()\n",
211 | "\n",
212 | " while fichas > 0:\n",
213 | " print('-' * 70)\n",
214 | " x = input('Aperte ENTER para puxar a alavanca ou digite SAIR para ir embora. ')\n",
215 | "\n",
216 | " if x.lower() == 'sair':\n",
217 | " print('-' * 70)\n",
218 | " Linhas('Fim de jogo! Você saiu com um total de {} fichas!'.format(fichas), '*', '\\033[94m') \n",
219 | " break \n",
220 | " else:\n",
221 | " jogo = Jogo()\n",
222 | " fichas += jogo -1\n",
223 | " print('\\nSeu saldo atual é de fichas é {}'.format(fichas))\n",
224 | " else:\n",
225 | " print('-' * 80)\n",
226 | " Linhas('Fim de jogo! Suas fichas acabaram! Até a próxima! Haha', '*', '\\033[94m')"
227 | ],
228 | "execution_count": null,
229 | "outputs": []
230 | },
231 | {
232 | "cell_type": "code",
233 | "metadata": {
234 | "id": "mqiHj92zWXEP",
235 | "colab_type": "code",
236 | "colab": {
237 | "base_uri": "https://localhost:8080/",
238 | "height": 1000
239 | },
240 | "outputId": "85ad59f5-51bd-4745-da1d-fadd28634921"
241 | },
242 | "source": [
243 | "SlotMachine()"
244 | ],
245 | "execution_count": null,
246 | "outputs": [
247 | {
248 | "output_type": "stream",
249 | "text": [
250 | "- Entre com o seu nome: Ricardo\n",
251 | "- Quantas fichas você gostaria de comprar? doze\n",
252 | "\n",
253 | "**Entrada inválida** - Tente novamente.\n",
254 | "\n",
255 | "- Quantas fichas você gostaria de comprar? 12\n",
256 | "\u001b[94m\n",
257 | "\u001b[1m********************************\n",
258 | " SEJA MUITO BEM-VINDO, RICARDO!\n",
259 | "********************************\u001b[0m\n",
260 | "----------------------------------------------------------------------\n",
261 | "\u001b[1m\n",
262 | "Regras:\n",
263 | "\u001b[0m\n",
264 | "Acerte 💎💎💎 >>>>> e ganhe 15 fichas!\n",
265 | "Acerte 🍒🍒🍒 >>>>> e ganhe 10 fichas!\n",
266 | "Acerte 🍌🍌🍌 >>>>> e ganhe 5 fichas!\n",
267 | "Acerte 💩💩💩 >>>>> e ganhe 3 fichas!\n",
268 | "\u001b[1m\n",
269 | "Boa Sorte! Haha\n",
270 | "\u001b[0m\n",
271 | "----------------------------------------------------------------------\n",
272 | "Aperte ENTER para puxar a alavanca ou digite SAIR para ir embora. \n",
273 | "\n",
274 | "Alavanca acionada! 💩... 🍌... 🍒!\n",
275 | "\n",
276 | "Não foi dessa vez! :(\n",
277 | "\n",
278 | "Seu saldo atual é de fichas é 11\n",
279 | "----------------------------------------------------------------------\n",
280 | "Aperte ENTER para puxar a alavanca ou digite SAIR para ir embora. \n",
281 | "\n",
282 | "Alavanca acionada! 💩... 🍌... 💎!\n",
283 | "\n",
284 | "Não foi dessa vez! :(\n",
285 | "\n",
286 | "Seu saldo atual é de fichas é 10\n",
287 | "----------------------------------------------------------------------\n",
288 | "Aperte ENTER para puxar a alavanca ou digite SAIR para ir embora. \n",
289 | "\n",
290 | "Alavanca acionada! 🍒... 💎... 💎!\n",
291 | "\n",
292 | "Não foi dessa vez! :(\n",
293 | "\n",
294 | "Seu saldo atual é de fichas é 9\n",
295 | "----------------------------------------------------------------------\n",
296 | "Aperte ENTER para puxar a alavanca ou digite SAIR para ir embora. \n",
297 | "\n",
298 | "Alavanca acionada! 💎... 🍒... 💎!\n",
299 | "\n",
300 | "Não foi dessa vez! :(\n",
301 | "\n",
302 | "Seu saldo atual é de fichas é 8\n",
303 | "----------------------------------------------------------------------\n",
304 | "Aperte ENTER para puxar a alavanca ou digite SAIR para ir embora. sair\n",
305 | "----------------------------------------------------------------------\n",
306 | "\u001b[94m\n",
307 | "\u001b[1m**************************************************\n",
308 | " Fim de jogo! Você saiu com um total de 8 fichas!\n",
309 | "**************************************************\u001b[0m\n"
310 | ],
311 | "name": "stdout"
312 | }
313 | ]
314 | }
315 | ]
316 | }
--------------------------------------------------------------------------------
/Exercícios/Exercício_4_Python_para_ML.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Exercício 4 - Python para ML.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyPnwwwmY3+hZx8FR1c03TJE",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "iTybxsEjYEko",
31 | "colab_type": "text"
32 | },
33 | "source": [
34 | "# **Exercício 4**\n",
35 | "\n",
36 | "Fazer um programa para simular um **álbum de figurinhas**.\n",
37 | "\n",
38 | "### EXEMPLO: **Álbum Premier League 2019-2020**\n",
39 | "1. Total de cromos: **636**\n",
40 | "2. Preço do livro ilustrado capa brochura: **R\\$ 8,90**\n",
41 | "3. Envelope com 5 cromos: **R\\$ 2,50**\n",
42 | "\n",
43 | "### SUPOSIÇÕES\n",
44 | "1. Todas as figurinhas tem igual probabilidade de serem sorteradas\n",
45 | "2. Um pacotinho é comprado por vez \n",
46 | "\n",
47 | "### ALGORITMO\n",
48 | "1. Comprar um pacotinho de figurinhas (5 figurinhas cada, que podem ser repetidas);\n",
49 | "2. Colar no álbum e verificar se o álbum está completo;\n",
50 | "3. Caso esteja incompleto, comprar mais um pacotinho, caso contrário, terminar.\n",
51 | "\n",
52 | "### PERGUNTAS\n",
53 | "1. Qual o valor médio investido para completar o álbum nessas condições?\n",
54 | "2. Quantos pacotes são necessários comprar, em média, para completar o álbum?\n",
55 | "3. Qual é a distribuição empírica do valor investido para completar o álbum?\n",
56 | "4. Quantas vezes saiu a figurinha mais repetida, em média?\n",
57 | "5. Em média, quantas figurinhas não se repetem ao completar o álbum?\n",
58 | "6. Qual a probabilidade de se gastar mais que R\\$3000,00 para completar o álbum?\n",
59 | "7. Qual a probabilidade de se gastar menos que R\\$1500,00 para completar o álbum?\n",
60 | "8. Qual a probabilidade de se gastar mais do que a média para completar o álbum?\n",
61 | "9. Qual é o intervalo de confiança de 95% para o gasto ao se completar o álbum?\n",
62 | "10. Qual o valor médio gasto caso se esteja completando o álbum com mais um amigo?\n",
63 | "11. Quanto se economiza ao utilizar o cenário da questão 10?\n",
64 | "12. Qual o valor médio gasto caso se esteja completando o álbum com mais dois amigos?\n",
65 | "13. Quanto se economiza ao utilizar o cenário da questão 12?"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "metadata": {
71 | "id": "h2l9BcKaV_eX",
72 | "colab_type": "code",
73 | "colab": {}
74 | },
75 | "source": [
76 | "n_album = 636\n",
77 | "preco_pacote = 2.50\n",
78 | "preco_album = 8.90\n",
79 | "simulacoes = 1000"
80 | ],
81 | "execution_count": null,
82 | "outputs": []
83 | },
84 | {
85 | "cell_type": "code",
86 | "metadata": {
87 | "id": "xB0munEeV_n_",
88 | "colab_type": "code",
89 | "colab": {}
90 | },
91 | "source": [
92 | "import numpy as np"
93 | ],
94 | "execution_count": null,
95 | "outputs": []
96 | },
97 | {
98 | "cell_type": "code",
99 | "metadata": {
100 | "id": "Jvyn7lBTYEtO",
101 | "colab_type": "code",
102 | "colab": {
103 | "base_uri": "https://localhost:8080/",
104 | "height": 34
105 | },
106 | "outputId": "70b1043b-d825-4644-df79-895321a6e29e"
107 | },
108 | "source": [
109 | "# comprando figurinhas até completar o álbum\n",
110 | "def SimulaAlbum():\n",
111 | " album = np.zeros(n_album) \n",
112 | " pacotes = 0\n",
113 | " while not np.all(album > 0):\n",
114 | " pacotinho = np.random.choice(range(n_album), 5)\n",
115 | " pacotes += 1\n",
116 | "\n",
117 | " for i in pacotinho:\n",
118 | " album[i] += 1\n",
119 | " \n",
120 | "\n",
121 | " valor_gasto = preco_album + preco_pacote * pacotes \n",
122 | "\n",
123 | " mais_repetida = max(album)\n",
124 | "\n",
125 | " nao_repetidas = sum(album == 1)\n",
126 | "\n",
127 | " # print(album)\n",
128 | " return valor_gasto, mais_repetida, nao_repetidas\n",
129 | "\n",
130 | "SimulaAlbum()"
131 | ],
132 | "execution_count": null,
133 | "outputs": [
134 | {
135 | "output_type": "execute_result",
136 | "data": {
137 | "text/plain": [
138 | "(2311.4, 16.0, 3)"
139 | ]
140 | },
141 | "metadata": {
142 | "tags": []
143 | },
144 | "execution_count": 69
145 | }
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "metadata": {
151 | "id": "q1khZHjQoJhg",
152 | "colab_type": "code",
153 | "colab": {
154 | "base_uri": "https://localhost:8080/",
155 | "height": 188
156 | },
157 | "outputId": "3202444b-43aa-425f-d863-d7f1152fcc13"
158 | },
159 | "source": [
160 | "valores = []\n",
161 | "maisrep = []\n",
162 | "nao_rep = []\n",
163 | "\n",
164 | "for i in range(simulacoes):\n",
165 | "\n",
166 | " s1, s2, s3 = SimulaAlbum()\n",
167 | "\n",
168 | " valores.append(s1)\n",
169 | " maisrep.append(s2)\n",
170 | " nao_rep.append(s3)\n",
171 | "\n",
172 | " if (i+1) % 100 == 0:\n",
173 | " print('Simulação:', i+1, '/', simulacoes)"
174 | ],
175 | "execution_count": null,
176 | "outputs": [
177 | {
178 | "output_type": "stream",
179 | "text": [
180 | "Simulação: 100 / 1000\n",
181 | "Simulação: 200 / 1000\n",
182 | "Simulação: 300 / 1000\n",
183 | "Simulação: 400 / 1000\n",
184 | "Simulação: 500 / 1000\n",
185 | "Simulação: 600 / 1000\n",
186 | "Simulação: 700 / 1000\n",
187 | "Simulação: 800 / 1000\n",
188 | "Simulação: 900 / 1000\n",
189 | "Simulação: 1000 / 1000\n"
190 | ],
191 | "name": "stdout"
192 | }
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {
198 | "id": "PM_3RPS5q_Z9",
199 | "colab_type": "text"
200 | },
201 | "source": [
202 | "Respostas das perguntas 1 e 2, respecitvamente:"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "metadata": {
208 | "id": "aWgOBRkHqoKk",
209 | "colab_type": "code",
210 | "colab": {
211 | "base_uri": "https://localhost:8080/",
212 | "height": 51
213 | },
214 | "outputId": "7c91b7f3-d5de-4531-a35f-57a2769098a7"
215 | },
216 | "source": [
217 | "print('O valor médio aproximado gasto foi:', round(np.mean(valores), 2))\n",
218 | "print('O numero de pacotes médio aproximado foi:', round((np.mean(valores) - preco_album)/preco_pacote, 2))"
219 | ],
220 | "execution_count": null,
221 | "outputs": [
222 | {
223 | "output_type": "stream",
224 | "text": [
225 | "O valor médio aproximado gasto foi: 2254.85\n",
226 | "O numero de pacotes médio aproximado foi: 898.38\n"
227 | ],
228 | "name": "stdout"
229 | }
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {
235 | "id": "eFFhtwKkrF__",
236 | "colab_type": "text"
237 | },
238 | "source": [
239 | "Resposta da pergunta 3:\n",
240 | "\n",
241 | "Podemos visualizar a distribuição empírica do valor gasto através do **histograma** dos valores simulados."
242 | ]
243 | },
244 | {
245 | "cell_type": "code",
246 | "metadata": {
247 | "id": "-jMyRhC-rNim",
248 | "colab_type": "code",
249 | "colab": {
250 | "base_uri": "https://localhost:8080/",
251 | "height": 283
252 | },
253 | "outputId": "c56efed8-2671-4ff1-e615-1e5972252b20"
254 | },
255 | "source": [
256 | "import matplotlib.pyplot as plt\n",
257 | "\n",
258 | "plt.hist(valores, bins = 20, density = True, edgecolor = 'black')\n",
259 | "plt.title('Distribuição Empírica do Valor Gasto para Completar o Álbum')\n",
260 | "plt.show()"
261 | ],
262 | "execution_count": null,
263 | "outputs": [
264 | {
265 | "output_type": "display_data",
266 | "data": {
267 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZkAAAEKCAYAAADAVygjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3de7xVdZ3/8de7A6KmoiI5KCAYVINdHCOtpiYna8BL4q+0sDJMjSz91XT5ecnGjKSkfqXN76fjWJp3kbTLyaEx05zqlzcsKy+RR1ABRRHQtPEGfn5/fL9HFpu1LxzOOnsfeT8fj/1g7e/67u/6fL977fVZt7NQRGBmZlaFl7U7ADMze+lykjEzs8o4yZiZWWWcZMzMrDJOMmZmVhknGTMzq4yTjJnZICXpKEkT2x1HI04yZmaDkKT3A2+LiHvbHUsjHZ9kJJ0r6V/6qa2xkp6S1JXf3yjpmD629QVJ322x7nWSfp2X/4O+LG+gSRojabG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hDnt5xgGPu6PEGRgLb5+mtgF8BBwHfZ/0L0Z/M08ex/oXoeY3604a4RxW+j7OAMzplHSnpw76su4je0eNdJ+ZBM9YD/Wp7AAPWUbiCdIrmedL5z6OBS4A/An8gPWetuBE8hXQ+fiGwf6H8ANLdUvcBpwxA3G8jnQr7A3BHfh1AOhd9PXAv8PPeFTSvzGfn+P4ITC60dRTpomgP8NE2xNzR4w28nvQooz8AdwKn5vLdSc/c68kbwGG5fMv8vifP371ZfwY47hvyeN8JXMq6O9Davo6U9GFf1m2wO3q868Q8aMZ6oF/+i38zM6vMZn1NxszMquUkY2ZmlXGSMTOzyjjJmJlZZZxkzMysMk4yZmZWGScZMzOrjJOMmZlV5v8DcBD0ff9MbSwAAAAASUVORK5CYII=\n",
268 | "text/plain": [
269 | ""
270 | ]
271 | },
272 | "metadata": {
273 | "tags": [],
274 | "needs_background": "light"
275 | }
276 | }
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {
282 | "id": "N2X3yLPMuFhp",
283 | "colab_type": "text"
284 | },
285 | "source": [
286 | "4. Quantas vezes saiu a figurinha mais repetida, em média?"
287 | ]
288 | },
289 | {
290 | "cell_type": "code",
291 | "metadata": {
292 | "id": "Wn9DlkB1uhkE",
293 | "colab_type": "code",
294 | "colab": {
295 | "base_uri": "https://localhost:8080/",
296 | "height": 34
297 | },
298 | "outputId": "a8288edb-93e7-4555-91c2-31cd6b8addc9"
299 | },
300 | "source": [
301 | "np.mean(maisrep)"
302 | ],
303 | "execution_count": null,
304 | "outputs": [
305 | {
306 | "output_type": "execute_result",
307 | "data": {
308 | "text/plain": [
309 | "16.634"
310 | ]
311 | },
312 | "metadata": {
313 | "tags": []
314 | },
315 | "execution_count": 93
316 | }
317 | ]
318 | },
319 | {
320 | "cell_type": "markdown",
321 | "metadata": {
322 | "id": "-0nYkG_auKsW",
323 | "colab_type": "text"
324 | },
325 | "source": [
326 | "5. Em média, quantas figurinhas não se repetem ao completar o álbum?\n"
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "metadata": {
332 | "id": "Ivwzf17vuiB6",
333 | "colab_type": "code",
334 | "colab": {
335 | "base_uri": "https://localhost:8080/",
336 | "height": 34
337 | },
338 | "outputId": "d9d03993-7d76-4ff5-8cbb-7fa0e81f5bef"
339 | },
340 | "source": [
341 | "np.mean(nao_rep)"
342 | ],
343 | "execution_count": null,
344 | "outputs": [
345 | {
346 | "output_type": "execute_result",
347 | "data": {
348 | "text/plain": [
349 | "6.825"
350 | ]
351 | },
352 | "metadata": {
353 | "tags": []
354 | },
355 | "execution_count": 92
356 | }
357 | ]
358 | },
359 | {
360 | "cell_type": "markdown",
361 | "metadata": {
362 | "id": "8OF1qw53uKvZ",
363 | "colab_type": "text"
364 | },
365 | "source": [
366 | "6. Qual a probabilidade de se gastar mais que R\\$3000,00 para completar o álbum?\n"
367 | ]
368 | },
369 | {
370 | "cell_type": "code",
371 | "metadata": {
372 | "id": "hKA0-Ioguild",
373 | "colab_type": "code",
374 | "colab": {
375 | "base_uri": "https://localhost:8080/",
376 | "height": 34
377 | },
378 | "outputId": "30f5a7c3-a547-4742-9fb2-2425f67c5199"
379 | },
380 | "source": [
381 | "(np.array(valores) > 3000).mean()"
382 | ],
383 | "execution_count": null,
384 | "outputs": [
385 | {
386 | "output_type": "execute_result",
387 | "data": {
388 | "text/plain": [
389 | "0.055"
390 | ]
391 | },
392 | "metadata": {
393 | "tags": []
394 | },
395 | "execution_count": 97
396 | }
397 | ]
398 | },
399 | {
400 | "cell_type": "markdown",
401 | "metadata": {
402 | "id": "9W7fAmdwuKyP",
403 | "colab_type": "text"
404 | },
405 | "source": [
406 | "7. Qual a probabilidade de se gastar menos que R\\$1500,00 para completar o álbum?\n"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "metadata": {
412 | "id": "iG81qzvWujAI",
413 | "colab_type": "code",
414 | "colab": {
415 | "base_uri": "https://localhost:8080/",
416 | "height": 34
417 | },
418 | "outputId": "9eec4eb3-2162-45ba-dec1-1e5e977a6602"
419 | },
420 | "source": [
421 | "(np.array(valores) < 1500).mean()"
422 | ],
423 | "execution_count": null,
424 | "outputs": [
425 | {
426 | "output_type": "execute_result",
427 | "data": {
428 | "text/plain": [
429 | "0.002"
430 | ]
431 | },
432 | "metadata": {
433 | "tags": []
434 | },
435 | "execution_count": 98
436 | }
437 | ]
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "metadata": {
442 | "id": "ZrZuJaiuuK01",
443 | "colab_type": "text"
444 | },
445 | "source": [
446 | "8. Qual a probabilidade de se gastar mais do que a média para completar o álbum?\n"
447 | ]
448 | },
449 | {
450 | "cell_type": "code",
451 | "metadata": {
452 | "id": "D8jWoXaNujdH",
453 | "colab_type": "code",
454 | "colab": {
455 | "base_uri": "https://localhost:8080/",
456 | "height": 34
457 | },
458 | "outputId": "dd53d0b4-7733-4be4-b5b7-37daecee2418"
459 | },
460 | "source": [
461 | "(np.array(valores) > np.mean(valores)).mean()"
462 | ],
463 | "execution_count": null,
464 | "outputs": [
465 | {
466 | "output_type": "execute_result",
467 | "data": {
468 | "text/plain": [
469 | "0.429"
470 | ]
471 | },
472 | "metadata": {
473 | "tags": []
474 | },
475 | "execution_count": 99
476 | }
477 | ]
478 | },
479 | {
480 | "cell_type": "markdown",
481 | "metadata": {
482 | "id": "DN2MYPMSuK3S",
483 | "colab_type": "text"
484 | },
485 | "source": [
486 | "9. Qual é o intervalo de confiança de 95% para o gasto ao se completar o álbum?\n"
487 | ]
488 | },
489 | {
490 | "cell_type": "code",
491 | "metadata": {
492 | "id": "qGEkU6emuj6H",
493 | "colab_type": "code",
494 | "colab": {
495 | "base_uri": "https://localhost:8080/",
496 | "height": 34
497 | },
498 | "outputId": "9aba72b7-e8c5-488b-a114-71f4f3f4ce3e"
499 | },
500 | "source": [
501 | "np.quantile(valores, [0.025, 0.975])"
502 | ],
503 | "execution_count": null,
504 | "outputs": [
505 | {
506 | "output_type": "execute_result",
507 | "data": {
508 | "text/plain": [
509 | "array([1678.8375, 3181.7125])"
510 | ]
511 | },
512 | "metadata": {
513 | "tags": []
514 | },
515 | "execution_count": 100
516 | }
517 | ]
518 | },
519 | {
520 | "cell_type": "markdown",
521 | "metadata": {
522 | "id": "daDpM8RCuWou",
523 | "colab_type": "text"
524 | },
525 | "source": [
526 | "10. Qual o valor médio gasto caso se esteja completando o álbum com mais um amigo?\n"
527 | ]
528 | },
529 | {
530 | "cell_type": "code",
531 | "metadata": {
532 | "id": "fG5utz8hukUh",
533 | "colab_type": "code",
534 | "colab": {}
535 | },
536 | "source": [
537 | "def SimulaAlbum2(n_amigos):\n",
538 | " album = np.zeros(n_album) \n",
539 | " pacotes = 0\n",
540 | " while not np.all(album > n_amigos):\n",
541 | " pacotinho = np.random.choice(range(n_album), 5)\n",
542 | " pacotes += 1\n",
543 | "\n",
544 | " for i in pacotinho:\n",
545 | " album[i] += 1\n",
546 | " \n",
547 | "\n",
548 | " valor_gasto = preco_album + preco_pacote * pacotes \n",
549 | " \n",
550 | " return valor_gasto"
551 | ],
552 | "execution_count": null,
553 | "outputs": []
554 | },
555 | {
556 | "cell_type": "code",
557 | "metadata": {
558 | "id": "SwqhlAak0A81",
559 | "colab_type": "code",
560 | "colab": {
561 | "base_uri": "https://localhost:8080/",
562 | "height": 188
563 | },
564 | "outputId": "22e8af2a-2c03-417f-f479-97222f366aa8"
565 | },
566 | "source": [
567 | "valores2 = []\n",
568 | "\n",
569 | "for i in range(simulacoes):\n",
570 | "\n",
571 | " valores2.append(SimulaAlbum2(n_amigos = 1))\n",
572 | "\n",
573 | " if (i+1) % 100 == 0:\n",
574 | " print('Simulação:', i+1, '/', simulacoes)"
575 | ],
576 | "execution_count": null,
577 | "outputs": [
578 | {
579 | "output_type": "stream",
580 | "text": [
581 | "Simulação: 100 / 1000\n",
582 | "Simulação: 200 / 1000\n",
583 | "Simulação: 300 / 1000\n",
584 | "Simulação: 400 / 1000\n",
585 | "Simulação: 500 / 1000\n",
586 | "Simulação: 600 / 1000\n",
587 | "Simulação: 700 / 1000\n",
588 | "Simulação: 800 / 1000\n",
589 | "Simulação: 900 / 1000\n",
590 | "Simulação: 1000 / 1000\n"
591 | ],
592 | "name": "stdout"
593 | }
594 | ]
595 | },
596 | {
597 | "cell_type": "code",
598 | "metadata": {
599 | "id": "lymCejs51xcg",
600 | "colab_type": "code",
601 | "colab": {
602 | "base_uri": "https://localhost:8080/",
603 | "height": 34
604 | },
605 | "outputId": "d44842bf-adb9-42d2-c5e0-72ae6a407b8f"
606 | },
607 | "source": [
608 | "valor2 = np.mean(valores2)/2\n",
609 | "print(valor2)"
610 | ],
611 | "execution_count": null,
612 | "outputs": [
613 | {
614 | "output_type": "stream",
615 | "text": [
616 | "1487.7200000000005\n"
617 | ],
618 | "name": "stdout"
619 | }
620 | ]
621 | },
622 | {
623 | "cell_type": "markdown",
624 | "metadata": {
625 | "id": "z2kS_pVtuW0r",
626 | "colab_type": "text"
627 | },
628 | "source": [
629 | "11. Quanto se economiza ao utilizar o cenário da questão 10?\n"
630 | ]
631 | },
632 | {
633 | "cell_type": "code",
634 | "metadata": {
635 | "id": "VuW3lqUSukwK",
636 | "colab_type": "code",
637 | "colab": {
638 | "base_uri": "https://localhost:8080/",
639 | "height": 34
640 | },
641 | "outputId": "df6ce2f3-422f-49ec-9af8-90756bf62be5"
642 | },
643 | "source": [
644 | "1 - valor2/np.mean(valores)"
645 | ],
646 | "execution_count": null,
647 | "outputs": [
648 | {
649 | "output_type": "execute_result",
650 | "data": {
651 | "text/plain": [
652 | "0.3402118549168568"
653 | ]
654 | },
655 | "metadata": {
656 | "tags": []
657 | },
658 | "execution_count": 109
659 | }
660 | ]
661 | },
662 | {
663 | "cell_type": "markdown",
664 | "metadata": {
665 | "id": "n5OXlAoJuW88",
666 | "colab_type": "text"
667 | },
668 | "source": [
669 | "12. Qual o valor médio gasto caso se esteja completando o álbum com mais dois amigos?\n"
670 | ]
671 | },
672 | {
673 | "cell_type": "code",
674 | "metadata": {
675 | "id": "HUWcTxLlulJU",
676 | "colab_type": "code",
677 | "colab": {
678 | "base_uri": "https://localhost:8080/",
679 | "height": 188
680 | },
681 | "outputId": "b6ee97cb-7894-4cc0-c365-b799d18bc12d"
682 | },
683 | "source": [
684 | "valores3 = []\n",
685 | "\n",
686 | "for i in range(simulacoes):\n",
687 | "\n",
688 | " valores3.append(SimulaAlbum2(n_amigos = 2))\n",
689 | "\n",
690 | " if (i+1) % 100 == 0:\n",
691 | " print('Simulação:', i+1, '/', simulacoes)"
692 | ],
693 | "execution_count": null,
694 | "outputs": [
695 | {
696 | "output_type": "stream",
697 | "text": [
698 | "Simulação: 100 / 1000\n",
699 | "Simulação: 200 / 1000\n",
700 | "Simulação: 300 / 1000\n",
701 | "Simulação: 400 / 1000\n",
702 | "Simulação: 500 / 1000\n",
703 | "Simulação: 600 / 1000\n",
704 | "Simulação: 700 / 1000\n",
705 | "Simulação: 800 / 1000\n",
706 | "Simulação: 900 / 1000\n",
707 | "Simulação: 1000 / 1000\n"
708 | ],
709 | "name": "stdout"
710 | }
711 | ]
712 | },
713 | {
714 | "cell_type": "code",
715 | "metadata": {
716 | "id": "FORO2yEK2JNi",
717 | "colab_type": "code",
718 | "colab": {
719 | "base_uri": "https://localhost:8080/",
720 | "height": 34
721 | },
722 | "outputId": "8b1036f0-943d-41d5-991d-a4cf8448514b"
723 | },
724 | "source": [
725 | "valor3 = np.mean(valores3)/3\n",
726 | "valor3"
727 | ],
728 | "execution_count": null,
729 | "outputs": [
730 | {
731 | "output_type": "execute_result",
732 | "data": {
733 | "text/plain": [
734 | "1208.8175000000003"
735 | ]
736 | },
737 | "metadata": {
738 | "tags": []
739 | },
740 | "execution_count": 112
741 | }
742 | ]
743 | },
744 | {
745 | "cell_type": "markdown",
746 | "metadata": {
747 | "id": "PsswhmybuXNa",
748 | "colab_type": "text"
749 | },
750 | "source": [
751 | "13. Quanto se economiza ao utilizar o cenário da questão 12?"
752 | ]
753 | },
754 | {
755 | "cell_type": "code",
756 | "metadata": {
757 | "id": "b4WX6qL-uln1",
758 | "colab_type": "code",
759 | "colab": {
760 | "base_uri": "https://localhost:8080/",
761 | "height": 34
762 | },
763 | "outputId": "2ddfcac0-cf30-4995-ebbd-fb3aebd330ed"
764 | },
765 | "source": [
766 | "1 - valor3/np.mean(valores)"
767 | ],
768 | "execution_count": null,
769 | "outputs": [
770 | {
771 | "output_type": "execute_result",
772 | "data": {
773 | "text/plain": [
774 | "0.4639021750940753"
775 | ]
776 | },
777 | "metadata": {
778 | "tags": []
779 | },
780 | "execution_count": 113
781 | }
782 | ]
783 | },
784 | {
785 | "cell_type": "markdown",
786 | "metadata": {
787 | "id": "DBDmjJTC4HbQ",
788 | "colab_type": "text"
789 | },
790 | "source": [
791 | "Bônus: E se fossem um grupo de 10 pessoas, como ficaria?"
792 | ]
793 | },
794 | {
795 | "cell_type": "code",
796 | "metadata": {
797 | "id": "bFcLNxaP4Oar",
798 | "colab_type": "code",
799 | "colab": {
800 | "base_uri": "https://localhost:8080/",
801 | "height": 188
802 | },
803 | "outputId": "0f902406-c1ec-4aea-88a5-a9e71bb56d59"
804 | },
805 | "source": [
806 | "valores10 = []\n",
807 | "\n",
808 | "for i in range(simulacoes):\n",
809 | "\n",
810 | " valores10.append(SimulaAlbum2(n_amigos = 9))\n",
811 | "\n",
812 | " if (i+1) % 100 == 0:\n",
813 | " print('Simulação:', i+1, '/', simulacoes)"
814 | ],
815 | "execution_count": null,
816 | "outputs": [
817 | {
818 | "output_type": "stream",
819 | "text": [
820 | "Simulação: 100 / 1000\n",
821 | "Simulação: 200 / 1000\n",
822 | "Simulação: 300 / 1000\n",
823 | "Simulação: 400 / 1000\n",
824 | "Simulação: 500 / 1000\n",
825 | "Simulação: 600 / 1000\n",
826 | "Simulação: 700 / 1000\n",
827 | "Simulação: 800 / 1000\n",
828 | "Simulação: 900 / 1000\n",
829 | "Simulação: 1000 / 1000\n"
830 | ],
831 | "name": "stdout"
832 | }
833 | ]
834 | },
835 | {
836 | "cell_type": "code",
837 | "metadata": {
838 | "id": "_SJQjUGD4HGY",
839 | "colab_type": "code",
840 | "colab": {
841 | "base_uri": "https://localhost:8080/",
842 | "height": 34
843 | },
844 | "outputId": "a39fb76a-5864-45eb-d3c0-ee93b908bcd2"
845 | },
846 | "source": [
847 | "valor10 = np.mean(valores10)/10\n",
848 | "valor10"
849 | ],
850 | "execution_count": null,
851 | "outputs": [
852 | {
853 | "output_type": "execute_result",
854 | "data": {
855 | "text/plain": [
856 | "728.6354999999998"
857 | ]
858 | },
859 | "metadata": {
860 | "tags": []
861 | },
862 | "execution_count": 119
863 | }
864 | ]
865 | },
866 | {
867 | "cell_type": "code",
868 | "metadata": {
869 | "id": "FABMQsQp4bNZ",
870 | "colab_type": "code",
871 | "colab": {
872 | "base_uri": "https://localhost:8080/",
873 | "height": 34
874 | },
875 | "outputId": "9724c296-655e-4118-ff78-322aedba7909"
876 | },
877 | "source": [
878 | "1 - valor10/np.mean(valores)"
879 | ],
880 | "execution_count": null,
881 | "outputs": [
882 | {
883 | "output_type": "execute_result",
884 | "data": {
885 | "text/plain": [
886 | "0.6768578328000374"
887 | ]
888 | },
889 | "metadata": {
890 | "tags": []
891 | },
892 | "execution_count": 120
893 | }
894 | ]
895 | }
896 | ]
897 | }
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 01.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 01.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 02.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 02.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 03.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 03.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 04.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 04.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 05.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 05.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 06.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 06.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 07.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 07.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 08.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 08.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 09.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 09.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 10.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 10.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 11.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 11.jpg
--------------------------------------------------------------------------------
/Imagens/capa - python para ml 12.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/capa - python para ml 12.jpg
--------------------------------------------------------------------------------
/Imagens/logo_matplotlib.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ricardorocha86/Python-para-Machine-Learning/4b33dc31742d76096814fe62a46fa2d01c2fb094/Imagens/logo_matplotlib.png
--------------------------------------------------------------------------------
/Python_para_ML_01.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Python para ML - 01",
7 | "provenance": [],
8 | "collapsed_sections": [],
9 | "toc_visible": true,
10 | "authorship_tag": "ABX9TyP3oRBgFFED+ZiKArnDewI8",
11 | "include_colab_link": true
12 | },
13 | "kernelspec": {
14 | "name": "python3",
15 | "display_name": "Python 3"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {
32 | "id": "3tJEOcVuvO9Z",
33 | "colab_type": "text"
34 | },
35 | "source": [
36 | " \n"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {
42 | "id": "oM35UJ-5pDci",
43 | "colab_type": "text"
44 | },
45 | "source": [
46 | "# **Python para Machine Learning** - Parte 1"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {
52 | "id": "4NfTY9LJpvkT",
53 | "colab_type": "text"
54 | },
55 | "source": [
56 | "Nesse curso, abordaremos os conceitos iniciais da linguagem Python com o foco de utilizá-la para modelagem preditiva e Machine Learning. \n",
57 | "\n",
58 | "O Python é uma linguagem de programação interpretada, criada com o propósito de ser o mais simples e legível possível. Foi em 1991 que o holandes Guido Van Rossum presenteou o mundo com essa linguagem e de lá pra cá ela teve uma longa história. Hoje em dia é uma das linguagens mais utilizadas no mundo e possui um papel fundamental no desenvolvimento em Inteligência Artificial/Data Science."
59 | ]
60 | },
61 | {
62 | "cell_type": "markdown",
63 | "metadata": {
64 | "id": "nDlsQrNPpqtf",
65 | "colab_type": "text"
66 | },
67 | "source": [
68 | "**Nesse notebook veremos:**\n",
69 | "1. [Atribuição de Variáveis](#atri)\n",
70 | "2. [Primeiras Funções](#prim)\n",
71 | "3. [Operadores Aritméticos](#oper)\n",
72 | "4. [Comparações e Booleanos](#comp)\n",
73 | "5. [Condicional IF-ELSE](#cond)\n",
74 | "6. [Definição de Funções](#defi)\n",
75 | "7. [Importação de Bibliotecas](#impo)\n",
76 | "8. [Exercícios](#exer)\n",
77 | "9. [Assuntos da Próxima Aula](#assu) \n",
78 | "10. [Links Úteis](#links)\n",
79 | "11. [Anexo: Zen of Python](#anex)\n"
80 | ]
81 | },
82 | {
83 | "cell_type": "markdown",
84 | "metadata": {
85 | "id": "8kMiWTL0pvFF",
86 | "colab_type": "text"
87 | },
88 | "source": [
89 | "## **Atribuição de Variáveis** "
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "metadata": {
95 | "id": "dwHX8egGBuTc",
96 | "colab_type": "code",
97 | "colab": {
98 | "base_uri": "https://localhost:8080/",
99 | "height": 90
100 | },
101 | "outputId": "ed01ed77-adb5-4142-88ac-dcb29a53e006"
102 | },
103 | "source": [
104 | "meu_nome = 'Ricardo'\n",
105 | "meu_sobrenome = 'Rocha'\n",
106 | "meu_produto = \"Caixa d'agua\"\n",
107 | "minha_frase = '\"Vamos que vamos!\"'\n",
108 | "\n",
109 | "print(meu_nome)\n",
110 | "print(meu_sobrenome)\n",
111 | "print(meu_produto)\n",
112 | "print(minha_frase)"
113 | ],
114 | "execution_count": null,
115 | "outputs": [
116 | {
117 | "output_type": "stream",
118 | "text": [
119 | "Ricardo\n",
120 | "Rocha\n",
121 | "Caixa d'agua\n",
122 | "\"Vamos que vamos!\"\n"
123 | ],
124 | "name": "stdout"
125 | }
126 | ]
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "metadata": {
131 | "id": "zXKWnv0wrllN",
132 | "colab_type": "text"
133 | },
134 | "source": [
135 | "Soma de duas varíaveis do tipo string\n"
136 | ]
137 | },
138 | {
139 | "cell_type": "code",
140 | "metadata": {
141 | "id": "Bqnflhqfrk4h",
142 | "colab_type": "code",
143 | "colab": {
144 | "base_uri": "https://localhost:8080/",
145 | "height": 35
146 | },
147 | "outputId": "68f0a3f1-9003-4b07-ec78-6698f45fe623"
148 | },
149 | "source": [
150 | "meu_nome + meu_sobrenome"
151 | ],
152 | "execution_count": null,
153 | "outputs": [
154 | {
155 | "output_type": "execute_result",
156 | "data": {
157 | "text/plain": [
158 | "'RicardoRocha'"
159 | ]
160 | },
161 | "metadata": {
162 | "tags": []
163 | },
164 | "execution_count": 144
165 | }
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "metadata": {
171 | "id": "xBT9RtP3ryFq",
172 | "colab_type": "code",
173 | "colab": {
174 | "base_uri": "https://localhost:8080/",
175 | "height": 35
176 | },
177 | "outputId": "68a499aa-19cc-4b30-aefa-587fbe52a4cd"
178 | },
179 | "source": [
180 | "meu_nome + ' ' + meu_sobrenome"
181 | ],
182 | "execution_count": null,
183 | "outputs": [
184 | {
185 | "output_type": "execute_result",
186 | "data": {
187 | "text/plain": [
188 | "'Ricardo Rocha'"
189 | ]
190 | },
191 | "metadata": {
192 | "tags": []
193 | },
194 | "execution_count": 145
195 | }
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "metadata": {
201 | "id": "a_tw5S1Hq0Ep",
202 | "colab_type": "text"
203 | },
204 | "source": [
205 | "Não é possível atribuir variáveis com nomes começando com números, nem com espaços no nome ou com aspas. "
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "metadata": {
211 | "id": "u3HvQ2e_YxID",
212 | "colab_type": "code",
213 | "colab": {}
214 | },
215 | "source": [
216 | "# 1_a = 2\n",
217 | "# var com espaco = 2\n",
218 | "# a'd = 12 "
219 | ],
220 | "execution_count": null,
221 | "outputs": []
222 | },
223 | {
224 | "cell_type": "markdown",
225 | "metadata": {
226 | "id": "nWqVaqeBrAb_",
227 | "colab_type": "text"
228 | },
229 | "source": [
230 | "OBS.: O **hashtag #** na frente de uma linha torna ela um **comentário**, pois o interpretador ignora tudo que vem depois dela "
231 | ]
232 | },
233 | {
234 | "cell_type": "markdown",
235 | "metadata": {
236 | "id": "38emYI_xJqeO",
237 | "colab_type": "text"
238 | },
239 | "source": [
240 | "## **Primeiras Funções** "
241 | ]
242 | },
243 | {
244 | "cell_type": "markdown",
245 | "metadata": {
246 | "id": "7JyT6165pi6l",
247 | "colab_type": "text"
248 | },
249 | "source": [
250 | "Conheça a função **len**, que retorna o tamanho de uma string (e de outros tipos de objetos também).\n",
251 | "\n",
252 | "A função **type**, que retorna o tipo do objeto na variável de entrada\n",
253 | "\n",
254 | "A função **round**, que arredonda um número de acordo com as casas decimas desejadas\n",
255 | "\n",
256 | "E a função **help**, que retorna a documentação da função inserida como entrada."
257 | ]
258 | },
259 | {
260 | "cell_type": "code",
261 | "metadata": {
262 | "id": "CNTROt8rb4Lg",
263 | "colab_type": "code",
264 | "colab": {
265 | "base_uri": "https://localhost:8080/",
266 | "height": 35
267 | },
268 | "outputId": "7aedb501-b5ac-4956-ea3d-eb7879efe1d2"
269 | },
270 | "source": [
271 | "nome = 'Ricardo'\n",
272 | "len(nome)"
273 | ],
274 | "execution_count": null,
275 | "outputs": [
276 | {
277 | "output_type": "execute_result",
278 | "data": {
279 | "text/plain": [
280 | "7"
281 | ]
282 | },
283 | "metadata": {
284 | "tags": []
285 | },
286 | "execution_count": 162
287 | }
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "metadata": {
293 | "id": "MAO1YA7ywWfa",
294 | "colab_type": "code",
295 | "colab": {
296 | "base_uri": "https://localhost:8080/",
297 | "height": 35
298 | },
299 | "outputId": "aba4974c-48f9-4e2b-9d3c-8f6633214e42"
300 | },
301 | "source": [
302 | "type(nome) "
303 | ],
304 | "execution_count": null,
305 | "outputs": [
306 | {
307 | "output_type": "execute_result",
308 | "data": {
309 | "text/plain": [
310 | "str"
311 | ]
312 | },
313 | "metadata": {
314 | "tags": []
315 | },
316 | "execution_count": 161
317 | }
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "metadata": {
323 | "id": "hVwnbtKYJ8YU",
324 | "colab_type": "code",
325 | "colab": {
326 | "base_uri": "https://localhost:8080/",
327 | "height": 35
328 | },
329 | "outputId": "a9143226-f0f0-4671-8a2e-bdd8acd0d099"
330 | },
331 | "source": [
332 | "idade = 33.54234523\n",
333 | "round(idade, 2)"
334 | ],
335 | "execution_count": null,
336 | "outputs": [
337 | {
338 | "output_type": "execute_result",
339 | "data": {
340 | "text/plain": [
341 | "33.54"
342 | ]
343 | },
344 | "metadata": {
345 | "tags": []
346 | },
347 | "execution_count": 164
348 | }
349 | ]
350 | },
351 | {
352 | "cell_type": "code",
353 | "metadata": {
354 | "id": "9o2s8MOIKTCT",
355 | "colab_type": "code",
356 | "colab": {
357 | "base_uri": "https://localhost:8080/",
358 | "height": 181
359 | },
360 | "outputId": "b275a358-4727-4fb3-8b24-d93b9168aa9a"
361 | },
362 | "source": [
363 | "help(round)"
364 | ],
365 | "execution_count": null,
366 | "outputs": [
367 | {
368 | "output_type": "stream",
369 | "text": [
370 | "Help on built-in function round in module builtins:\n",
371 | "\n",
372 | "round(...)\n",
373 | " round(number[, ndigits]) -> number\n",
374 | " \n",
375 | " Round a number to a given precision in decimal digits (default 0 digits).\n",
376 | " This returns an int when called with one argument, otherwise the\n",
377 | " same type as the number. ndigits may be negative.\n",
378 | "\n"
379 | ],
380 | "name": "stdout"
381 | }
382 | ]
383 | },
384 | {
385 | "cell_type": "markdown",
386 | "metadata": {
387 | "id": "ZH4vZ7NtI5Jx",
388 | "colab_type": "text"
389 | },
390 | "source": [
391 | "Não só no Python, mas em programação em geral, sempre precisamos controlar o tipo das variáveis que utilizamos. Veja o exemplo abaixo:"
392 | ]
393 | },
394 | {
395 | "cell_type": "code",
396 | "metadata": {
397 | "id": "DvKNUkrPIlDt",
398 | "colab_type": "code",
399 | "colab": {
400 | "base_uri": "https://localhost:8080/",
401 | "height": 35
402 | },
403 | "outputId": "793aaf4a-23d5-43af-f82a-c37ea1050044"
404 | },
405 | "source": [
406 | "minha_idade = '33'\n",
407 | "type(minha_idade)\n"
408 | ],
409 | "execution_count": null,
410 | "outputs": [
411 | {
412 | "output_type": "execute_result",
413 | "data": {
414 | "text/plain": [
415 | "str"
416 | ]
417 | },
418 | "metadata": {
419 | "tags": []
420 | },
421 | "execution_count": 160
422 | }
423 | ]
424 | },
425 | {
426 | "cell_type": "markdown",
427 | "metadata": {
428 | "id": "fyCVd6Tov_ao",
429 | "colab_type": "text"
430 | },
431 | "source": [
432 | "Usando a função **int** para converter a variável em formato string para o formato inteiro"
433 | ]
434 | },
435 | {
436 | "cell_type": "code",
437 | "metadata": {
438 | "id": "svsiydRLv8vT",
439 | "colab_type": "code",
440 | "colab": {
441 | "base_uri": "https://localhost:8080/",
442 | "height": 35
443 | },
444 | "outputId": "8acee7fd-17cd-49d3-df49-23ef86642b69"
445 | },
446 | "source": [
447 | "\n",
448 | "minha_idade = int(minha_idade)\n",
449 | "type(minha_idade)"
450 | ],
451 | "execution_count": null,
452 | "outputs": [
453 | {
454 | "output_type": "execute_result",
455 | "data": {
456 | "text/plain": [
457 | "int"
458 | ]
459 | },
460 | "metadata": {
461 | "tags": []
462 | },
463 | "execution_count": 159
464 | }
465 | ]
466 | },
467 | {
468 | "cell_type": "markdown",
469 | "metadata": {
470 | "id": "RbEdJeDVysFT",
471 | "colab_type": "text"
472 | },
473 | "source": [
474 | "E a função **str** para converter um objeto para string"
475 | ]
476 | },
477 | {
478 | "cell_type": "code",
479 | "metadata": {
480 | "id": "w7PobqiNyv9u",
481 | "colab_type": "code",
482 | "colab": {
483 | "base_uri": "https://localhost:8080/",
484 | "height": 35
485 | },
486 | "outputId": "7b643af9-22b7-4293-a72d-dfe014a1421e"
487 | },
488 | "source": [
489 | "minha_idade = str(minha_idade)\n",
490 | "type(minha_idade)"
491 | ],
492 | "execution_count": null,
493 | "outputs": [
494 | {
495 | "output_type": "execute_result",
496 | "data": {
497 | "text/plain": [
498 | "str"
499 | ]
500 | },
501 | "metadata": {
502 | "tags": []
503 | },
504 | "execution_count": 166
505 | }
506 | ]
507 | },
508 | {
509 | "cell_type": "markdown",
510 | "metadata": {
511 | "id": "PUH_pzLDJa4Q",
512 | "colab_type": "text"
513 | },
514 | "source": [
515 | "## **Operadores Aritméticos** "
516 | ]
517 | },
518 | {
519 | "cell_type": "markdown",
520 | "metadata": {
521 | "id": "bS7hxv-Codpz",
522 | "colab_type": "text"
523 | },
524 | "source": [
525 | "Confira na tabela abaixo como funcionam os operadores aritméticos em Python. \n",
526 | "\n",
527 | "Em especial, veja que a sintaxe da exponenciação não é feita através do sinal '^'."
528 | ]
529 | },
530 | {
531 | "cell_type": "markdown",
532 | "metadata": {
533 | "id": "mHsbgk0UGIaL",
534 | "colab_type": "text"
535 | },
536 | "source": [
537 | "| Operator | Name | Description |\n",
538 | "|--------------|----------------|--------------------------------------------------------|\n",
539 | "| ``a + b`` | Adiçao | Soma entre ``a`` e ``b`` |\n",
540 | "| ``a - b`` | Subtração | Diferença entre ``a`` e ``b`` |\n",
541 | "| ``a * b`` | Multiplicação | Produto entre ``a`` e ``b`` |\n",
542 | "| ``a / b`` | Divisão | Divisão usual entre ``a`` e ``b`` |\n",
543 | "| ``a // b`` | Divisão inteira | A divisão entre ``a`` e ``b``, removendo as partes decimais |\n",
544 | "| ``a % b`` | Resto da divisão | O resto da divisão inteira de ``a`` por ``b`` |\n",
545 | "| ``a ** b`` | Exponenciação | ``a`` elevado a ``b`` |\n",
546 | "| ``-a`` | Negação | O negativo de ``a`` |"
547 | ]
548 | },
549 | {
550 | "cell_type": "code",
551 | "metadata": {
552 | "id": "xZVTqDzUGWlj",
553 | "colab_type": "code",
554 | "colab": {}
555 | },
556 | "source": [
557 | "a = 7\n",
558 | "b = 2"
559 | ],
560 | "execution_count": null,
561 | "outputs": []
562 | },
563 | {
564 | "cell_type": "code",
565 | "metadata": {
566 | "id": "T9gTYGKo31B4",
567 | "colab_type": "code",
568 | "colab": {}
569 | },
570 | "source": [
571 | "print(a + b)\n",
572 | "print(a - b)\n",
573 | "print(a * b)\n",
574 | "print(a / b)\n",
575 | "print(a // b)\n",
576 | "print(a % b)\n",
577 | "print(a ** b)\n",
578 | "print(-a)"
579 | ],
580 | "execution_count": null,
581 | "outputs": []
582 | },
583 | {
584 | "cell_type": "markdown",
585 | "metadata": {
586 | "id": "BgFHWhOOsbv0",
587 | "colab_type": "text"
588 | },
589 | "source": [
590 | "Temos também as funções **min**, **max** e **abs**, para calcular, respectivamente, o mínimo, o máximo e o valor absoluto de um número"
591 | ]
592 | },
593 | {
594 | "cell_type": "code",
595 | "metadata": {
596 | "id": "FLOeFbsZGcx2",
597 | "colab_type": "code",
598 | "colab": {
599 | "base_uri": "https://localhost:8080/",
600 | "height": 90
601 | },
602 | "outputId": "35dabf11-ce8f-4f7a-c352-aedbddcaa52b"
603 | },
604 | "source": [
605 | "print(max(a, b))\n",
606 | "print(min(a, b))\n",
607 | "print(abs(-a))"
608 | ],
609 | "execution_count": null,
610 | "outputs": [
611 | {
612 | "output_type": "stream",
613 | "text": [
614 | "1.0\n",
615 | "1\n",
616 | "-2\n",
617 | "1\n"
618 | ],
619 | "name": "stdout"
620 | }
621 | ]
622 | },
623 | {
624 | "cell_type": "markdown",
625 | "metadata": {
626 | "id": "Lh6dGL01YdHZ",
627 | "colab_type": "text"
628 | },
629 | "source": [
630 | "## **Comparações e Booleanos** "
631 | ]
632 | },
633 | {
634 | "cell_type": "markdown",
635 | "metadata": {
636 | "id": "zJGYOSKzn62F",
637 | "colab_type": "text"
638 | },
639 | "source": [
640 | "Quando comparamos objetos em Python é como se estivéssemos fazendo uma pergunta a ele. Duas variáveis são iguais? Uma é maior que a outra? \n",
641 | "\n",
642 | "E a resposta é um objeto do tipo **booleano**, que é indicado por **True** ou **False**, indicando, respectivamente, se a resposta é verdadeira ou falsa."
643 | ]
644 | },
645 | {
646 | "cell_type": "code",
647 | "metadata": {
648 | "id": "h13Wb-T0YoL8",
649 | "colab_type": "code",
650 | "colab": {
651 | "base_uri": "https://localhost:8080/",
652 | "height": 35
653 | },
654 | "outputId": "8d504728-9ffe-44bc-9943-63d7c18df4fa"
655 | },
656 | "source": [
657 | "var1 = 35\n",
658 | "var2 = 36\n",
659 | "\n",
660 | "var1 == var2"
661 | ],
662 | "execution_count": null,
663 | "outputs": [
664 | {
665 | "output_type": "execute_result",
666 | "data": {
667 | "text/plain": [
668 | "False"
669 | ]
670 | },
671 | "metadata": {
672 | "tags": []
673 | },
674 | "execution_count": 136
675 | }
676 | ]
677 | },
678 | {
679 | "cell_type": "code",
680 | "metadata": {
681 | "id": "J_u74wBhZDJ6",
682 | "colab_type": "code",
683 | "colab": {
684 | "base_uri": "https://localhost:8080/",
685 | "height": 35
686 | },
687 | "outputId": "4a99b09e-db03-4485-d5c8-19c6cc69502f"
688 | },
689 | "source": [
690 | "var1 > var2"
691 | ],
692 | "execution_count": null,
693 | "outputs": [
694 | {
695 | "output_type": "execute_result",
696 | "data": {
697 | "text/plain": [
698 | "False"
699 | ]
700 | },
701 | "metadata": {
702 | "tags": []
703 | },
704 | "execution_count": 137
705 | }
706 | ]
707 | },
708 | {
709 | "cell_type": "code",
710 | "metadata": {
711 | "id": "sVC_1dfjZFu5",
712 | "colab_type": "code",
713 | "colab": {
714 | "base_uri": "https://localhost:8080/",
715 | "height": 35
716 | },
717 | "outputId": "7fab7576-b813-420c-b8bc-c4fda0deff17"
718 | },
719 | "source": [
720 | "var1 <= var2"
721 | ],
722 | "execution_count": null,
723 | "outputs": [
724 | {
725 | "output_type": "execute_result",
726 | "data": {
727 | "text/plain": [
728 | "True"
729 | ]
730 | },
731 | "metadata": {
732 | "tags": []
733 | },
734 | "execution_count": 138
735 | }
736 | ]
737 | },
738 | {
739 | "cell_type": "code",
740 | "metadata": {
741 | "id": "W_tNf7CldSvp",
742 | "colab_type": "code",
743 | "colab": {
744 | "base_uri": "https://localhost:8080/",
745 | "height": 72
746 | },
747 | "outputId": "2a71a5f2-a775-4a0e-ce81-dbc2000b86a7"
748 | },
749 | "source": [
750 | "print(type(33.2) == str)\n",
751 | "print(type(33.2) == int)\n",
752 | "print(type(33.2) == float)"
753 | ],
754 | "execution_count": null,
755 | "outputs": [
756 | {
757 | "output_type": "stream",
758 | "text": [
759 | "False\n",
760 | "False\n",
761 | "True\n"
762 | ],
763 | "name": "stdout"
764 | }
765 | ]
766 | },
767 | {
768 | "cell_type": "markdown",
769 | "metadata": {
770 | "id": "SF6EtbmrZQO3",
771 | "colab_type": "text"
772 | },
773 | "source": [
774 | "## **Condicional IF-ELSE** "
775 | ]
776 | },
777 | {
778 | "cell_type": "markdown",
779 | "metadata": {
780 | "id": "t9WtbCPDAWkW",
781 | "colab_type": "text"
782 | },
783 | "source": [
784 | "Veja abaixo como funciona a estrutura geral de condicionais no Python. \n",
785 | "\n",
786 | "Temos a versão IF ELSE, e a versão IF ELIF ELSE.\n",
787 | "\n",
788 | "Note que é possível utilizar o ELIF várias vezes se necessário.\n",
789 | "\n",
790 | "No espaço de condição, o Python espera um objeto booleano. Se for True, ele executa, se for False, segue adiante. \n",
791 | "\n",
792 | "Note também a estrutura de identação do Python. Aqui não usamos (), [] ou {} para estipular o que deve ser executado nas condicionais. Simplesmente colocamos o código que deve ser executado na linha seguinte, mas identado. Em geral, se utiliza o espaço de **1 tab**."
793 | ]
794 | },
795 | {
796 | "cell_type": "code",
797 | "metadata": {
798 | "id": "kuqS_I9GdobX",
799 | "colab_type": "code",
800 | "colab": {}
801 | },
802 | "source": [
803 | "# sintaxe IF-ELSE geral\n",
804 | "if condição:\n",
805 | " executa aqui\n",
806 | "else:\n",
807 | " executa aqui\n",
808 | "\n",
809 | "# sintaxe IF-ELIF-ELSE geral\n",
810 | "if condição:\n",
811 | " executa aqui\n",
812 | "elif outra_condição:\n",
813 | " executa aqui\n",
814 | "else:\n",
815 | " executa aqui\n",
816 | "\n",
817 | "#não executar essa célula!"
818 | ],
819 | "execution_count": null,
820 | "outputs": []
821 | },
822 | {
823 | "cell_type": "code",
824 | "metadata": {
825 | "id": "X7Fw4opvdznZ",
826 | "colab_type": "code",
827 | "colab": {
828 | "base_uri": "https://localhost:8080/",
829 | "height": 35
830 | },
831 | "outputId": "83a5d3fb-5898-4627-de2c-56f8f85f723d"
832 | },
833 | "source": [
834 | "a, b, c = 1, -2 , 0\n",
835 | "\n",
836 | "delta = b**2 - 4*a*c\n",
837 | "\n",
838 | "if delta > 0:\n",
839 | " x1 = (-b + delta**0.5)/(2*a)\n",
840 | " x2 = (-b - delta**0.5)/(2*a)\n",
841 | " print('As raízes são {} e {}'.format(x1, x2))\n",
842 | "elif delta == 0:\n",
843 | " x = -b/(2*a)\n",
844 | " print('A única raíz é {}'.format(x))\n",
845 | "else:\n",
846 | " print('Não há soluções reais para essa equação')"
847 | ],
848 | "execution_count": null,
849 | "outputs": [
850 | {
851 | "output_type": "stream",
852 | "text": [
853 | "As raízes são 3.0 e 1.0\n"
854 | ],
855 | "name": "stdout"
856 | }
857 | ]
858 | },
859 | {
860 | "cell_type": "markdown",
861 | "metadata": {
862 | "id": "3K7wNqEIembT",
863 | "colab_type": "text"
864 | },
865 | "source": [
866 | "## **Definição de Funções** "
867 | ]
868 | },
869 | {
870 | "cell_type": "markdown",
871 | "metadata": {
872 | "id": "jEMYVdTLlf5t",
873 | "colab_type": "text"
874 | },
875 | "source": [
876 | "Uma função é um recurso para não precisarmos repetir o mesmo código desnecessariamente. Assim como as funções da matemática, sua tarefa é transformar um input, uma entrada, uma coleção de variáveis, em uma saída, um output. \n",
877 | "\n",
878 | "No exemplo abaixo, utilizamos como variáveis o tipo de símbolo e o tamanho da linha que queremos fazer. Você entenderá melhor vendo o exemplo:\n",
879 | "\n",
880 | "Da mesma maneira que na estrutura de condicionais, o conteúdo de uma função fica nas linhas seguintes sendo indicados pela **indentação** do código"
881 | ]
882 | },
883 | {
884 | "cell_type": "code",
885 | "metadata": {
886 | "id": "VQ-INeRi6tTG",
887 | "colab_type": "code",
888 | "colab": {}
889 | },
890 | "source": [
891 | "def Mensagem(simbolo = '*', tamanho = 51):\n",
892 | " print(simbolo*tamanho)\n",
893 | " print('Estamos só começando, o melhor ainda estar por vir!')\n",
894 | " print(simbolo*tamanho)"
895 | ],
896 | "execution_count": null,
897 | "outputs": []
898 | },
899 | {
900 | "cell_type": "code",
901 | "metadata": {
902 | "id": "YLiKW1mVu3B2",
903 | "colab_type": "code",
904 | "colab": {
905 | "base_uri": "https://localhost:8080/",
906 | "height": 72
907 | },
908 | "outputId": "a5f2236f-48c0-4e88-d196-c2d8df28772d"
909 | },
910 | "source": [
911 | "Mensagem('-', 51) "
912 | ],
913 | "execution_count": null,
914 | "outputs": [
915 | {
916 | "output_type": "stream",
917 | "text": [
918 | "---------------------------------------------------\n",
919 | "Estamos só começando, o melhor ainda estar por vir!\n",
920 | "---------------------------------------------------\n"
921 | ],
922 | "name": "stdout"
923 | }
924 | ]
925 | },
926 | {
927 | "cell_type": "code",
928 | "metadata": {
929 | "id": "Bh8kgefAu7E7",
930 | "colab_type": "code",
931 | "colab": {
932 | "base_uri": "https://localhost:8080/",
933 | "height": 72
934 | },
935 | "outputId": "a36ae15d-35b0-4caa-c24b-d2f4e97f8998"
936 | },
937 | "source": [
938 | "Mensagem('*-*', 17)"
939 | ],
940 | "execution_count": null,
941 | "outputs": [
942 | {
943 | "output_type": "stream",
944 | "text": [
945 | "*-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-*\n",
946 | "Estamos só começando, o melhor ainda estar por vir!\n",
947 | "*-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-*\n"
948 | ],
949 | "name": "stdout"
950 | }
951 | ]
952 | },
953 | {
954 | "cell_type": "markdown",
955 | "metadata": {
956 | "id": "s8XyjoLsmZB3",
957 | "colab_type": "text"
958 | },
959 | "source": [
960 | "Voltando no exemplo da fórmula de Bhaskara, fica bem conveniente quando colocamos no formato de função, para analisar as saídas de maneira mais prática. "
961 | ]
962 | },
963 | {
964 | "cell_type": "code",
965 | "metadata": {
966 | "id": "9s_3C-ZrgKPb",
967 | "colab_type": "code",
968 | "colab": {}
969 | },
970 | "source": [
971 | "def Bhaskara(a, b, c):\n",
972 | " # dando uma resposta adequada para quando o valor de a for igual a zero\n",
973 | " if a == 0:\n",
974 | " print('Quando a = 0 não se aplica Bhaskara')\n",
975 | " else:\n",
976 | " delta = b**2 - 4*a*c\n",
977 | "\n",
978 | " if delta > 0:\n",
979 | " x1 = (-b + delta**0.5)/(2*a)\n",
980 | " x2 = (-b - delta**0.5)/(2*a)\n",
981 | " print('As raízes são {} e {}'.format(round(x1, 2), round(x2, 2)))\n",
982 | " elif delta == 0:\n",
983 | " x = -b/(2*a)\n",
984 | " print('A única raíz é {}'.format(x))\n",
985 | " else:\n",
986 | " print('Não há soluções reais para essa equação')"
987 | ],
988 | "execution_count": null,
989 | "outputs": []
990 | },
991 | {
992 | "cell_type": "code",
993 | "metadata": {
994 | "id": "zs9zyfICgddJ",
995 | "colab_type": "code",
996 | "colab": {
997 | "base_uri": "https://localhost:8080/",
998 | "height": 35
999 | },
1000 | "outputId": "17e12e19-8632-4ca0-a6ea-e18897d1f8ef"
1001 | },
1002 | "source": [
1003 | "Bhaskara(3, 2, 0.9)"
1004 | ],
1005 | "execution_count": null,
1006 | "outputs": [
1007 | {
1008 | "output_type": "stream",
1009 | "text": [
1010 | "Não há soluções reais para essa equação\n"
1011 | ],
1012 | "name": "stdout"
1013 | }
1014 | ]
1015 | },
1016 | {
1017 | "cell_type": "code",
1018 | "metadata": {
1019 | "id": "Q9DIe2akvNjC",
1020 | "colab_type": "code",
1021 | "colab": {
1022 | "base_uri": "https://localhost:8080/",
1023 | "height": 35
1024 | },
1025 | "outputId": "4dacda38-6993-4d65-a950-edc774ca194b"
1026 | },
1027 | "source": [
1028 | "Bhaskara(-3, 2, 0.9)"
1029 | ],
1030 | "execution_count": null,
1031 | "outputs": [
1032 | {
1033 | "output_type": "stream",
1034 | "text": [
1035 | "As raízes são -0.31 e 0.97\n"
1036 | ],
1037 | "name": "stdout"
1038 | }
1039 | ]
1040 | },
1041 | {
1042 | "cell_type": "code",
1043 | "metadata": {
1044 | "id": "ngQ_4XV8vOKj",
1045 | "colab_type": "code",
1046 | "colab": {
1047 | "base_uri": "https://localhost:8080/",
1048 | "height": 35
1049 | },
1050 | "outputId": "4d75aa90-d95a-4052-c927-b4d5cd020922"
1051 | },
1052 | "source": [
1053 | "Bhaskara(1, -2, 1)"
1054 | ],
1055 | "execution_count": null,
1056 | "outputs": [
1057 | {
1058 | "output_type": "stream",
1059 | "text": [
1060 | "A única raíz é 1.0\n"
1061 | ],
1062 | "name": "stdout"
1063 | }
1064 | ]
1065 | },
1066 | {
1067 | "cell_type": "markdown",
1068 | "metadata": {
1069 | "id": "9vmnCWv9mNwX",
1070 | "colab_type": "text"
1071 | },
1072 | "source": [
1073 | "## **Importação de Bibliotecas** "
1074 | ]
1075 | },
1076 | {
1077 | "cell_type": "code",
1078 | "metadata": {
1079 | "id": "JuvRAAl0mTD8",
1080 | "colab_type": "code",
1081 | "colab": {
1082 | "base_uri": "https://localhost:8080/",
1083 | "height": 35
1084 | },
1085 | "outputId": "c737cdf9-c6c8-4ece-c97b-37564642cd32"
1086 | },
1087 | "source": [
1088 | "from math import pi\n",
1089 | "print(pi)"
1090 | ],
1091 | "execution_count": null,
1092 | "outputs": [
1093 | {
1094 | "output_type": "stream",
1095 | "text": [
1096 | "3.141592653589793\n"
1097 | ],
1098 | "name": "stdout"
1099 | }
1100 | ]
1101 | },
1102 | {
1103 | "cell_type": "code",
1104 | "metadata": {
1105 | "id": "HoE-Zyp5ma7S",
1106 | "colab_type": "code",
1107 | "colab": {
1108 | "base_uri": "https://localhost:8080/",
1109 | "height": 35
1110 | },
1111 | "outputId": "d274206f-9a38-4e95-96ed-7361ad8ba436"
1112 | },
1113 | "source": [
1114 | "import math\n",
1115 | "print(math.pi)"
1116 | ],
1117 | "execution_count": null,
1118 | "outputs": [
1119 | {
1120 | "output_type": "stream",
1121 | "text": [
1122 | "3.141592653589793\n"
1123 | ],
1124 | "name": "stdout"
1125 | }
1126 | ]
1127 | },
1128 | {
1129 | "cell_type": "code",
1130 | "metadata": {
1131 | "id": "tCtUvjfY6Nbw",
1132 | "colab_type": "code",
1133 | "colab": {
1134 | "base_uri": "https://localhost:8080/",
1135 | "height": 35
1136 | },
1137 | "outputId": "0522ab9f-37f4-4902-85fd-b6a17bc4976f"
1138 | },
1139 | "source": [
1140 | "import math as m\n",
1141 | "print(m.pi)"
1142 | ],
1143 | "execution_count": null,
1144 | "outputs": [
1145 | {
1146 | "output_type": "stream",
1147 | "text": [
1148 | "3.141592653589793\n"
1149 | ],
1150 | "name": "stdout"
1151 | }
1152 | ]
1153 | },
1154 | {
1155 | "cell_type": "markdown",
1156 | "metadata": {
1157 | "id": "r1X1E_zEge7K",
1158 | "colab_type": "text"
1159 | },
1160 | "source": [
1161 | "## **Exercícios** \n",
1162 | "\n",
1163 | "1. Considere um balde cuja base possui raio $r_1$ e altura igual ao diâmetro da base. Considere também uma esfera de raio $r_2$ cheia de água. Faça um programa que verifique se o volume da esfera cabe no balde, dados os valores de $r_1$ e $r_2$. \n",
1164 | "\n",
1165 | "2. Crie uma função que simule o jogo do *jokempô*, isto é, dada a entrada de dois jogadores, retorne a indicação de qual deles venceu. "
1166 | ]
1167 | },
1168 | {
1169 | "cell_type": "markdown",
1170 | "metadata": {
1171 | "id": "ClqLxTV3Euys",
1172 | "colab_type": "text"
1173 | },
1174 | "source": [
1175 | "## **Assuntos da Próxima Aula** \n",
1176 | "\n",
1177 | "- Listas e Tuplas\n",
1178 | "- Dicionários\n",
1179 | "- Loops FOR e WHILE\n",
1180 | "- Funções e métodos"
1181 | ]
1182 | },
1183 | {
1184 | "cell_type": "markdown",
1185 | "metadata": {
1186 | "id": "nSJC-C50lrdi",
1187 | "colab_type": "text"
1188 | },
1189 | "source": [
1190 | " ## **Links Úteis** "
1191 | ]
1192 | },
1193 | {
1194 | "cell_type": "markdown",
1195 | "metadata": {
1196 | "id": "qvCE4Gnglshy",
1197 | "colab_type": "text"
1198 | },
1199 | "source": [
1200 | "1. [Documentação do Python](https://docs.python.org/3/)\n",
1201 | "2. [Download do Anaconda](https://anaconda.org/)\n",
1202 | "3. [Curso do Gustavo Guanabara no YouTube (Canal Curso em Vídeo)](https://www.youtube.com/watch?v=S9uPNppGsGo&list=PLvE-ZAFRgX8hnECDn1v9HNTI71veL3oW0)\n",
1203 | "4. [Curso de Python gratuito do Kaggle (inglês)](https://www.kaggle.com/learn/python)\n",
1204 | "5. [Conceitos de Python em 40min por Derek Banas (inglês)](https://www.youtube.com/watch?v=N4mEzFDjqtA) \n"
1205 | ]
1206 | },
1207 | {
1208 | "cell_type": "markdown",
1209 | "metadata": {
1210 | "id": "UE-KpVQlpZYZ",
1211 | "colab_type": "text"
1212 | },
1213 | "source": [
1214 | "## **Anexo** \n",
1215 | "\n",
1216 | "### **The Zen of Python, por Tim Peters**\n",
1217 | "\n",
1218 | "É um conjunto de regras pela qual o Python é idealizado.\n",
1219 | "\n",
1220 | "1. Bonito é melhor que feio.\n",
1221 | "2. Explícito é melhor que implícito.\n",
1222 | "3. Simples é melhor que complexo.\n",
1223 | "4. Complexo é melhor que complicado.\n",
1224 | "5. Plano é melhor que aglomerado.\n",
1225 | "6. Esparso é melhor que denso.\n",
1226 | "7. Legibilidade faz diferença.\n",
1227 | "8. Casos especiais não são especiais o bastante para quebrar as regras.\n",
1228 | "9. Embora a praticidade vença a pureza.\n",
1229 | "10. Erros nunca devem passar silenciosamente.\n",
1230 | "11. A menos que sejam explicitamente silenciados.\n",
1231 | "12. Diante da ambigüidade, recuse a tentação de adivinhar.\n",
1232 | "13. Deve haver um -- e preferencialmente só um -- modo óbvio para fazer algo.\n",
1233 | "14. Embora esse modo possa não ser óbvio à primeira vista a menos que você seja holandês.\n",
1234 | "15. Agora é melhor que nunca.\n",
1235 | "16. Embora nunca freqüentemente seja melhor que *exatamente* agora.\n",
1236 | "17. Se a implementação é difícil de explicar, é uma má idéia.\n",
1237 | "18. Se a implementação é fácil de explicar, pode ser uma boa idéia.\n",
1238 | "19. Namespaces são uma grande idéia -- vamos fazer mais dessas! "
1239 | ]
1240 | }
1241 | ]
1242 | }
--------------------------------------------------------------------------------
/Python_para_ML_02.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Python para ML - 02",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyNYVNm/A5hMStX+CIOklGrt",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "6xhb-xXjZQ65",
31 | "colab_type": "text"
32 | },
33 | "source": [
34 | ""
35 | ]
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "metadata": {
40 | "id": "-LOUIeZLZWo1",
41 | "colab_type": "text"
42 | },
43 | "source": [
44 | "# **Python para Machine Learning** - Parte 2"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {
50 | "id": "iyK5-8ivZccx",
51 | "colab_type": "text"
52 | },
53 | "source": [
54 | "Nesse notebook veremos:\n",
55 | "\n",
56 | "- [Importação de Bibliotecas](#impo)\n",
57 | "- [Solução do Exercício 1 da aula anterior](#sol1)\n",
58 | "- [Solução do Exercício 2 da aula anterior](#sol2)\n",
59 | "- [Operadores Lógicos](#oper)\n",
60 | "- [Listas](#lists)\n",
61 | "- [Funções Importantes](#func)\n",
62 | "- [Métodos de Listas](#metl)\n",
63 | "- [Métodos de Strings](#mets)\n",
64 | "- [Iteradores FOR e WHILE](#iter)\n",
65 | "- [Projeto - Jogo da Roleta](#proj)\n",
66 | "- [Exercício](#exer)\n",
67 | "- [Links Úteis](#links)\n",
68 | "\n"
69 | ]
70 | },
71 | {
72 | "cell_type": "markdown",
73 | "metadata": {
74 | "id": "JmcfioJIOVwS",
75 | "colab_type": "text"
76 | },
77 | "source": [
78 | "## **Importação de Bibliotecas** "
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {
84 | "id": "TjviRBTGSkTB",
85 | "colab_type": "text"
86 | },
87 | "source": [
88 | "Existem várias maneiras de se importar funções de uma biblioteca a parte no python. Abaixo está listada as maneiras mais comuns. \n",
89 | "\n",
90 | "Veja que uma biblioteca precisa ser instalada antes de ser importada. No caso, as bibliotecas que usaremos na aula de hoje já estão instaladas no ambiente do Google Colab, por essa razão não é necessária fazer uma instalação prévia. "
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "metadata": {
96 | "id": "3CeuJc7NOaHw",
97 | "colab_type": "code",
98 | "colab": {
99 | "base_uri": "https://localhost:8080/",
100 | "height": 35
101 | },
102 | "outputId": "f0ca019b-e2e2-4927-929b-d9295fc5fc60"
103 | },
104 | "source": [
105 | "from math import pi\n",
106 | "print(pi)"
107 | ],
108 | "execution_count": null,
109 | "outputs": [
110 | {
111 | "output_type": "stream",
112 | "text": [
113 | "3.141592653589793\n"
114 | ],
115 | "name": "stdout"
116 | }
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "metadata": {
122 | "id": "Of5K4ao1OXSD",
123 | "colab_type": "code",
124 | "colab": {
125 | "base_uri": "https://localhost:8080/",
126 | "height": 35
127 | },
128 | "outputId": "75cf4066-39e2-49ab-a65e-43aad9e984e7"
129 | },
130 | "source": [
131 | "import math\n",
132 | "print(math.pi)"
133 | ],
134 | "execution_count": null,
135 | "outputs": [
136 | {
137 | "output_type": "stream",
138 | "text": [
139 | "3.141592653589793\n"
140 | ],
141 | "name": "stdout"
142 | }
143 | ]
144 | },
145 | {
146 | "cell_type": "code",
147 | "metadata": {
148 | "id": "apemaHD9OksT",
149 | "colab_type": "code",
150 | "colab": {
151 | "base_uri": "https://localhost:8080/",
152 | "height": 35
153 | },
154 | "outputId": "68f4b07c-58bc-4e86-cd70-6fe83713141f"
155 | },
156 | "source": [
157 | "import math as m\n",
158 | "print(m.pi)"
159 | ],
160 | "execution_count": null,
161 | "outputs": [
162 | {
163 | "output_type": "stream",
164 | "text": [
165 | "3.141592653589793\n"
166 | ],
167 | "name": "stdout"
168 | }
169 | ]
170 | },
171 | {
172 | "cell_type": "code",
173 | "metadata": {
174 | "id": "NSNYFHzrOmZ7",
175 | "colab_type": "code",
176 | "colab": {
177 | "base_uri": "https://localhost:8080/",
178 | "height": 35
179 | },
180 | "outputId": "dae94277-dcab-4f34-872c-20dbffccc4f5"
181 | },
182 | "source": [
183 | "from math import pi as Piii\n",
184 | "print(Piii)"
185 | ],
186 | "execution_count": null,
187 | "outputs": [
188 | {
189 | "output_type": "stream",
190 | "text": [
191 | "3.141592653589793\n"
192 | ],
193 | "name": "stdout"
194 | }
195 | ]
196 | },
197 | {
198 | "cell_type": "code",
199 | "metadata": {
200 | "id": "mKOPYeo2Ourn",
201 | "colab_type": "code",
202 | "colab": {
203 | "base_uri": "https://localhost:8080/",
204 | "height": 53
205 | },
206 | "outputId": "6ee3e9ec-8136-4c3b-b75f-eead104f09ed"
207 | },
208 | "source": [
209 | "from math import *\n",
210 | "print(pi)\n",
211 | "print(sqrt(5))"
212 | ],
213 | "execution_count": null,
214 | "outputs": [
215 | {
216 | "output_type": "stream",
217 | "text": [
218 | "3.141592653589793\n",
219 | "2.23606797749979\n"
220 | ],
221 | "name": "stdout"
222 | }
223 | ]
224 | },
225 | {
226 | "cell_type": "markdown",
227 | "metadata": {
228 | "id": "O_bzFMF_xX9M",
229 | "colab_type": "text"
230 | },
231 | "source": [
232 | " **Solução do Exercício 1 da aula anterior** Considere um balde cuja base possui raio $r_1$ e altura igual ao diâmetro da base. Considere também uma esfera de raio $r_2$ cheia de água. Faça um programa que verifique se o volume da esfera cabe no balde, dados os valores de $r_1$ e $r_2$. "
233 | ]
234 | },
235 | {
236 | "cell_type": "code",
237 | "metadata": {
238 | "id": "nAl_lSsJxbsf",
239 | "colab_type": "code",
240 | "colab": {}
241 | },
242 | "source": [
243 | "from math import pi\n",
244 | "\n",
245 | "def ChecaVolume(raio_balde, raio_esfera):\n",
246 | " if raio_balde <=0:\n",
247 | " print('Erro: valor do raio do balde inadequado')\n",
248 | "\n",
249 | " elif raio_esfera <=0:\n",
250 | " print('Erro: valor do raio da esfera inadequado')\n",
251 | "\n",
252 | " else:\n",
253 | " vol_balde = 2*raio_balde**3*pi\n",
254 | " vol_esfera = 4/3*pi*raio_esfera**3\n",
255 | "\n",
256 | " if vol_esfera <= vol_balde:\n",
257 | " print('O volume da esfera cabe no balde')\n",
258 | " else:\n",
259 | " print('O volume da esfera não cabe no balde')"
260 | ],
261 | "execution_count": 126,
262 | "outputs": []
263 | },
264 | {
265 | "cell_type": "code",
266 | "metadata": {
267 | "id": "pKf6k2RLzxV-",
268 | "colab_type": "code",
269 | "colab": {
270 | "base_uri": "https://localhost:8080/",
271 | "height": 35
272 | },
273 | "outputId": "ee8a3414-3e78-466c-e656-61e75c1f28cb"
274 | },
275 | "source": [
276 | "ChecaVolume(-1,2)"
277 | ],
278 | "execution_count": 127,
279 | "outputs": [
280 | {
281 | "output_type": "stream",
282 | "text": [
283 | "Erro: valor do raio do balde inadequado\n"
284 | ],
285 | "name": "stdout"
286 | }
287 | ]
288 | },
289 | {
290 | "cell_type": "markdown",
291 | "metadata": {
292 | "id": "FvbqK-JpNXie",
293 | "colab_type": "text"
294 | },
295 | "source": [
296 | "**Solução do Exercício 2 da aula anterior** Crie uma função que simule o jogo do *jokempô*, isto é, dada a entrada de dois jogadores, retorne a indicação de qual deles venceu. "
297 | ]
298 | },
299 | {
300 | "cell_type": "code",
301 | "metadata": {
302 | "id": "i7VtxLEY0K-3",
303 | "colab_type": "code",
304 | "colab": {}
305 | },
306 | "source": [
307 | "def Jokenpo(jogador1, jogador2):\n",
308 | " if jogador1 == 'pedra':\n",
309 | " if jogador2 == 'tesoura':\n",
310 | " print('Jogador 1 ganhou')\n",
311 | " elif jogador2 == 'papel':\n",
312 | " print('Jogador 2 ganhou')\n",
313 | " else:\n",
314 | " print('Empate!')\n",
315 | " \n",
316 | " if jogador1 == 'tesoura':\n",
317 | " if jogador2 == 'papel':\n",
318 | " print('Jogador 1 ganhou')\n",
319 | " elif jogador2 == 'pedra':\n",
320 | " print('Jogador 2 ganhou')\n",
321 | " else:\n",
322 | " print('Empate!')\n",
323 | " \n",
324 | " if jogador1 == 'papel':\n",
325 | " if jogador2 == 'pedra':\n",
326 | " print('Jogador 1 ganhou')\n",
327 | " elif jogador2 == 'tesoura':\n",
328 | " print('Jogador 2 ganhou')\n",
329 | " else:\n",
330 | " print('Empate!')"
331 | ],
332 | "execution_count": 128,
333 | "outputs": []
334 | },
335 | {
336 | "cell_type": "code",
337 | "metadata": {
338 | "id": "NrnHdfr70WWv",
339 | "colab_type": "code",
340 | "colab": {
341 | "base_uri": "https://localhost:8080/",
342 | "height": 35
343 | },
344 | "outputId": "cea349d9-3c25-4358-ff1e-eb3540f108c7"
345 | },
346 | "source": [
347 | "Jokenpo(jogador1 = 'tesoura', jogador2 = 'papel')"
348 | ],
349 | "execution_count": 129,
350 | "outputs": [
351 | {
352 | "output_type": "stream",
353 | "text": [
354 | "Jogador 1 ganhou\n"
355 | ],
356 | "name": "stdout"
357 | }
358 | ]
359 | },
360 | {
361 | "cell_type": "markdown",
362 | "metadata": {
363 | "id": "vdRNZpeu0bBm",
364 | "colab_type": "text"
365 | },
366 | "source": [
367 | "## **Operadores Lógicos** "
368 | ]
369 | },
370 | {
371 | "cell_type": "markdown",
372 | "metadata": {
373 | "id": "LJ1LH_He3gKc",
374 | "colab_type": "text"
375 | },
376 | "source": [
377 | "Alguns operadores lógicos importantes:\n",
378 | "- ``and`` para o conectivo ``e``\n",
379 | "- ``or`` para o conectivo ``ou``\n",
380 | "- ``not`` para a negação de algo"
381 | ]
382 | },
383 | {
384 | "cell_type": "code",
385 | "metadata": {
386 | "id": "piqaX-vE386o",
387 | "colab_type": "code",
388 | "colab": {
389 | "base_uri": "https://localhost:8080/",
390 | "height": 35
391 | },
392 | "outputId": "09332419-9a46-47a4-89dd-02be4ba2e5ea"
393 | },
394 | "source": [
395 | "True and True"
396 | ],
397 | "execution_count": null,
398 | "outputs": [
399 | {
400 | "output_type": "execute_result",
401 | "data": {
402 | "text/plain": [
403 | "True"
404 | ]
405 | },
406 | "metadata": {
407 | "tags": []
408 | },
409 | "execution_count": 19
410 | }
411 | ]
412 | },
413 | {
414 | "cell_type": "code",
415 | "metadata": {
416 | "id": "HbKhL6qz3_25",
417 | "colab_type": "code",
418 | "colab": {
419 | "base_uri": "https://localhost:8080/",
420 | "height": 35
421 | },
422 | "outputId": "b5bfaa1b-1bfd-470f-bf10-86ed3931fcf0"
423 | },
424 | "source": [
425 | "True and False"
426 | ],
427 | "execution_count": null,
428 | "outputs": [
429 | {
430 | "output_type": "execute_result",
431 | "data": {
432 | "text/plain": [
433 | "False"
434 | ]
435 | },
436 | "metadata": {
437 | "tags": []
438 | },
439 | "execution_count": 20
440 | }
441 | ]
442 | },
443 | {
444 | "cell_type": "code",
445 | "metadata": {
446 | "id": "XGJVzhbh4BiK",
447 | "colab_type": "code",
448 | "colab": {
449 | "base_uri": "https://localhost:8080/",
450 | "height": 35
451 | },
452 | "outputId": "4016a9a5-b0bd-4b17-f8e0-0d70d805fcb6"
453 | },
454 | "source": [
455 | "True or True"
456 | ],
457 | "execution_count": null,
458 | "outputs": [
459 | {
460 | "output_type": "execute_result",
461 | "data": {
462 | "text/plain": [
463 | "True"
464 | ]
465 | },
466 | "metadata": {
467 | "tags": []
468 | },
469 | "execution_count": 21
470 | }
471 | ]
472 | },
473 | {
474 | "cell_type": "code",
475 | "metadata": {
476 | "id": "oWhbUZYW4DCY",
477 | "colab_type": "code",
478 | "colab": {
479 | "base_uri": "https://localhost:8080/",
480 | "height": 35
481 | },
482 | "outputId": "16c45f11-dc8a-49d7-a06a-dbec40b2792a"
483 | },
484 | "source": [
485 | "True or False"
486 | ],
487 | "execution_count": null,
488 | "outputs": [
489 | {
490 | "output_type": "execute_result",
491 | "data": {
492 | "text/plain": [
493 | "True"
494 | ]
495 | },
496 | "metadata": {
497 | "tags": []
498 | },
499 | "execution_count": 22
500 | }
501 | ]
502 | },
503 | {
504 | "cell_type": "code",
505 | "metadata": {
506 | "id": "K9iEzD3W4EdH",
507 | "colab_type": "code",
508 | "colab": {
509 | "base_uri": "https://localhost:8080/",
510 | "height": 35
511 | },
512 | "outputId": "140f4c0b-1d2c-41d2-8ce4-c8d8dd6a7f60"
513 | },
514 | "source": [
515 | "not True"
516 | ],
517 | "execution_count": null,
518 | "outputs": [
519 | {
520 | "output_type": "execute_result",
521 | "data": {
522 | "text/plain": [
523 | "False"
524 | ]
525 | },
526 | "metadata": {
527 | "tags": []
528 | },
529 | "execution_count": 23
530 | }
531 | ]
532 | },
533 | {
534 | "cell_type": "code",
535 | "metadata": {
536 | "id": "Dct9Vjh04F1w",
537 | "colab_type": "code",
538 | "colab": {
539 | "base_uri": "https://localhost:8080/",
540 | "height": 35
541 | },
542 | "outputId": "877eb9b8-9a2e-411f-8252-647d43b35d91"
543 | },
544 | "source": [
545 | "any([False, False])"
546 | ],
547 | "execution_count": null,
548 | "outputs": [
549 | {
550 | "output_type": "execute_result",
551 | "data": {
552 | "text/plain": [
553 | "False"
554 | ]
555 | },
556 | "metadata": {
557 | "tags": []
558 | },
559 | "execution_count": 9
560 | }
561 | ]
562 | },
563 | {
564 | "cell_type": "markdown",
565 | "metadata": {
566 | "id": "NX6fojF75D1Q",
567 | "colab_type": "text"
568 | },
569 | "source": [
570 | "Voltando no Exercício 1, podemos reescrever da seguinte maneira:"
571 | ]
572 | },
573 | {
574 | "cell_type": "code",
575 | "metadata": {
576 | "id": "bZr7aQ5g9o_H",
577 | "colab_type": "code",
578 | "colab": {}
579 | },
580 | "source": [
581 | "def ChecaVolume(raio_balde, raio_esfera):\n",
582 | " if raio_balde > 0 and raio_esfera > 0: #OBSERVE O CONECTIVO 'AND' NESSA LINHA\n",
583 | " \n",
584 | " vol_balde = 2*raio_balde**3*pi\n",
585 | " vol_esfera = 4/3*pi*raio_esfera**3\n",
586 | "\n",
587 | " if vol_esfera <= vol_balde:\n",
588 | " print('O volume da esfera cabe no balde')\n",
589 | " else:\n",
590 | " print('O volume da esfera não cabe no balde')\n",
591 | " else:\n",
592 | " print('Valores de entrada inadequados')"
593 | ],
594 | "execution_count": null,
595 | "outputs": []
596 | },
597 | {
598 | "cell_type": "markdown",
599 | "metadata": {
600 | "id": "H-eQY_VJ9voY",
601 | "colab_type": "text"
602 | },
603 | "source": [
604 | "## **Listas** "
605 | ]
606 | },
607 | {
608 | "cell_type": "markdown",
609 | "metadata": {
610 | "id": "bgBgFBTo-8gx",
611 | "colab_type": "text"
612 | },
613 | "source": [
614 | "Um objeto do tipo lista é um dos mais importantes do universo Python. Sua versatilidade permite a construção e organização simples de diversas funcionalidades. **Uma lista é definida por conchetes [ ]**. Vejamos alguns exemplos:"
615 | ]
616 | },
617 | {
618 | "cell_type": "code",
619 | "metadata": {
620 | "id": "oxQQgv2j_TGn",
621 | "colab_type": "code",
622 | "colab": {}
623 | },
624 | "source": [
625 | "minha_lista = [1, 1, 2, 3, 5, 8, 13]"
626 | ],
627 | "execution_count": null,
628 | "outputs": []
629 | },
630 | {
631 | "cell_type": "code",
632 | "metadata": {
633 | "id": "sxt_3Q_h_JuO",
634 | "colab_type": "code",
635 | "colab": {
636 | "base_uri": "https://localhost:8080/",
637 | "height": 35
638 | },
639 | "outputId": "c89f09ba-e51d-4f3a-9cfc-ed8bb460a313"
640 | },
641 | "source": [
642 | "print(minha_lista)"
643 | ],
644 | "execution_count": null,
645 | "outputs": [
646 | {
647 | "output_type": "stream",
648 | "text": [
649 | "[1, 1, 2, 3, 5, 8, 13]\n"
650 | ],
651 | "name": "stdout"
652 | }
653 | ]
654 | },
655 | {
656 | "cell_type": "code",
657 | "metadata": {
658 | "id": "twfQLsSOBFUi",
659 | "colab_type": "code",
660 | "colab": {
661 | "base_uri": "https://localhost:8080/",
662 | "height": 35
663 | },
664 | "outputId": "e50d25e7-ea5e-47e8-e55c-8f5ba83e90ff"
665 | },
666 | "source": [
667 | "len(minha_lista)"
668 | ],
669 | "execution_count": null,
670 | "outputs": [
671 | {
672 | "output_type": "execute_result",
673 | "data": {
674 | "text/plain": [
675 | "7"
676 | ]
677 | },
678 | "metadata": {
679 | "tags": []
680 | },
681 | "execution_count": 57
682 | }
683 | ]
684 | },
685 | {
686 | "cell_type": "markdown",
687 | "metadata": {
688 | "id": "2s66TOcfAhh5",
689 | "colab_type": "text"
690 | },
691 | "source": [
692 | "**Importante**: A indexação no Python, assim como em diversas linguagens de programação, começam com índice 0\n",
693 | "\n",
694 | "Isso quer dizer que o primeiro elemento está na entrada 0 da lista, o segundo elemento na entrada 1, e assim por diante.\n",
695 | "\n",
696 | "Nós acessamos os elementos de uma lista também utilizando **colchetes**"
697 | ]
698 | },
699 | {
700 | "cell_type": "code",
701 | "metadata": {
702 | "id": "IFrs9gJt_hoo",
703 | "colab_type": "code",
704 | "colab": {
705 | "base_uri": "https://localhost:8080/",
706 | "height": 35
707 | },
708 | "outputId": "58bf7836-e73a-4246-cb5b-a943a10b1164"
709 | },
710 | "source": [
711 | "minha_lista[0]"
712 | ],
713 | "execution_count": null,
714 | "outputs": [
715 | {
716 | "output_type": "execute_result",
717 | "data": {
718 | "text/plain": [
719 | "1"
720 | ]
721 | },
722 | "metadata": {
723 | "tags": []
724 | },
725 | "execution_count": 52
726 | }
727 | ]
728 | },
729 | {
730 | "cell_type": "code",
731 | "metadata": {
732 | "id": "LLz1CsHF_rQx",
733 | "colab_type": "code",
734 | "colab": {
735 | "base_uri": "https://localhost:8080/",
736 | "height": 35
737 | },
738 | "outputId": "c79c68fc-fd1b-40b9-beba-ff47b276b9f4"
739 | },
740 | "source": [
741 | "minha_lista[len(minha_lista) - 1]"
742 | ],
743 | "execution_count": null,
744 | "outputs": [
745 | {
746 | "output_type": "execute_result",
747 | "data": {
748 | "text/plain": [
749 | "13"
750 | ]
751 | },
752 | "metadata": {
753 | "tags": []
754 | },
755 | "execution_count": 55
756 | }
757 | ]
758 | },
759 | {
760 | "cell_type": "markdown",
761 | "metadata": {
762 | "id": "rCxiEy34_95J",
763 | "colab_type": "text"
764 | },
765 | "source": [
766 | "Uma lista permite armazenar todos os tipos de objetos do Python, veja no exemplo abaixo:"
767 | ]
768 | },
769 | {
770 | "cell_type": "code",
771 | "metadata": {
772 | "id": "mPMB_H5-BSHQ",
773 | "colab_type": "code",
774 | "colab": {
775 | "base_uri": "https://localhost:8080/",
776 | "height": 35
777 | },
778 | "outputId": "f7da50e4-8846-4ab9-a3df-20c73313b983"
779 | },
780 | "source": [
781 | "lista = ['Python', 1996, True, len, [1,2,'Ricardo']]\n",
782 | "print(lista)"
783 | ],
784 | "execution_count": null,
785 | "outputs": [
786 | {
787 | "output_type": "stream",
788 | "text": [
789 | "['Python', 1996, True, , [1, 2, 'Ricardo']]\n"
790 | ],
791 | "name": "stdout"
792 | }
793 | ]
794 | },
795 | {
796 | "cell_type": "code",
797 | "metadata": {
798 | "id": "ub4FXtitBZW4",
799 | "colab_type": "code",
800 | "colab": {
801 | "base_uri": "https://localhost:8080/",
802 | "height": 35
803 | },
804 | "outputId": "623eb9e9-d81d-4ae2-8ea3-3869cac9aae9"
805 | },
806 | "source": [
807 | "lista[0] + ' é muito massa' "
808 | ],
809 | "execution_count": null,
810 | "outputs": [
811 | {
812 | "output_type": "execute_result",
813 | "data": {
814 | "text/plain": [
815 | "'Python é muito massa'"
816 | ]
817 | },
818 | "metadata": {
819 | "tags": []
820 | },
821 | "execution_count": 70
822 | }
823 | ]
824 | },
825 | {
826 | "cell_type": "code",
827 | "metadata": {
828 | "id": "6vQfJXe_BckY",
829 | "colab_type": "code",
830 | "colab": {
831 | "base_uri": "https://localhost:8080/",
832 | "height": 35
833 | },
834 | "outputId": "97d9e804-836a-4ba3-e00c-b24b99edbd22"
835 | },
836 | "source": [
837 | "lista[1] + 1337"
838 | ],
839 | "execution_count": null,
840 | "outputs": [
841 | {
842 | "output_type": "execute_result",
843 | "data": {
844 | "text/plain": [
845 | "3333"
846 | ]
847 | },
848 | "metadata": {
849 | "tags": []
850 | },
851 | "execution_count": 71
852 | }
853 | ]
854 | },
855 | {
856 | "cell_type": "code",
857 | "metadata": {
858 | "id": "_p8LnFNoBy_Z",
859 | "colab_type": "code",
860 | "colab": {
861 | "base_uri": "https://localhost:8080/",
862 | "height": 35
863 | },
864 | "outputId": "c6f681c0-d2c6-4e56-bd37-92e70234d1a9"
865 | },
866 | "source": [
867 | "lista[2] and True"
868 | ],
869 | "execution_count": null,
870 | "outputs": [
871 | {
872 | "output_type": "execute_result",
873 | "data": {
874 | "text/plain": [
875 | "True"
876 | ]
877 | },
878 | "metadata": {
879 | "tags": []
880 | },
881 | "execution_count": 72
882 | }
883 | ]
884 | },
885 | {
886 | "cell_type": "code",
887 | "metadata": {
888 | "id": "xYbglI3QB49K",
889 | "colab_type": "code",
890 | "colab": {
891 | "base_uri": "https://localhost:8080/",
892 | "height": 35
893 | },
894 | "outputId": "ce4b9179-6cb4-4fbb-f7c2-8c82656c67bb"
895 | },
896 | "source": [
897 | "lista[3](lista)"
898 | ],
899 | "execution_count": null,
900 | "outputs": [
901 | {
902 | "output_type": "execute_result",
903 | "data": {
904 | "text/plain": [
905 | "4"
906 | ]
907 | },
908 | "metadata": {
909 | "tags": []
910 | },
911 | "execution_count": 73
912 | }
913 | ]
914 | },
915 | {
916 | "cell_type": "markdown",
917 | "metadata": {
918 | "id": "iYaycgzoB8RQ",
919 | "colab_type": "text"
920 | },
921 | "source": [
922 | "Podemos acessar caracteres de listas através de colchetes também. No python, strings são como listas de caracteres:"
923 | ]
924 | },
925 | {
926 | "cell_type": "code",
927 | "metadata": {
928 | "id": "Tc7JYUWuCHbo",
929 | "colab_type": "code",
930 | "colab": {
931 | "base_uri": "https://localhost:8080/",
932 | "height": 35
933 | },
934 | "outputId": "79f88f58-8338-4873-cbed-99209f1e585d"
935 | },
936 | "source": [
937 | "'Python'[0]"
938 | ],
939 | "execution_count": null,
940 | "outputs": [
941 | {
942 | "output_type": "execute_result",
943 | "data": {
944 | "text/plain": [
945 | "'P'"
946 | ]
947 | },
948 | "metadata": {
949 | "tags": []
950 | },
951 | "execution_count": 74
952 | }
953 | ]
954 | },
955 | {
956 | "cell_type": "code",
957 | "metadata": {
958 | "id": "_P_pkjYZCHfG",
959 | "colab_type": "code",
960 | "colab": {
961 | "base_uri": "https://localhost:8080/",
962 | "height": 35
963 | },
964 | "outputId": "b57638e3-2f40-48a1-dd87-a16579e31ca9"
965 | },
966 | "source": [
967 | "py = 'Python'\n",
968 | "py[0] + py[5]"
969 | ],
970 | "execution_count": null,
971 | "outputs": [
972 | {
973 | "output_type": "execute_result",
974 | "data": {
975 | "text/plain": [
976 | "'Pn'"
977 | ]
978 | },
979 | "metadata": {
980 | "tags": []
981 | },
982 | "execution_count": 77
983 | }
984 | ]
985 | },
986 | {
987 | "cell_type": "markdown",
988 | "metadata": {
989 | "id": "DK3LkMgqCOrR",
990 | "colab_type": "text"
991 | },
992 | "source": [
993 | "E podemos acessar listas dentro de listas"
994 | ]
995 | },
996 | {
997 | "cell_type": "code",
998 | "metadata": {
999 | "id": "Ce3loc-iCwp_",
1000 | "colab_type": "code",
1001 | "colab": {
1002 | "base_uri": "https://localhost:8080/",
1003 | "height": 35
1004 | },
1005 | "outputId": "6dd17226-c3c2-4962-ca3f-f2f83ccddd29"
1006 | },
1007 | "source": [
1008 | "lista[4]"
1009 | ],
1010 | "execution_count": null,
1011 | "outputs": [
1012 | {
1013 | "output_type": "execute_result",
1014 | "data": {
1015 | "text/plain": [
1016 | "[1, 2, 3]"
1017 | ]
1018 | },
1019 | "metadata": {
1020 | "tags": []
1021 | },
1022 | "execution_count": 86
1023 | }
1024 | ]
1025 | },
1026 | {
1027 | "cell_type": "code",
1028 | "metadata": {
1029 | "id": "01UJ8UcBC2bB",
1030 | "colab_type": "code",
1031 | "colab": {
1032 | "base_uri": "https://localhost:8080/",
1033 | "height": 35
1034 | },
1035 | "outputId": "71193235-ba48-450d-bc07-a51a3377455d"
1036 | },
1037 | "source": [
1038 | "lista[4][2]"
1039 | ],
1040 | "execution_count": null,
1041 | "outputs": [
1042 | {
1043 | "output_type": "execute_result",
1044 | "data": {
1045 | "text/plain": [
1046 | "'Ricardo'"
1047 | ]
1048 | },
1049 | "metadata": {
1050 | "tags": []
1051 | },
1052 | "execution_count": 93
1053 | }
1054 | ]
1055 | },
1056 | {
1057 | "cell_type": "code",
1058 | "metadata": {
1059 | "id": "aFacnDGgDApB",
1060 | "colab_type": "code",
1061 | "colab": {
1062 | "base_uri": "https://localhost:8080/",
1063 | "height": 35
1064 | },
1065 | "outputId": "9c56f0a4-22d6-4cd9-959e-f43961306413"
1066 | },
1067 | "source": [
1068 | "lista[4][2][0]"
1069 | ],
1070 | "execution_count": null,
1071 | "outputs": [
1072 | {
1073 | "output_type": "execute_result",
1074 | "data": {
1075 | "text/plain": [
1076 | "'R'"
1077 | ]
1078 | },
1079 | "metadata": {
1080 | "tags": []
1081 | },
1082 | "execution_count": 94
1083 | }
1084 | ]
1085 | },
1086 | {
1087 | "cell_type": "code",
1088 | "metadata": {
1089 | "id": "U1dMoG4YDNWr",
1090 | "colab_type": "code",
1091 | "colab": {}
1092 | },
1093 | "source": [
1094 | ""
1095 | ],
1096 | "execution_count": null,
1097 | "outputs": []
1098 | },
1099 | {
1100 | "cell_type": "code",
1101 | "metadata": {
1102 | "id": "L8PlzPF1Cx-w",
1103 | "colab_type": "code",
1104 | "colab": {
1105 | "base_uri": "https://localhost:8080/",
1106 | "height": 35
1107 | },
1108 | "outputId": "96036126-5eee-48aa-99fb-07cad14b0d69"
1109 | },
1110 | "source": [
1111 | "lista[0]"
1112 | ],
1113 | "execution_count": null,
1114 | "outputs": [
1115 | {
1116 | "output_type": "execute_result",
1117 | "data": {
1118 | "text/plain": [
1119 | "'Python'"
1120 | ]
1121 | },
1122 | "metadata": {
1123 | "tags": []
1124 | },
1125 | "execution_count": 88
1126 | }
1127 | ]
1128 | },
1129 | {
1130 | "cell_type": "code",
1131 | "metadata": {
1132 | "id": "7jAN0-DFC42g",
1133 | "colab_type": "code",
1134 | "colab": {
1135 | "base_uri": "https://localhost:8080/",
1136 | "height": 35
1137 | },
1138 | "outputId": "c992f6df-4438-4cb2-b046-0ca0276ed930"
1139 | },
1140 | "source": [
1141 | "lista[0][0]"
1142 | ],
1143 | "execution_count": null,
1144 | "outputs": [
1145 | {
1146 | "output_type": "execute_result",
1147 | "data": {
1148 | "text/plain": [
1149 | "'P'"
1150 | ]
1151 | },
1152 | "metadata": {
1153 | "tags": []
1154 | },
1155 | "execution_count": 89
1156 | }
1157 | ]
1158 | },
1159 | {
1160 | "cell_type": "markdown",
1161 | "metadata": {
1162 | "id": "zMa7ivrxC7EL",
1163 | "colab_type": "text"
1164 | },
1165 | "source": [
1166 | "Alguns comandos úteis para acessar elementos em listas:"
1167 | ]
1168 | },
1169 | {
1170 | "cell_type": "code",
1171 | "metadata": {
1172 | "id": "y7EWm9MaDd7R",
1173 | "colab_type": "code",
1174 | "colab": {}
1175 | },
1176 | "source": [
1177 | "frase = 'Python é excelente para análise de dados'"
1178 | ],
1179 | "execution_count": null,
1180 | "outputs": []
1181 | },
1182 | {
1183 | "cell_type": "code",
1184 | "metadata": {
1185 | "id": "0GFA9PY1Dloy",
1186 | "colab_type": "code",
1187 | "colab": {
1188 | "base_uri": "https://localhost:8080/",
1189 | "height": 35
1190 | },
1191 | "outputId": "e4ec071c-d743-4b13-e47c-0587dfd8acf8"
1192 | },
1193 | "source": [
1194 | "frase[:6] #retorna todos os elementos até o indice 6 da lista (o indice 6 (sétimo elemento) não está incluido)"
1195 | ],
1196 | "execution_count": null,
1197 | "outputs": [
1198 | {
1199 | "output_type": "execute_result",
1200 | "data": {
1201 | "text/plain": [
1202 | "'Python'"
1203 | ]
1204 | },
1205 | "metadata": {
1206 | "tags": []
1207 | },
1208 | "execution_count": 100
1209 | }
1210 | ]
1211 | },
1212 | {
1213 | "cell_type": "code",
1214 | "metadata": {
1215 | "id": "D5aR7aeGD1gT",
1216 | "colab_type": "code",
1217 | "colab": {
1218 | "base_uri": "https://localhost:8080/",
1219 | "height": 35
1220 | },
1221 | "outputId": "cd62322d-8733-4cfd-aa28-78a6fb41ccf1"
1222 | },
1223 | "source": [
1224 | "frase[6:] #retorna todos os elementos após o indice 6 da lista"
1225 | ],
1226 | "execution_count": null,
1227 | "outputs": [
1228 | {
1229 | "output_type": "execute_result",
1230 | "data": {
1231 | "text/plain": [
1232 | "' é excelente para análise de dados'"
1233 | ]
1234 | },
1235 | "metadata": {
1236 | "tags": []
1237 | },
1238 | "execution_count": 101
1239 | }
1240 | ]
1241 | },
1242 | {
1243 | "cell_type": "markdown",
1244 | "metadata": {
1245 | "id": "cpzQ0wxyESUY",
1246 | "colab_type": "text"
1247 | },
1248 | "source": [
1249 | "No Python, o padrão é trabalhar com **intervalos semi-abertos [ , )**, como no exemplo abaixo. O índice 9 está incluso na seleção, mas não o 18. "
1250 | ]
1251 | },
1252 | {
1253 | "cell_type": "code",
1254 | "metadata": {
1255 | "id": "8g1SouLeD5hJ",
1256 | "colab_type": "code",
1257 | "colab": {
1258 | "base_uri": "https://localhost:8080/",
1259 | "height": 35
1260 | },
1261 | "outputId": "b54e34ec-95da-4bb5-f53a-183614f26f45"
1262 | },
1263 | "source": [
1264 | "frase[9:18] # retorna o elemento de indice 9 até o 18, mas não inclui o 18"
1265 | ],
1266 | "execution_count": null,
1267 | "outputs": [
1268 | {
1269 | "output_type": "execute_result",
1270 | "data": {
1271 | "text/plain": [
1272 | "'excelente'"
1273 | ]
1274 | },
1275 | "metadata": {
1276 | "tags": []
1277 | },
1278 | "execution_count": 105
1279 | }
1280 | ]
1281 | },
1282 | {
1283 | "cell_type": "markdown",
1284 | "metadata": {
1285 | "id": "-PPDP_x3Elvp",
1286 | "colab_type": "text"
1287 | },
1288 | "source": [
1289 | "Para inverter a ordem de uma lista, utilize"
1290 | ]
1291 | },
1292 | {
1293 | "cell_type": "code",
1294 | "metadata": {
1295 | "id": "R6Q_Pg2iEl1B",
1296 | "colab_type": "code",
1297 | "colab": {
1298 | "base_uri": "https://localhost:8080/",
1299 | "height": 35
1300 | },
1301 | "outputId": "1b534e11-ab52-4d4d-f564-3febd93ba90e"
1302 | },
1303 | "source": [
1304 | "frase[::-1]"
1305 | ],
1306 | "execution_count": null,
1307 | "outputs": [
1308 | {
1309 | "output_type": "execute_result",
1310 | "data": {
1311 | "text/plain": [
1312 | "'sodad ed esilána arap etnelecxe é nohtyP'"
1313 | ]
1314 | },
1315 | "metadata": {
1316 | "tags": []
1317 | },
1318 | "execution_count": 110
1319 | }
1320 | ]
1321 | },
1322 | {
1323 | "cell_type": "markdown",
1324 | "metadata": {
1325 | "id": "BRWDbZLXFBy4",
1326 | "colab_type": "text"
1327 | },
1328 | "source": [
1329 | "Para tomar elementos de 2 em 2, utilize"
1330 | ]
1331 | },
1332 | {
1333 | "cell_type": "code",
1334 | "metadata": {
1335 | "id": "a4VPNkkEFHSY",
1336 | "colab_type": "code",
1337 | "colab": {
1338 | "base_uri": "https://localhost:8080/",
1339 | "height": 35
1340 | },
1341 | "outputId": "2658caa8-a85b-4940-d364-4cb6800e6e30"
1342 | },
1343 | "source": [
1344 | "frase[::2]"
1345 | ],
1346 | "execution_count": null,
1347 | "outputs": [
1348 | {
1349 | "output_type": "execute_result",
1350 | "data": {
1351 | "text/plain": [
1352 | "'Pto xeet aaaáied ao'"
1353 | ]
1354 | },
1355 | "metadata": {
1356 | "tags": []
1357 | },
1358 | "execution_count": 111
1359 | }
1360 | ]
1361 | },
1362 | {
1363 | "cell_type": "markdown",
1364 | "metadata": {
1365 | "id": "zgfaeacMElyi",
1366 | "colab_type": "text"
1367 | },
1368 | "source": [
1369 | "Combine os comandos da maneira que lhe for conveniente"
1370 | ]
1371 | },
1372 | {
1373 | "cell_type": "code",
1374 | "metadata": {
1375 | "id": "yXNjLt4AEiyo",
1376 | "colab_type": "code",
1377 | "colab": {
1378 | "base_uri": "https://localhost:8080/",
1379 | "height": 35
1380 | },
1381 | "outputId": "be78d32b-f44f-4811-d27b-72a0872fe5e9"
1382 | },
1383 | "source": [
1384 | "frase[::-1][::3]"
1385 | ],
1386 | "execution_count": null,
1387 | "outputs": [
1388 | {
1389 | "output_type": "execute_result",
1390 | "data": {
1391 | "text/plain": [
1392 | "'saeelar nee hP'"
1393 | ]
1394 | },
1395 | "metadata": {
1396 | "tags": []
1397 | },
1398 | "execution_count": 113
1399 | }
1400 | ]
1401 | },
1402 | {
1403 | "cell_type": "markdown",
1404 | "metadata": {
1405 | "id": "Rgc2tLUFwYCs",
1406 | "colab_type": "text"
1407 | },
1408 | "source": [
1409 | "## **Funções Importantes** "
1410 | ]
1411 | },
1412 | {
1413 | "cell_type": "markdown",
1414 | "metadata": {
1415 | "id": "Usm30s_ZUPHX",
1416 | "colab_type": "text"
1417 | },
1418 | "source": [
1419 | "O comando **input** serve para se comunicar com o usuário enquanto se executa um programa. Veja o exemplo abaixo"
1420 | ]
1421 | },
1422 | {
1423 | "cell_type": "code",
1424 | "metadata": {
1425 | "id": "xiBIg2DvUWCN",
1426 | "colab_type": "code",
1427 | "colab": {
1428 | "base_uri": "https://localhost:8080/",
1429 | "height": 35
1430 | },
1431 | "outputId": "141f15fe-481b-4f69-bc1f-853dc318ceb4"
1432 | },
1433 | "source": [
1434 | "x = input('Digite o seu nome: ')"
1435 | ],
1436 | "execution_count": 131,
1437 | "outputs": [
1438 | {
1439 | "output_type": "stream",
1440 | "text": [
1441 | "Digite o seu nome: Ricardo\n"
1442 | ],
1443 | "name": "stdout"
1444 | }
1445 | ]
1446 | },
1447 | {
1448 | "cell_type": "code",
1449 | "metadata": {
1450 | "id": "Y8Hlz87UUb1n",
1451 | "colab_type": "code",
1452 | "colab": {
1453 | "base_uri": "https://localhost:8080/",
1454 | "height": 35
1455 | },
1456 | "outputId": "279a1055-7066-43ad-a474-9b1b7d84a45a"
1457 | },
1458 | "source": [
1459 | "print(x)"
1460 | ],
1461 | "execution_count": 132,
1462 | "outputs": [
1463 | {
1464 | "output_type": "stream",
1465 | "text": [
1466 | "Ricardo\n"
1467 | ],
1468 | "name": "stdout"
1469 | }
1470 | ]
1471 | },
1472 | {
1473 | "cell_type": "code",
1474 | "metadata": {
1475 | "id": "vW4v9HdiUe6B",
1476 | "colab_type": "code",
1477 | "colab": {
1478 | "base_uri": "https://localhost:8080/",
1479 | "height": 72
1480 | },
1481 | "outputId": "08843126-1de2-4d5c-fd95-407c486129c1"
1482 | },
1483 | "source": [
1484 | "y = input('Digite a sua idade: ')\n",
1485 | "print('\\nA idade declarada foi {}'.format(y))"
1486 | ],
1487 | "execution_count": 135,
1488 | "outputs": [
1489 | {
1490 | "output_type": "stream",
1491 | "text": [
1492 | "Digite a sua idade: 33\n",
1493 | "\n",
1494 | "A idade declarada foi 33\n"
1495 | ],
1496 | "name": "stdout"
1497 | }
1498 | ]
1499 | },
1500 | {
1501 | "cell_type": "markdown",
1502 | "metadata": {
1503 | "id": "hIAv3lqpU1CE",
1504 | "colab_type": "text"
1505 | },
1506 | "source": [
1507 | "Mas note que os inputs são sempre no format string. Se quisermos usá-lo numericamente, temos que converter as entradas adequadamanente. Em geral usamos a função **int** para inteiros ou **float** para números reais. Você pode usar a função float para números que são inteiros também."
1508 | ]
1509 | },
1510 | {
1511 | "cell_type": "code",
1512 | "metadata": {
1513 | "id": "0ZU8B0hwVQoK",
1514 | "colab_type": "code",
1515 | "colab": {
1516 | "base_uri": "https://localhost:8080/",
1517 | "height": 72
1518 | },
1519 | "outputId": "483b7552-e9ee-47ad-fe82-e3a8df31f26c"
1520 | },
1521 | "source": [
1522 | "y = int(input('Digite a sua idade '))\n",
1523 | "\n",
1524 | "print('\\nSe você já fez aniversário esse ano, seu ano de nascimento foi {}.'.format(2020 - y))"
1525 | ],
1526 | "execution_count": null,
1527 | "outputs": [
1528 | {
1529 | "output_type": "stream",
1530 | "text": [
1531 | "Digite a sua idade 33\n",
1532 | "\n",
1533 | "Se você já fez aniversário esse ano, seu ano de nascimento foi 1987.\n"
1534 | ],
1535 | "name": "stdout"
1536 | }
1537 | ]
1538 | },
1539 | {
1540 | "cell_type": "markdown",
1541 | "metadata": {
1542 | "id": "KJ82dttbW44T",
1543 | "colab_type": "text"
1544 | },
1545 | "source": [
1546 | "\n",
1547 | "\n",
1548 | "---\n",
1549 | "\n"
1550 | ]
1551 | },
1552 | {
1553 | "cell_type": "markdown",
1554 | "metadata": {
1555 | "id": "5ZBxaKUswYSg",
1556 | "colab_type": "text"
1557 | },
1558 | "source": [
1559 | "A função **range** cria um objeto capaz de gerar listas de valores entre dois números. Ela não gera a lista em si, mas um gerador de lista pra quando ela for utilizada. **Ela trabalha com intervalos semi-abertos do tipo [ , )**. Veja os exemplos."
1560 | ]
1561 | },
1562 | {
1563 | "cell_type": "code",
1564 | "metadata": {
1565 | "id": "f4N5FABWW-6P",
1566 | "colab_type": "code",
1567 | "colab": {
1568 | "base_uri": "https://localhost:8080/",
1569 | "height": 35
1570 | },
1571 | "outputId": "5432e8ed-36f1-48bb-fbc4-23bacd63b59f"
1572 | },
1573 | "source": [
1574 | "range(4)"
1575 | ],
1576 | "execution_count": null,
1577 | "outputs": [
1578 | {
1579 | "output_type": "execute_result",
1580 | "data": {
1581 | "text/plain": [
1582 | "range(0, 4)"
1583 | ]
1584 | },
1585 | "metadata": {
1586 | "tags": []
1587 | },
1588 | "execution_count": 17
1589 | }
1590 | ]
1591 | },
1592 | {
1593 | "cell_type": "markdown",
1594 | "metadata": {
1595 | "id": "umj3TALlXBJG",
1596 | "colab_type": "text"
1597 | },
1598 | "source": [
1599 | "Para listar os valores da função range, utilize o comando **list**"
1600 | ]
1601 | },
1602 | {
1603 | "cell_type": "code",
1604 | "metadata": {
1605 | "id": "FzLHaZmOXLIm",
1606 | "colab_type": "code",
1607 | "colab": {
1608 | "base_uri": "https://localhost:8080/",
1609 | "height": 35
1610 | },
1611 | "outputId": "4933f225-dfab-48ed-e2ff-2c7dbb8ba6dd"
1612 | },
1613 | "source": [
1614 | "list(range(4))"
1615 | ],
1616 | "execution_count": null,
1617 | "outputs": [
1618 | {
1619 | "output_type": "execute_result",
1620 | "data": {
1621 | "text/plain": [
1622 | "[0, 1, 2, 3]"
1623 | ]
1624 | },
1625 | "metadata": {
1626 | "tags": []
1627 | },
1628 | "execution_count": 18
1629 | }
1630 | ]
1631 | },
1632 | {
1633 | "cell_type": "markdown",
1634 | "metadata": {
1635 | "id": "SK-6cuaIXJbU",
1636 | "colab_type": "text"
1637 | },
1638 | "source": [
1639 | "Use-a com dois parâmetros para definir o começo e o fim da lista. Note que o primeiro valor é incluido, mas o último não, pois o intervalo dela é semi-aberto"
1640 | ]
1641 | },
1642 | {
1643 | "cell_type": "code",
1644 | "metadata": {
1645 | "id": "fbgK2M_CXZSp",
1646 | "colab_type": "code",
1647 | "colab": {
1648 | "base_uri": "https://localhost:8080/",
1649 | "height": 35
1650 | },
1651 | "outputId": "d08b8e19-0de4-4de4-e580-fbf441da1ff6"
1652 | },
1653 | "source": [
1654 | "list(range(3, 10))"
1655 | ],
1656 | "execution_count": null,
1657 | "outputs": [
1658 | {
1659 | "output_type": "execute_result",
1660 | "data": {
1661 | "text/plain": [
1662 | "[3, 4, 5, 6, 7, 8, 9]"
1663 | ]
1664 | },
1665 | "metadata": {
1666 | "tags": []
1667 | },
1668 | "execution_count": 20
1669 | }
1670 | ]
1671 | },
1672 | {
1673 | "cell_type": "markdown",
1674 | "metadata": {
1675 | "id": "R5VMnOFlXfBB",
1676 | "colab_type": "text"
1677 | },
1678 | "source": [
1679 | "Você ainda pode usar um terceiro parâmetro, que é o passo da lista. Ele deve ser inteiro e representa o salto de um número para o outro."
1680 | ]
1681 | },
1682 | {
1683 | "cell_type": "code",
1684 | "metadata": {
1685 | "id": "YXI4nmWTXnYP",
1686 | "colab_type": "code",
1687 | "colab": {
1688 | "base_uri": "https://localhost:8080/",
1689 | "height": 35
1690 | },
1691 | "outputId": "68a0934a-6bd0-44c4-ed23-e8964ff64cc5"
1692 | },
1693 | "source": [
1694 | "list(range(2, 21, 2))"
1695 | ],
1696 | "execution_count": null,
1697 | "outputs": [
1698 | {
1699 | "output_type": "execute_result",
1700 | "data": {
1701 | "text/plain": [
1702 | "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]"
1703 | ]
1704 | },
1705 | "metadata": {
1706 | "tags": []
1707 | },
1708 | "execution_count": 23
1709 | }
1710 | ]
1711 | },
1712 | {
1713 | "cell_type": "code",
1714 | "metadata": {
1715 | "id": "vmqGMlkRX5fA",
1716 | "colab_type": "code",
1717 | "colab": {
1718 | "base_uri": "https://localhost:8080/",
1719 | "height": 35
1720 | },
1721 | "outputId": "0e3ac1c1-198e-4169-829b-bd6ea4d03d68"
1722 | },
1723 | "source": [
1724 | "list(range(10, 101, 10))"
1725 | ],
1726 | "execution_count": null,
1727 | "outputs": [
1728 | {
1729 | "output_type": "execute_result",
1730 | "data": {
1731 | "text/plain": [
1732 | "[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]"
1733 | ]
1734 | },
1735 | "metadata": {
1736 | "tags": []
1737 | },
1738 | "execution_count": 24
1739 | }
1740 | ]
1741 | },
1742 | {
1743 | "cell_type": "markdown",
1744 | "metadata": {
1745 | "id": "kgAeaNFnaz6Z",
1746 | "colab_type": "text"
1747 | },
1748 | "source": [
1749 | "A função **list** também pode ser utilizada para quebrar uma string em caracteres dentro de uma lista, veja:"
1750 | ]
1751 | },
1752 | {
1753 | "cell_type": "code",
1754 | "metadata": {
1755 | "id": "EsH_I9yya7vQ",
1756 | "colab_type": "code",
1757 | "colab": {
1758 | "base_uri": "https://localhost:8080/",
1759 | "height": 35
1760 | },
1761 | "outputId": "5921f70b-b120-419f-9d5e-30c97edd56a9"
1762 | },
1763 | "source": [
1764 | "list('Python')"
1765 | ],
1766 | "execution_count": null,
1767 | "outputs": [
1768 | {
1769 | "output_type": "execute_result",
1770 | "data": {
1771 | "text/plain": [
1772 | "['P', 'y', 't', 'h', 'o', 'n']"
1773 | ]
1774 | },
1775 | "metadata": {
1776 | "tags": []
1777 | },
1778 | "execution_count": 37
1779 | }
1780 | ]
1781 | },
1782 | {
1783 | "cell_type": "markdown",
1784 | "metadata": {
1785 | "id": "gR0E1z2WEZtJ",
1786 | "colab_type": "text"
1787 | },
1788 | "source": [
1789 | "## **Métodos de Listas** "
1790 | ]
1791 | },
1792 | {
1793 | "cell_type": "markdown",
1794 | "metadata": {
1795 | "id": "OAxHMtUlFoXw",
1796 | "colab_type": "text"
1797 | },
1798 | "source": [
1799 | "**Métodos são como funções que são aplicáveis a um certo tipo de objetos.** Nesse caso, veremos os métodos para os objetos do tipo lista. Métodos são inerentes ao Python pelo contexto de programação orientada a objetos, que veremos com mais detalhes adiante nesse curso"
1800 | ]
1801 | },
1802 | {
1803 | "cell_type": "code",
1804 | "metadata": {
1805 | "id": "u8c0s9IBFjxI",
1806 | "colab_type": "code",
1807 | "colab": {}
1808 | },
1809 | "source": [
1810 | "lista = ['A', 'B']"
1811 | ],
1812 | "execution_count": 144,
1813 | "outputs": []
1814 | },
1815 | {
1816 | "cell_type": "code",
1817 | "metadata": {
1818 | "id": "40Th9p6NGWCH",
1819 | "colab_type": "code",
1820 | "colab": {
1821 | "base_uri": "https://localhost:8080/",
1822 | "height": 172
1823 | },
1824 | "outputId": "b5c6ec50-10cb-4fdf-9fa1-95c4261b3bb6"
1825 | },
1826 | "source": [
1827 | "lista[2] = 'C' # exemplo de que não funciona"
1828 | ],
1829 | "execution_count": 137,
1830 | "outputs": [
1831 | {
1832 | "output_type": "error",
1833 | "ename": "IndexError",
1834 | "evalue": "ignored",
1835 | "traceback": [
1836 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1837 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
1838 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlista\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'C'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1839 | "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
1840 | ]
1841 | }
1842 | ]
1843 | },
1844 | {
1845 | "cell_type": "code",
1846 | "metadata": {
1847 | "id": "bc-jGonLEZx0",
1848 | "colab_type": "code",
1849 | "colab": {
1850 | "base_uri": "https://localhost:8080/",
1851 | "height": 35
1852 | },
1853 | "outputId": "4a2743c8-68de-4d30-eb41-43499b291c8d"
1854 | },
1855 | "source": [
1856 | "lista.append('C')\n",
1857 | "lista\n",
1858 | " "
1859 | ],
1860 | "execution_count": 145,
1861 | "outputs": [
1862 | {
1863 | "output_type": "execute_result",
1864 | "data": {
1865 | "text/plain": [
1866 | "['A', 'B', 'C']"
1867 | ]
1868 | },
1869 | "metadata": {
1870 | "tags": []
1871 | },
1872 | "execution_count": 145
1873 | }
1874 | ]
1875 | },
1876 | {
1877 | "cell_type": "code",
1878 | "metadata": {
1879 | "id": "1oq_OgH8EDKg",
1880 | "colab_type": "code",
1881 | "colab": {
1882 | "base_uri": "https://localhost:8080/",
1883 | "height": 35
1884 | },
1885 | "outputId": "3dbb5313-90a2-4317-f971-4e90e2c87169"
1886 | },
1887 | "source": [
1888 | "lista.pop()"
1889 | ],
1890 | "execution_count": 146,
1891 | "outputs": [
1892 | {
1893 | "output_type": "execute_result",
1894 | "data": {
1895 | "text/plain": [
1896 | "'C'"
1897 | ]
1898 | },
1899 | "metadata": {
1900 | "tags": []
1901 | },
1902 | "execution_count": 146
1903 | }
1904 | ]
1905 | },
1906 | {
1907 | "cell_type": "code",
1908 | "metadata": {
1909 | "id": "7g6ztmG-Dnhg",
1910 | "colab_type": "code",
1911 | "colab": {
1912 | "base_uri": "https://localhost:8080/",
1913 | "height": 35
1914 | },
1915 | "outputId": "d6e0730f-9975-412b-fa7c-364c3fa05fa9"
1916 | },
1917 | "source": [
1918 | "lista"
1919 | ],
1920 | "execution_count": 147,
1921 | "outputs": [
1922 | {
1923 | "output_type": "execute_result",
1924 | "data": {
1925 | "text/plain": [
1926 | "['A', 'B']"
1927 | ]
1928 | },
1929 | "metadata": {
1930 | "tags": []
1931 | },
1932 | "execution_count": 147
1933 | }
1934 | ]
1935 | },
1936 | {
1937 | "cell_type": "code",
1938 | "metadata": {
1939 | "id": "rUYPbsndag0z",
1940 | "colab_type": "code",
1941 | "colab": {
1942 | "base_uri": "https://localhost:8080/",
1943 | "height": 35
1944 | },
1945 | "outputId": "7f467a9d-dc25-4b90-916b-8b1794ac3846"
1946 | },
1947 | "source": [
1948 | "lista.count('B')"
1949 | ],
1950 | "execution_count": 150,
1951 | "outputs": [
1952 | {
1953 | "output_type": "execute_result",
1954 | "data": {
1955 | "text/plain": [
1956 | "1"
1957 | ]
1958 | },
1959 | "metadata": {
1960 | "tags": []
1961 | },
1962 | "execution_count": 150
1963 | }
1964 | ]
1965 | },
1966 | {
1967 | "cell_type": "code",
1968 | "metadata": {
1969 | "id": "QtWTZze6aoQK",
1970 | "colab_type": "code",
1971 | "colab": {
1972 | "base_uri": "https://localhost:8080/",
1973 | "height": 35
1974 | },
1975 | "outputId": "2c59e9a4-5afb-4f73-8f57-4518333cbbaa"
1976 | },
1977 | "source": [
1978 | "lista.index('B')"
1979 | ],
1980 | "execution_count": 151,
1981 | "outputs": [
1982 | {
1983 | "output_type": "execute_result",
1984 | "data": {
1985 | "text/plain": [
1986 | "1"
1987 | ]
1988 | },
1989 | "metadata": {
1990 | "tags": []
1991 | },
1992 | "execution_count": 151
1993 | }
1994 | ]
1995 | },
1996 | {
1997 | "cell_type": "code",
1998 | "metadata": {
1999 | "id": "P32Re0fKRTmu",
2000 | "colab_type": "code",
2001 | "colab": {
2002 | "base_uri": "https://localhost:8080/",
2003 | "height": 35
2004 | },
2005 | "outputId": "4821df72-0db7-4bcc-952b-608abba499de"
2006 | },
2007 | "source": [
2008 | "lista + lista"
2009 | ],
2010 | "execution_count": 152,
2011 | "outputs": [
2012 | {
2013 | "output_type": "execute_result",
2014 | "data": {
2015 | "text/plain": [
2016 | "['A', 'B', 'A', 'B']"
2017 | ]
2018 | },
2019 | "metadata": {
2020 | "tags": []
2021 | },
2022 | "execution_count": 152
2023 | }
2024 | ]
2025 | },
2026 | {
2027 | "cell_type": "code",
2028 | "metadata": {
2029 | "id": "3HPfUnQvRbSa",
2030 | "colab_type": "code",
2031 | "colab": {
2032 | "base_uri": "https://localhost:8080/",
2033 | "height": 35
2034 | },
2035 | "outputId": "335e7390-026b-4350-e68f-d5200345a660"
2036 | },
2037 | "source": [
2038 | "lista * 2"
2039 | ],
2040 | "execution_count": 153,
2041 | "outputs": [
2042 | {
2043 | "output_type": "execute_result",
2044 | "data": {
2045 | "text/plain": [
2046 | "['A', 'B', 'A', 'B']"
2047 | ]
2048 | },
2049 | "metadata": {
2050 | "tags": []
2051 | },
2052 | "execution_count": 153
2053 | }
2054 | ]
2055 | },
2056 | {
2057 | "cell_type": "code",
2058 | "metadata": {
2059 | "id": "Ozt0-U8MRcZg",
2060 | "colab_type": "code",
2061 | "colab": {
2062 | "base_uri": "https://localhost:8080/",
2063 | "height": 172
2064 | },
2065 | "outputId": "f3ca4b96-4162-4308-c7bb-a445e97ab39a"
2066 | },
2067 | "source": [
2068 | "lista + 2 #exemplo do que não funciona"
2069 | ],
2070 | "execution_count": 154,
2071 | "outputs": [
2072 | {
2073 | "output_type": "error",
2074 | "ename": "TypeError",
2075 | "evalue": "ignored",
2076 | "traceback": [
2077 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
2078 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
2079 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlista\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
2080 | "\u001b[0;31mTypeError\u001b[0m: can only concatenate list (not \"int\") to list"
2081 | ]
2082 | }
2083 | ]
2084 | },
2085 | {
2086 | "cell_type": "markdown",
2087 | "metadata": {
2088 | "id": "IvzSIyD-vQ0t",
2089 | "colab_type": "text"
2090 | },
2091 | "source": [
2092 | "## **Métodos de Strings** \n"
2093 | ]
2094 | },
2095 | {
2096 | "cell_type": "code",
2097 | "metadata": {
2098 | "id": "mZs_bHKnvqme",
2099 | "colab_type": "code",
2100 | "colab": {}
2101 | },
2102 | "source": [
2103 | "nome = 'Ricardo Rocha'"
2104 | ],
2105 | "execution_count": 155,
2106 | "outputs": []
2107 | },
2108 | {
2109 | "cell_type": "code",
2110 | "metadata": {
2111 | "id": "2WnusPh8bHAQ",
2112 | "colab_type": "code",
2113 | "colab": {
2114 | "base_uri": "https://localhost:8080/",
2115 | "height": 35
2116 | },
2117 | "outputId": "05e2dcc0-0a82-4b2b-f0b9-171a39d62259"
2118 | },
2119 | "source": [
2120 | "nome.lower()"
2121 | ],
2122 | "execution_count": 156,
2123 | "outputs": [
2124 | {
2125 | "output_type": "execute_result",
2126 | "data": {
2127 | "text/plain": [
2128 | "'ricardo rocha'"
2129 | ]
2130 | },
2131 | "metadata": {
2132 | "tags": []
2133 | },
2134 | "execution_count": 156
2135 | }
2136 | ]
2137 | },
2138 | {
2139 | "cell_type": "code",
2140 | "metadata": {
2141 | "id": "clmmVdIrbY8I",
2142 | "colab_type": "code",
2143 | "colab": {
2144 | "base_uri": "https://localhost:8080/",
2145 | "height": 35
2146 | },
2147 | "outputId": "88477507-ac0e-4a27-cafc-0679d72041ab"
2148 | },
2149 | "source": [
2150 | "nome.upper()"
2151 | ],
2152 | "execution_count": 157,
2153 | "outputs": [
2154 | {
2155 | "output_type": "execute_result",
2156 | "data": {
2157 | "text/plain": [
2158 | "'RICARDO ROCHA'"
2159 | ]
2160 | },
2161 | "metadata": {
2162 | "tags": []
2163 | },
2164 | "execution_count": 157
2165 | }
2166 | ]
2167 | },
2168 | {
2169 | "cell_type": "code",
2170 | "metadata": {
2171 | "id": "NOL14vrtba4Y",
2172 | "colab_type": "code",
2173 | "colab": {
2174 | "base_uri": "https://localhost:8080/",
2175 | "height": 35
2176 | },
2177 | "outputId": "415afb0f-7fe1-4869-f481-24b0bff112fc"
2178 | },
2179 | "source": [
2180 | "nome.capitalize()"
2181 | ],
2182 | "execution_count": 158,
2183 | "outputs": [
2184 | {
2185 | "output_type": "execute_result",
2186 | "data": {
2187 | "text/plain": [
2188 | "'Ricardo rocha'"
2189 | ]
2190 | },
2191 | "metadata": {
2192 | "tags": []
2193 | },
2194 | "execution_count": 158
2195 | }
2196 | ]
2197 | },
2198 | {
2199 | "cell_type": "code",
2200 | "metadata": {
2201 | "id": "_oBa94mobetG",
2202 | "colab_type": "code",
2203 | "colab": {
2204 | "base_uri": "https://localhost:8080/",
2205 | "height": 35
2206 | },
2207 | "outputId": "ec4a764b-f489-4030-b4ba-ffefe886cea0"
2208 | },
2209 | "source": [
2210 | "nome.split(' ')"
2211 | ],
2212 | "execution_count": 159,
2213 | "outputs": [
2214 | {
2215 | "output_type": "execute_result",
2216 | "data": {
2217 | "text/plain": [
2218 | "['Ricardo', 'Rocha']"
2219 | ]
2220 | },
2221 | "metadata": {
2222 | "tags": []
2223 | },
2224 | "execution_count": 159
2225 | }
2226 | ]
2227 | },
2228 | {
2229 | "cell_type": "markdown",
2230 | "metadata": {
2231 | "id": "810gugS8vlH-",
2232 | "colab_type": "text"
2233 | },
2234 | "source": [
2235 | "## **Iteradores FOR e WHILE** "
2236 | ]
2237 | },
2238 | {
2239 | "cell_type": "markdown",
2240 | "metadata": {
2241 | "id": "nuwTyZqyvrFX",
2242 | "colab_type": "text"
2243 | },
2244 | "source": [
2245 | "Utilizamos a estrutura de repetição FOR sempre que desejamos repetir um certo pedaço de código, com alterações ou não, por um número pre-determinado de vezes.\n",
2246 | "\n",
2247 | "Veja os exemplos"
2248 | ]
2249 | },
2250 | {
2251 | "cell_type": "code",
2252 | "metadata": {
2253 | "id": "N9ZLbdPTeYa4",
2254 | "colab_type": "code",
2255 | "colab": {}
2256 | },
2257 | "source": [
2258 | "for variante in lista_de_variações:\n",
2259 | " codigo a ser repetido\n",
2260 | "\n",
2261 | "#esquema geral do FOR, não executar esse bloco"
2262 | ],
2263 | "execution_count": null,
2264 | "outputs": []
2265 | },
2266 | {
2267 | "cell_type": "code",
2268 | "metadata": {
2269 | "id": "oMlptGxbel3R",
2270 | "colab_type": "code",
2271 | "colab": {
2272 | "base_uri": "https://localhost:8080/",
2273 | "height": 72
2274 | },
2275 | "outputId": "fa8bb0df-d383-43a0-ceb6-4cd73068755b"
2276 | },
2277 | "source": [
2278 | "for i in ['banana', 'mamão', 'abacate']:\n",
2279 | " print('Eu gosto de {}'.format(i))"
2280 | ],
2281 | "execution_count": null,
2282 | "outputs": [
2283 | {
2284 | "output_type": "stream",
2285 | "text": [
2286 | "Eu gosto de banana\n",
2287 | "Eu gosto de mamão\n",
2288 | "Eu gosto de abacate\n"
2289 | ],
2290 | "name": "stdout"
2291 | }
2292 | ]
2293 | },
2294 | {
2295 | "cell_type": "code",
2296 | "metadata": {
2297 | "id": "S9ZcdJy1vrRH",
2298 | "colab_type": "code",
2299 | "colab": {
2300 | "base_uri": "https://localhost:8080/",
2301 | "height": 199
2302 | },
2303 | "outputId": "faa31159-c022-4e97-d914-e2784c1fb836"
2304 | },
2305 | "source": [
2306 | "for i in range(10):\n",
2307 | " print(i*'*')"
2308 | ],
2309 | "execution_count": null,
2310 | "outputs": [
2311 | {
2312 | "output_type": "stream",
2313 | "text": [
2314 | "\n",
2315 | "*\n",
2316 | "**\n",
2317 | "***\n",
2318 | "****\n",
2319 | "*****\n",
2320 | "******\n",
2321 | "*******\n",
2322 | "********\n",
2323 | "*********\n"
2324 | ],
2325 | "name": "stdout"
2326 | }
2327 | ]
2328 | },
2329 | {
2330 | "cell_type": "markdown",
2331 | "metadata": {
2332 | "id": "-mhqfzfGfnbS",
2333 | "colab_type": "text"
2334 | },
2335 | "source": [
2336 | "Problema: Se eu tomar dois números inteiros no intervalo [1, 10], qual a probabilidade aproximada da soma dos números ser maior que 10?"
2337 | ]
2338 | },
2339 | {
2340 | "cell_type": "code",
2341 | "metadata": {
2342 | "id": "H98ra85ke7bY",
2343 | "colab_type": "code",
2344 | "colab": {
2345 | "base_uri": "https://localhost:8080/",
2346 | "height": 35
2347 | },
2348 | "outputId": "e70df48b-34c7-4ef9-f6ca-53082c9b7bd0"
2349 | },
2350 | "source": [
2351 | "from random import randint\n",
2352 | "lista = []\n",
2353 | "replicas = 10000\n",
2354 | "\n",
2355 | "for i in range(replicas):\n",
2356 | " if randint(1,10) + randint (1,10) > 10:\n",
2357 | " lista.append(True)\n",
2358 | " else:\n",
2359 | " lista.append(False)\n",
2360 | "\n",
2361 | "prob = sum(lista)/replicas\n",
2362 | "\n",
2363 | "print('A probabilidade aproximada é {}%'.format(100*prob))"
2364 | ],
2365 | "execution_count": 171,
2366 | "outputs": [
2367 | {
2368 | "output_type": "stream",
2369 | "text": [
2370 | "A probabilidade aproximada é 54.52%\n"
2371 | ],
2372 | "name": "stdout"
2373 | }
2374 | ]
2375 | },
2376 | {
2377 | "cell_type": "markdown",
2378 | "metadata": {
2379 | "id": "e63f82migunw",
2380 | "colab_type": "text"
2381 | },
2382 | "source": [
2383 | "\n",
2384 | "\n",
2385 | "---\n",
2386 | "\n"
2387 | ]
2388 | },
2389 | {
2390 | "cell_type": "markdown",
2391 | "metadata": {
2392 | "id": "aCsKAKnogvoL",
2393 | "colab_type": "text"
2394 | },
2395 | "source": [
2396 | "O iterador WHILE usamos quando queremos repetir código sem saber de antemão quando este deve ser interrompido. Ele será finalizado quando alguma condição for satisfeita. \n",
2397 | "\n",
2398 | "Por isso, tome cuidado em não escrever um WHILE infinito. Você terá que cancelar a execução do código para pará-lo.\n",
2399 | "\n",
2400 | "Veja a estrutura geral"
2401 | ]
2402 | },
2403 | {
2404 | "cell_type": "code",
2405 | "metadata": {
2406 | "id": "XZq7UqmPhBQ_",
2407 | "colab_type": "code",
2408 | "colab": {}
2409 | },
2410 | "source": [
2411 | "while condicao_verdadeira:\n",
2412 | " codigo repetido\n",
2413 | " if condicao:\n",
2414 | " break\n",
2415 | " \n",
2416 | "#esquema geral do FOR, não executar esse bloco"
2417 | ],
2418 | "execution_count": null,
2419 | "outputs": []
2420 | },
2421 | {
2422 | "cell_type": "code",
2423 | "metadata": {
2424 | "id": "PlNNJ5xAhgBG",
2425 | "colab_type": "code",
2426 | "colab": {
2427 | "base_uri": "https://localhost:8080/",
2428 | "height": 217
2429 | },
2430 | "outputId": "ccb58d55-288a-4533-d169-977770dd1a28"
2431 | },
2432 | "source": [
2433 | "i = 1\n",
2434 | "while i <= 10:\n",
2435 | " print(i * '*') \n",
2436 | " i += 1"
2437 | ],
2438 | "execution_count": null,
2439 | "outputs": [
2440 | {
2441 | "output_type": "stream",
2442 | "text": [
2443 | "*\n",
2444 | "**\n",
2445 | "***\n",
2446 | "****\n",
2447 | "*****\n",
2448 | "******\n",
2449 | "*******\n",
2450 | "********\n",
2451 | "*********\n",
2452 | "**********\n",
2453 | "*\n"
2454 | ],
2455 | "name": "stdout"
2456 | }
2457 | ]
2458 | },
2459 | {
2460 | "cell_type": "markdown",
2461 | "metadata": {
2462 | "id": "FZwNhK1bidv-",
2463 | "colab_type": "text"
2464 | },
2465 | "source": [
2466 | "Código equivalente utilizando o comando break para interromper o loop"
2467 | ]
2468 | },
2469 | {
2470 | "cell_type": "code",
2471 | "metadata": {
2472 | "id": "n5sqpDODiNIB",
2473 | "colab_type": "code",
2474 | "colab": {
2475 | "base_uri": "https://localhost:8080/",
2476 | "height": 181
2477 | },
2478 | "outputId": "c63468b9-6742-40cc-a844-ea7071949fc2"
2479 | },
2480 | "source": [
2481 | "i = 1\n",
2482 | "while True:\n",
2483 | " print(i * '*') \n",
2484 | " i += 1\n",
2485 | " if i == 10:\n",
2486 | " break\n"
2487 | ],
2488 | "execution_count": null,
2489 | "outputs": [
2490 | {
2491 | "output_type": "stream",
2492 | "text": [
2493 | "*\n",
2494 | "**\n",
2495 | "***\n",
2496 | "****\n",
2497 | "*****\n",
2498 | "******\n",
2499 | "*******\n",
2500 | "********\n",
2501 | "*********\n"
2502 | ],
2503 | "name": "stdout"
2504 | }
2505 | ]
2506 | },
2507 | {
2508 | "cell_type": "markdown",
2509 | "metadata": {
2510 | "id": "y_vENC2cim9V",
2511 | "colab_type": "text"
2512 | },
2513 | "source": [
2514 | "Exemplo de um **while** infinito"
2515 | ]
2516 | },
2517 | {
2518 | "cell_type": "code",
2519 | "metadata": {
2520 | "id": "8OSTl1SQiqso",
2521 | "colab_type": "code",
2522 | "colab": {}
2523 | },
2524 | "source": [
2525 | "while True:\n",
2526 | " print('Python é legal demais!')"
2527 | ],
2528 | "execution_count": null,
2529 | "outputs": []
2530 | },
2531 | {
2532 | "cell_type": "markdown",
2533 | "metadata": {
2534 | "id": "ZAOdZjiQiyJ8",
2535 | "colab_type": "text"
2536 | },
2537 | "source": [
2538 | "O **while** também aceita o comando **else** no após o seu fim. Algo para executar após o break acontecer"
2539 | ]
2540 | },
2541 | {
2542 | "cell_type": "code",
2543 | "metadata": {
2544 | "id": "B9vxNRKOi9zH",
2545 | "colab_type": "code",
2546 | "colab": {
2547 | "base_uri": "https://localhost:8080/",
2548 | "height": 217
2549 | },
2550 | "outputId": "b515dc9e-8416-4b66-fec4-d946b464e8f2"
2551 | },
2552 | "source": [
2553 | "i = 1\n",
2554 | "while i <= 10:\n",
2555 | " print(i * '*') \n",
2556 | " i += 1\n",
2557 | "else:\n",
2558 | " print('*')"
2559 | ],
2560 | "execution_count": 163,
2561 | "outputs": [
2562 | {
2563 | "output_type": "stream",
2564 | "text": [
2565 | "*\n",
2566 | "**\n",
2567 | "***\n",
2568 | "****\n",
2569 | "*****\n",
2570 | "******\n",
2571 | "*******\n",
2572 | "********\n",
2573 | "*********\n",
2574 | "**********\n",
2575 | "*\n"
2576 | ],
2577 | "name": "stdout"
2578 | }
2579 | ]
2580 | },
2581 | {
2582 | "cell_type": "markdown",
2583 | "metadata": {
2584 | "id": "5x-9-W9cReKJ",
2585 | "colab_type": "text"
2586 | },
2587 | "source": [
2588 | "## **Projeto** - Jogo da Roleta \n",
2589 | "\n",
2590 | "\n",
2591 | "\n"
2592 | ]
2593 | },
2594 | {
2595 | "cell_type": "markdown",
2596 | "metadata": {
2597 | "id": "tCpIlj_djS9S",
2598 | "colab_type": "text"
2599 | },
2600 | "source": [
2601 | "Vamos criar um programa para simular o jogo da roleta. Vamos considerar uma simplificação do jogo, em que uma pessoa aposta em um número e aí se verifica se a roleta sorteou o número que a pessoa apostou. Para fins de ilustração, suponha também que a roleta possui apenas os números de 0 a 10. O programa deve controlar a quantidade de fichas que o usuário tem para jogar."
2602 | ]
2603 | },
2604 | {
2605 | "cell_type": "code",
2606 | "metadata": {
2607 | "id": "BLWLVIp-w8P8",
2608 | "colab_type": "code",
2609 | "colab": {}
2610 | },
2611 | "source": [
2612 | "from random import randint\n",
2613 | "\n",
2614 | "def Jogo():\n",
2615 | " n = input('\\nEm qual número da roleta você deseja apostar? ')\n",
2616 | " if n == 'sair':\n",
2617 | " return 'sair'\n",
2618 | " else: \n",
2619 | " rol = randint(0, 5)\n",
2620 | " if int(n) == rol:\n",
2621 | " print('\\n' + '*' * 12)\n",
2622 | " print('Você ganhou!')\n",
2623 | " print('*' * 12)\n",
2624 | " saldo = 5\n",
2625 | " else:\n",
2626 | " print('\\nNão foi dessa vez!')\n",
2627 | " saldo = -1\n",
2628 | " return saldo\n"
2629 | ],
2630 | "execution_count": 164,
2631 | "outputs": []
2632 | },
2633 | {
2634 | "cell_type": "code",
2635 | "metadata": {
2636 | "id": "rWnVfI5IvXTP",
2637 | "colab_type": "code",
2638 | "colab": {}
2639 | },
2640 | "source": [
2641 | "def Roleta():\n",
2642 | "\n",
2643 | " historico = []\n",
2644 | " print('\\nBem vindos a Roleta!')\n",
2645 | "\n",
2646 | " fichas = int(input('\\nQuantas fichas você deseja comprar? '))\n",
2647 | "\n",
2648 | " while fichas > 0:\n",
2649 | " a = Jogo()\n",
2650 | "\n",
2651 | " if a == 'sair':\n",
2652 | " print('\\n***Fim de jogo, seu saldo final é de {} fichas***'.format(fichas))\n",
2653 | " print(historico)\n",
2654 | " break\n",
2655 | " else:\n",
2656 | " fichas += a\n",
2657 | " print('\\nSeu saldo de fichas é {}'.format(fichas))\n",
2658 | " if a == -1:\n",
2659 | " historico.append('D')\n",
2660 | " else:\n",
2661 | " historico.append('V')\n",
2662 | " else:\n",
2663 | " print('\\n***Fim de jogo!***')\n",
2664 | " print(historico)"
2665 | ],
2666 | "execution_count": 165,
2667 | "outputs": []
2668 | },
2669 | {
2670 | "cell_type": "code",
2671 | "metadata": {
2672 | "id": "sga-xfXxzuhA",
2673 | "colab_type": "code",
2674 | "colab": {
2675 | "base_uri": "https://localhost:8080/",
2676 | "height": 545
2677 | },
2678 | "outputId": "3f89a99a-1e86-4d92-c464-7cc67e47c882"
2679 | },
2680 | "source": [
2681 | "Roleta()"
2682 | ],
2683 | "execution_count": 167,
2684 | "outputs": [
2685 | {
2686 | "output_type": "stream",
2687 | "text": [
2688 | "\n",
2689 | "Bem vindos a Roleta!\n",
2690 | "\n",
2691 | "Quantas fichas você deseja comprar? 5\n",
2692 | "\n",
2693 | "Em qual número da roleta você deseja apostar? 3\n",
2694 | "\n",
2695 | "Não foi dessa vez!\n",
2696 | "\n",
2697 | "Seu saldo de fichas é 4\n",
2698 | "\n",
2699 | "Em qual número da roleta você deseja apostar? 3\n",
2700 | "\n",
2701 | "************\n",
2702 | "Você ganhou!\n",
2703 | "************\n",
2704 | "\n",
2705 | "Seu saldo de fichas é 9\n",
2706 | "\n",
2707 | "Em qual número da roleta você deseja apostar? 3\n",
2708 | "\n",
2709 | "Não foi dessa vez!\n",
2710 | "\n",
2711 | "Seu saldo de fichas é 8\n",
2712 | "\n",
2713 | "Em qual número da roleta você deseja apostar? sair\n",
2714 | "\n",
2715 | "***Fim de jogo, seu saldo final é de 8 fichas***\n",
2716 | "['D', 'V', 'D']\n"
2717 | ],
2718 | "name": "stdout"
2719 | }
2720 | ]
2721 | },
2722 | {
2723 | "cell_type": "markdown",
2724 | "metadata": {
2725 | "id": "XW7xW90KucKA",
2726 | "colab_type": "text"
2727 | },
2728 | "source": [
2729 | "## **Exercício** \n",
2730 | "\n",
2731 | "Faça um programa que simule uma **slot machine**.\n",
2732 | "\n",
2733 | "**Parte A:** Uma slot machine é uma máquina muito comum em cassinos. A pessoa puxa uma alavanca e aparecem na tela 3 símbolos aleatoriamente, de uma lista com diversos deles. Se os símbolos forem iguais, então a pessoa ganha. A pessoa entrar com um tanto escolhido de fichas, e jogar até que acabe. Quando o programa terminar, uma mensagem resumindo os totais que ela ganhou deve ser exibida.\n",
2734 | "\n",
2735 | "**Parte B:** Estime as probabilidades de acontecer cada tipo de prêmio na slot machine."
2736 | ]
2737 | },
2738 | {
2739 | "cell_type": "markdown",
2740 | "metadata": {
2741 | "id": "5hW8TlDfpUaX",
2742 | "colab_type": "text"
2743 | },
2744 | "source": [
2745 | "## **Links Úteis** \n",
2746 | "\n",
2747 | "- [Documentação sobre os métodos de listas](https://docs.python.org/3/tutorial/datastructures.html)\n",
2748 | "- [Documentação sobre os métodos de strings](https://docs.python.org/3/library/stdtypes.html)\n",
2749 | "- [Documentação da biblioteca random](https://docs.python.org/3/library/random.html)"
2750 | ]
2751 | }
2752 | ]
2753 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Conjunto de arquivos para o meu curso de Python para Machine Learning
2 |
--------------------------------------------------------------------------------