forked from prog-geo/prog-geo.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
introducao-programacao.html
252 lines (237 loc) · 23.1 KB
/
introducao-programacao.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html class="writer-html5" lang="pt-BR" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>2. Introdução à Programação com a Linguagem Python — SER-347/CAP-419</title>
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="_static/tabs.css" type="text/css" />
<link rel="stylesheet" href="_static/table_styling.css" type="text/css" />
<link rel="stylesheet" href="_static/prog-geo.css" type="text/css" />
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="canonical" href="https://prog-geo.github.io/introducao-programacao.html" />
<!--[if lt IE 9]>
<script src="_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script src="_static/translations.js"></script>
<script src="_static/js/theme.js"></script>
<link rel="index" title="Índice" href="genindex.html" />
<link rel="search" title="Buscar" href="search.html" />
<link rel="next" title="2.1. Introdução" href="introducao-programacao/introducao.html" />
<link rel="prev" title="1.4. Instalação do JupyterLab através do Docker" href="instalacao/jupyterlab.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #2980B9" >
<a href="index.html" class="icon icon-home"> SER-347/CAP-419
<img src="_static/logo.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Pesquisar documentos" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Menu de navegação">
<ul>
<li class="toctree-l1"><a class="reference internal" href="index.html">Introdução à Programação com Dados Geoespaciais</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Visão Geral do Curso:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="visao-geral.html">Visão Geral do Curso</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Aulas:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="instalacao.html">1. Instalando e Configurando o Ambiente de Programação</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">2. Introdução à Programação com a Linguagem Python</a><ul>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/introducao.html">2.1. Introdução</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/tipos-dados.html">2.2. Tipos de Dados</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/tipos-numericos.html">2.3. Tipos Numéricos</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/expressoes.html">2.4. Expressões</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/chamada-funcoes.html">2.5. Chamada de Funções</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/variaveis.html">2.6. Variáveis</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/comentarios.html">2.7. Comentários</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/tipo-logico.html">2.8. Tipo Lógico</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/op-relacionais.html">2.9. Operadores Relacionais</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/expressoes-logicas.html">2.10. Expressões Lógicas</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/estruturas-condicionais.html">2.11. Estruturas Condicionais</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/estruturas-repeticao.html">2.12. Estruturas de Repetição</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/strings.html">2.13. O Tipo String</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/keywords.html">2.14. Palavras-chave</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/sequencias.html">2.15. Sequências</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/dicionarios.html">2.16. Dicionários</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/set.html">2.17. Conjuntos</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/formatacao-strings.html">2.18. Formatação de Strings</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/lacos-for.html">2.19. Laços do tipo for</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/break-continue.html">2.20. Os Comandos <code class="docutils literal notranslate"><span class="pre">break</span></code> e <code class="docutils literal notranslate"><span class="pre">continue</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/comandos-compostos.html">2.21. Comandos Compostos</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/funcoes.html">2.22. Funções</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/escopo.html">2.23. Escopo de Variáveis</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/consideracoes.html">2.24. Considerações Finais</a></li>
<li class="toctree-l2"><a class="reference internal" href="introducao-programacao/exemplos.html">2.25. Exemplos</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="jupyter.html">3. Jupyter</a></li>
<li class="toctree-l1"><a class="reference internal" href="imagens.html">4. Imagens - Processamento e Visualização</a></li>
<li class="toctree-l1"><a class="reference internal" href="vetorial.html">5. Manipulação de Dados Vetoriais</a></li>
<li class="toctree-l1"><a class="reference internal" href="qgis.html">6. Programando com o QGIS</a></li>
<li class="toctree-l1"><a class="reference internal" href="variados.html">7. Tópicos Variados</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Referências Bibliográficas</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="referencias.html">Referências Bibliográficas</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Listas de Exercícios:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="listas.html">Listas de Exercícios</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Lista de Projetos:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="projetos/2022/index.html">Turma 2022</a></li>
<li class="toctree-l1"><a class="reference internal" href="projetos/2021/index.html">Turma 2021</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Informações Gerais:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="licenca.html">Licença</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Agradecimentos:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="agradecimentos.html">Agradecimentos</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Menu de navegação móvel" style="background: #2980B9" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">SER-347/CAP-419</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Navegação da página">
<ul class="wy-breadcrumbs">
<li><a href="index.html" class="icon icon-home"></a> »</li>
<li><span class="section-number">2. </span>Introdução à Programação com a Linguagem Python</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Navegação sequencial da página">
<a href="instalacao/jupyterlab.html" class="btn btn-neutral float-left" title="1.4. Instalação do JupyterLab através do Docker" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Anterior</a>
<a href="introducao-programacao/introducao.html" class="btn btn-neutral float-right" title="2.1. Introdução" accesskey="n">Próximo <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="introducao-a-programacao-com-a-linguagem-python">
<span id="cap-introducao-programacao"></span><h1><span class="section-number">2. </span>Introdução à Programação com a Linguagem Python<a class="headerlink" href="#introducao-a-programacao-com-a-linguagem-python" title="Link permanente para este título"></a></h1>
<p>Um <strong>programa</strong> é formado por uma <strong>sequência de instruções</strong> que diz ao computador como realizar uma determinada tarefa. As instruções podem dizer como o programa deve interagir com o usuário, como ele deve utilizar o hardware, e como operar os dados para realizar o que chamamos de <strong>computações</strong>. Quando um computador segue as instruções de um programa, dizemos que ele está <strong>executando o programa</strong>.</p>
<p>A <strong>atividade de programar</strong> consiste na habilidade de expressar as computações que dizem ao computador como resolver um determinado problema. Uma das ferramentas que permitem expressar essas computações são as <strong>Linguagens de Programação</strong>. O <a class="reference internal" href="#introd-prog-code-timeseries"><span class="std std-numref">Trecho de Código 2.1</span></a> exemplifica um programa escrito na linguagem Python, composto por uma sequência de instruções encadeadas de forma a produzir o gráfico com a série temporal mostrado na <a class="reference internal" href="#introd-prog-fig-timeseries"><span class="std std-numref">Figura 2.1</span></a>.</p>
<div class="literal-block-wrapper docutils container" id="introd-prog-code-timeseries">
<div class="code-block-caption"><span class="caption-number">Trecho de Código 2.1 - </span><span class="caption-text">Recuperando uma série temporal.</span><a class="headerlink" href="#introd-prog-code-timeseries" title="Link Permanente para esse código"></a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="linenos"> 2</span><span class="kn">from</span> <span class="nn">wtss</span> <span class="kn">import</span> <span class="n">wtss</span>
<span class="linenos"> 3</span>
<span class="linenos"> 4</span><span class="n">w</span> <span class="o">=</span> <span class="n">wtss</span><span class="p">(</span><span class="s1">'http://www.esensing.dpi.inpe.br/'</span><span class="p">)</span>
<span class="linenos"> 5</span>
<span class="linenos"> 6</span><span class="n">ts</span> <span class="o">=</span> <span class="n">w</span><span class="o">.</span><span class="n">time_series</span><span class="p">(</span><span class="n">coverage</span><span class="o">=</span><span class="s1">'MOD13Q1'</span><span class="p">,</span> <span class="n">attributes</span><span class="o">=</span><span class="p">(</span><span class="s1">'red'</span><span class="p">,</span> <span class="s1">'nir'</span><span class="p">),</span>
<span class="linenos"> 7</span> <span class="n">latitude</span><span class="o">=-</span><span class="mf">12.0</span><span class="p">,</span> <span class="n">longitude</span><span class="o">=-</span><span class="mf">54.0</span><span class="p">,</span>
<span class="linenos"> 8</span> <span class="n">start_date</span><span class="o">=</span><span class="s1">'2001-01-01'</span><span class="p">,</span> <span class="n">end_date</span><span class="o">=</span><span class="s1">'2001-12-31'</span><span class="p">)</span>
<span class="linenos"> 9</span>
<span class="linenos">10</span><span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
<span class="linenos">11</span>
<span class="linenos">12</span><span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">'Time Series MOD13Q1'</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">24</span><span class="p">)</span>
<span class="linenos">13</span>
<span class="linenos">14</span><span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">'Date'</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span>
<span class="linenos">15</span><span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'Surface Reflectance'</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span>
<span class="linenos">16</span>
<span class="linenos">17</span><span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">timeline</span><span class="p">,</span> <span class="n">ts</span><span class="p">[</span><span class="s1">'red'</span><span class="p">],</span> <span class="n">color</span><span class="o">=</span><span class="s1">'red'</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="s1">'-'</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">'o'</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'red'</span><span class="p">)</span>
<span class="linenos">18</span><span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">timeline</span><span class="p">,</span> <span class="n">ts</span><span class="p">[</span><span class="s1">'nir'</span><span class="p">],</span> <span class="n">color</span><span class="o">=</span><span class="s1">'purple'</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="s1">'-'</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">'o'</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'nir'</span><span class="p">)</span>
<span class="linenos">19</span>
<span class="linenos">20</span><span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="linenos">21</span>
<span class="linenos">22</span><span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="n">b</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'gray'</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">'--'</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.5</span><span class="p">);</span>
<span class="linenos">23</span>
<span class="linenos">24</span><span class="n">fig</span><span class="o">.</span><span class="n">autofmt_xdate</span><span class="p">()</span>
<span class="linenos">25</span>
<span class="linenos">26</span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="align-center figure align-default" id="introd-prog-fig-timeseries">
<a class="reference internal image-reference" href="_images/timeseries.png"><img alt="Série temporal das bandas red e nir para uma dada localização geográfica" src="_images/timeseries.png" style="width: 80%;" /></a>
<p class="caption"><span class="caption-number">Figura 2.1 - </span><span class="caption-text">Série temporal das bandas <code class="docutils literal notranslate"><span class="pre">red</span></code> e <code class="docutils literal notranslate"><span class="pre">nir</span></code> para uma dada localização geográfica.</span><a class="headerlink" href="#introd-prog-fig-timeseries" title="Link Permanente para essa imagem"></a></p>
</div>
<p>Para escrever programas em alguma linguagem, é preciso conhecer um pouco de <strong>lógica de programação</strong>. Este capítulo apresenta uma introdução aos principais conceitos e técnicas envolvidos na atividade de programação de computadores utilizando como ambiente de aprendizado a linguagem Python.</p>
<div class="toctree-wrapper compound">
<p class="caption" role="heading"><span class="caption-text">Tópicos</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/introducao.html">2.1. Introdução</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/tipos-dados.html">2.2. Tipos de Dados</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/tipos-numericos.html">2.3. Tipos Numéricos</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/expressoes.html">2.4. Expressões</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/chamada-funcoes.html">2.5. Chamada de Funções</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/variaveis.html">2.6. Variáveis</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/comentarios.html">2.7. Comentários</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/tipo-logico.html">2.8. Tipo Lógico</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/op-relacionais.html">2.9. Operadores Relacionais</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/expressoes-logicas.html">2.10. Expressões Lógicas</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/estruturas-condicionais.html">2.11. Estruturas Condicionais</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/estruturas-repeticao.html">2.12. Estruturas de Repetição</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/strings.html">2.13. O Tipo String</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/keywords.html">2.14. Palavras-chave</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/sequencias.html">2.15. Sequências</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/dicionarios.html">2.16. Dicionários</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/set.html">2.17. Conjuntos</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/formatacao-strings.html">2.18. Formatação de Strings</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/lacos-for.html">2.19. Laços do tipo for</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/break-continue.html">2.20. Os Comandos <code class="docutils literal notranslate"><span class="pre">break</span></code> e <code class="docutils literal notranslate"><span class="pre">continue</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/comandos-compostos.html">2.21. Comandos Compostos</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/funcoes.html">2.22. Funções</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/escopo.html">2.23. Escopo de Variáveis</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/consideracoes.html">2.24. Considerações Finais</a></li>
<li class="toctree-l1"><a class="reference internal" href="introducao-programacao/exemplos.html">2.25. Exemplos</a></li>
</ul>
</div>
</div>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Rodapé">
<a href="instalacao/jupyterlab.html" class="btn btn-neutral float-left" title="1.4. Instalação do JupyterLab através do Docker" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Anterior</a>
<a href="introducao-programacao/introducao.html" class="btn btn-neutral float-right" title="2.1. Introdução" accesskey="n" rel="next">Próximo <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2020-2022, Gilberto Queiroz and Thales Körting.</p>
</div>
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(false);
});
</script>
<!-- Theme Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-165908761-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-165908761-1', {
'anonymize_ip': false,
});
</script>
</body>
</html>