EddyGiusepe commited on
Commit
302d253
·
1 Parent(s): 49fb328

exemplos em Python

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. 1_Python_coding.ipynb +356 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ venv_coding/
1_Python_coding.ipynb ADDED
@@ -0,0 +1,356 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "attachments": {},
5
+ "cell_type": "markdown",
6
+ "metadata": {},
7
+ "source": [
8
+ "<h1 align=\"center\"><font color=\"yellow\">Python coding</font></h1>"
9
+ ]
10
+ },
11
+ {
12
+ "attachments": {},
13
+ "cell_type": "markdown",
14
+ "metadata": {},
15
+ "source": [
16
+ "<font color=\"yellow\">Data Scientist.: Dr.Eddy Giusepe Chirinos Isidro</font>"
17
+ ]
18
+ },
19
+ {
20
+ "attachments": {},
21
+ "cell_type": "markdown",
22
+ "metadata": {},
23
+ "source": [
24
+ "# <font color=\"red\">Exemplo 1</font>"
25
+ ]
26
+ },
27
+ {
28
+ "attachments": {},
29
+ "cell_type": "markdown",
30
+ "metadata": {},
31
+ "source": [
32
+ "Dado um ano, informe se é um ano bissexto (lead)."
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 23,
38
+ "metadata": {},
39
+ "outputs": [
40
+ {
41
+ "data": {
42
+ "text/plain": [
43
+ "False"
44
+ ]
45
+ },
46
+ "execution_count": 23,
47
+ "metadata": {},
48
+ "output_type": "execute_result"
49
+ }
50
+ ],
51
+ "source": [
52
+ "def is_leap(year: int):\n",
53
+ " if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
54
+ " return True\n",
55
+ " else: \n",
56
+ " return False\n",
57
+ "\n",
58
+ "is_leap(2015)"
59
+ ]
60
+ },
61
+ {
62
+ "attachments": {},
63
+ "cell_type": "markdown",
64
+ "metadata": {},
65
+ "source": [
66
+ "# <font color=\"red\">Exemplo 2</font>"
67
+ ]
68
+ },
69
+ {
70
+ "attachments": {},
71
+ "cell_type": "markdown",
72
+ "metadata": {},
73
+ "source": [
74
+ "Encontre a diferença entre o quadrado da soma e a soma dos quadrados dos primeiros N números naturais"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": 31,
80
+ "metadata": {},
81
+ "outputs": [
82
+ {
83
+ "name": "stdout",
84
+ "output_type": "stream",
85
+ "text": [
86
+ "4\n"
87
+ ]
88
+ }
89
+ ],
90
+ "source": [
91
+ "def square(x):\n",
92
+ " return x ** 2\n",
93
+ "\n",
94
+ "\n",
95
+ "def dif_square(n):\n",
96
+ " return sum(range(n + 1)) ** 2 - sum(map(lambda x: x**2, range(n + 1)))\n",
97
+ "\n",
98
+ "\n",
99
+ "print(dif_square(2))"
100
+ ]
101
+ },
102
+ {
103
+ "attachments": {},
104
+ "cell_type": "markdown",
105
+ "metadata": {},
106
+ "source": [
107
+ "# <font color=\"red\">Exemplo 3</font>"
108
+ ]
109
+ },
110
+ {
111
+ "attachments": {},
112
+ "cell_type": "markdown",
113
+ "metadata": {},
114
+ "source": [
115
+ "Um [número de Armstrong](https://en.wikipedia.org/wiki/Narcissistic_number) é um número que é a soma de seus próprios dígitos, cada um elevado à potência do número de dígitos.\n",
116
+ "\n",
117
+ "Por exemplo: \n",
118
+ "\n",
119
+ "* $9 = 9^1 = 9$\n",
120
+ "\n",
121
+ "* $153 =1^3 + 5^3 + 3^3 = 153$ "
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": 33,
127
+ "metadata": {},
128
+ "outputs": [
129
+ {
130
+ "name": "stdout",
131
+ "output_type": "stream",
132
+ "text": [
133
+ "True\n"
134
+ ]
135
+ }
136
+ ],
137
+ "source": [
138
+ "def armstrong(number: int):\n",
139
+ " sum = 0\n",
140
+ " temp = number\n",
141
+ " order = len(str(number))\n",
142
+ "\n",
143
+ " while temp > 0:\n",
144
+ " digit = temp % 10\n",
145
+ " sum += digit ** order\n",
146
+ " temp //= 10 # temp = temp // 10\n",
147
+ "\n",
148
+ " return True if sum == number else False\n",
149
+ "\n",
150
+ "\n",
151
+ "\n",
152
+ "print(armstrong(153))"
153
+ ]
154
+ },
155
+ {
156
+ "attachments": {},
157
+ "cell_type": "markdown",
158
+ "metadata": {},
159
+ "source": [
160
+ "# <font color=\"red\">Exemplo 4</font>"
161
+ ]
162
+ },
163
+ {
164
+ "attachments": {},
165
+ "cell_type": "markdown",
166
+ "metadata": {},
167
+ "source": [
168
+ "Exibir os primeiros N números na ordem inversa."
169
+ ]
170
+ },
171
+ {
172
+ "cell_type": "code",
173
+ "execution_count": 42,
174
+ "metadata": {},
175
+ "outputs": [
176
+ {
177
+ "name": "stdout",
178
+ "output_type": "stream",
179
+ "text": [
180
+ "8 7 6 5 4 3 2 1 "
181
+ ]
182
+ }
183
+ ],
184
+ "source": [
185
+ "N = int(input(\"Digita um número Inteiro N: \"))\n",
186
+ "\n",
187
+ "for i in range(N, 0, -1):\n",
188
+ " print(i, end=\" \")\n"
189
+ ]
190
+ },
191
+ {
192
+ "attachments": {},
193
+ "cell_type": "markdown",
194
+ "metadata": {},
195
+ "source": [
196
+ "Soma dos primeiros N números."
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": 45,
202
+ "metadata": {},
203
+ "outputs": [
204
+ {
205
+ "name": "stdout",
206
+ "output_type": "stream",
207
+ "text": [
208
+ "55\n"
209
+ ]
210
+ }
211
+ ],
212
+ "source": [
213
+ "N = int(input(\"Digita um número inteiro N: \"))\n",
214
+ "\n",
215
+ "sum = 0\n",
216
+ "for i in range(0, N + 1): # Também: for i in range(N, 0, -1):\n",
217
+ " sum = sum + i \n",
218
+ "\n",
219
+ "print(sum)"
220
+ ]
221
+ },
222
+ {
223
+ "attachments": {},
224
+ "cell_type": "markdown",
225
+ "metadata": {},
226
+ "source": [
227
+ "É um ano bissexto."
228
+ ]
229
+ },
230
+ {
231
+ "cell_type": "code",
232
+ "execution_count": 47,
233
+ "metadata": {},
234
+ "outputs": [
235
+ {
236
+ "name": "stdout",
237
+ "output_type": "stream",
238
+ "text": [
239
+ "1996 é ano bissexto.\n"
240
+ ]
241
+ }
242
+ ],
243
+ "source": [
244
+ "year = int(input(\"Digita o Ano para verificar se é bissexto:\"))\n",
245
+ "\n",
246
+ "if (year % 400 == 0):\n",
247
+ " print(str(year) + \" é ano bissexto.\")\n",
248
+ "\n",
249
+ "elif (year % 4 == 0 and year % 100 != 0):\n",
250
+ " print(str(year) + \" é ano bissexto.\")\n",
251
+ "\n",
252
+ "else:\n",
253
+ " print(str(year) + \" não é ano bissexto.\")\n"
254
+ ]
255
+ },
256
+ {
257
+ "attachments": {},
258
+ "cell_type": "markdown",
259
+ "metadata": {},
260
+ "source": [
261
+ "Verifique se o número fornecido é um número primo ou não."
262
+ ]
263
+ },
264
+ {
265
+ "cell_type": "code",
266
+ "execution_count": 63,
267
+ "metadata": {},
268
+ "outputs": [
269
+ {
270
+ "name": "stdout",
271
+ "output_type": "stream",
272
+ "text": [
273
+ "3 é um número primo.\n"
274
+ ]
275
+ }
276
+ ],
277
+ "source": [
278
+ "num = int(input(\"Digita um número Inteiro: \"))\n",
279
+ "\n",
280
+ "\n",
281
+ "count = 0\n",
282
+ "for i in range(2,int(num/2) + 1):\n",
283
+ " if(num % i == 0):\n",
284
+ " count = count + 1\n",
285
+ "\n",
286
+ "\n",
287
+ "if((count == 0) and (num > 1)):\n",
288
+ " print(str(num) + \" é um número primo.\")\n",
289
+ "else:\n",
290
+ " print(str(num) + \" não é um número primo.\")"
291
+ ]
292
+ },
293
+ {
294
+ "attachments": {},
295
+ "cell_type": "markdown",
296
+ "metadata": {},
297
+ "source": [
298
+ "Soma dos dígitos de um número."
299
+ ]
300
+ },
301
+ {
302
+ "cell_type": "code",
303
+ "execution_count": 68,
304
+ "metadata": {},
305
+ "outputs": [
306
+ {
307
+ "name": "stdout",
308
+ "output_type": "stream",
309
+ "text": [
310
+ "3\n"
311
+ ]
312
+ }
313
+ ],
314
+ "source": [
315
+ "n = int(input(\"Digita um número inteiro: \"))\n",
316
+ "\n",
317
+ "sum = 0\n",
318
+ "\n",
319
+ "while (n != 0):\n",
320
+ " sum = sum + n % 10\n",
321
+ " n = n // 10\n",
322
+ "\n",
323
+ "print(sum)"
324
+ ]
325
+ },
326
+ {
327
+ "cell_type": "code",
328
+ "execution_count": null,
329
+ "metadata": {},
330
+ "outputs": [],
331
+ "source": []
332
+ }
333
+ ],
334
+ "metadata": {
335
+ "kernelspec": {
336
+ "display_name": "venv_coding",
337
+ "language": "python",
338
+ "name": "python3"
339
+ },
340
+ "language_info": {
341
+ "codemirror_mode": {
342
+ "name": "ipython",
343
+ "version": 3
344
+ },
345
+ "file_extension": ".py",
346
+ "mimetype": "text/x-python",
347
+ "name": "python",
348
+ "nbconvert_exporter": "python",
349
+ "pygments_lexer": "ipython3",
350
+ "version": "3.10.12"
351
+ },
352
+ "orig_nbformat": 4
353
+ },
354
+ "nbformat": 4,
355
+ "nbformat_minor": 2
356
+ }