forked from OpenTechSchool/python-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.html
More file actions
282 lines (245 loc) · 16.2 KB
/
variables.html
File metadata and controls
282 lines (245 loc) · 16.2 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Переменные — Introduction to Programming with Python</title>
<link rel="stylesheet" href="_static/basic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-3.3.4/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-3.3.4/css/bootstrap-theme.min.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2015.10.02',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/translations.js"></script>
<script type="text/javascript" src="_static/spoilers.js"></script>
<script type="text/javascript" src="_static/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="_static/js/jquery-fix.js"></script>
<script type="text/javascript" src="_static/bootstrap-3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="_static/bootstrap-sphinx.js"></script>
<link rel="copyright" title="Авторские права" href="copyright.html" />
<link rel="top" title="Introduction to Programming with Python" href="index.html" />
<link rel="next" title="Циклы" href="loops.html" />
<link rel="prev" title="Простое рисование с помощью черепашки" href="simple_drawing.html" />
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1'>
<meta name="apple-mobile-web-app-capable" content="yes">
</head>
<body role="document">
<div id="navbar" class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">
Python for Beginners</a>
<span class="navbar-text navbar-version pull-left"><b>2015.10.02</b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li class="dropdown globaltoc-container">
<a role="button"
id="dLabelGlobalToc"
data-toggle="dropdown"
data-target="#"
href="index.html">Material <b class="caret"></b></a>
<ul class="dropdown-menu globaltoc"
role="menu"
aria-labelledby="dLabelGlobalToc"><ul class="current">
<li class="toctree-l1"><a class="reference internal" href="getting_started.html">Приступая к работе</a></li>
<li class="toctree-l1"><a class="reference internal" href="simple_drawing.html">Простое рисование с помощью черепашки</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Переменные</a></li>
<li class="toctree-l1"><a class="reference internal" href="loops.html">Циклы</a></li>
<li class="toctree-l1"><a class="reference internal" href="functions.html">Функции, определяемые пользователем</a></li>
<li class="toctree-l1"><a class="reference internal" href="functions_parameters.html">Функции с параметрами</a></li>
<li class="toctree-l1"><a class="reference internal" href="conditionals.html">Условные операторы</a></li>
<li class="toctree-l1"><a class="reference internal" href="conditional_loops.html">Условные циклы</a></li>
<li class="toctree-l1"><a class="reference internal" href="logical_operators.html">Логические операторы</a></li>
<li class="toctree-l1"><a class="reference internal" href="where_to_go.html">Что дальше?</a></li>
<li class="toctree-l1"><a class="reference internal" href="copyright.html">Лицензия</a></li>
</ul>
</ul>
</li>
<li class="dropdown">
<a role="button"
id="dLabelLocalToc"
data-toggle="dropdown"
data-target="#"
href="#">Chapter <b class="caret"></b></a>
<ul class="dropdown-menu localtoc"
role="menu"
aria-labelledby="dLabelLocalToc"><ul>
<li><a class="reference internal" href="#">Переменные</a><ul>
<li><a class="reference internal" href="#introduction">Введение</a></li>
<li><a class="reference internal" href="#a-variable-called-angle">Переменная, зовущаяся углом</a><ul>
<li><a class="reference internal" href="#exercise">Упражнение</a></li>
<li><a class="reference internal" href="#solution">Решение</a></li>
<li><a class="reference internal" href="#bonus">Дополнительно</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-house-of-santa-claus">Домик Деда Мороза</a><ul>
<li><a class="reference internal" href="#id1">Упражнение</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</li>
<li class="navbar-rel">
<a href="simple_drawing.html" title="Previous Chapter: Простое рисование с помощью черепашки">
<span class="glyphicon glyphicon-step-backward"></span>
<span>Previous</span>
</a>
</li>
<li class="navbar-rel">
<a href="loops.html" title="Next Chapter: Циклы">
<span class="glyphicon glyphicon-step-forward"></span>
<span>Next</span>
</a>
</li>
<li class="dropdown">
<a role="button"
data-toggle="dropdown"
data-target="#"
href="#">Languages <b class="caret"></b></a>
<ul class="dropdown-menu"
role="menu">
<li><a href="../en/index.html">English</a></li>
<li><a href="../de/index.html">Deutsch (German)</a></li>
<li><a href="../es_CL/index.html">Español (Spanish)</a></li>
<li><a href="../ru/index.html">русский (Russian)</a></li>
<li><a href="../ko/index.html">한국인 (Korean)</a></li>
<li><a href="../ro/index.html">Română (Romanian)</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section" id="variables">
<h1>Переменные<a class="headerlink" href="#variables" title="Ссылка на этот заголовок">¶</a></h1>
<div class="section" id="introduction">
<h2>Введение<a class="headerlink" href="#introduction" title="Ссылка на этот заголовок">¶</a></h2>
<p>Хорошо. Экспериментируя в прошлом уроке, нам приходилось редактировать код в нескольких местах, чтобы изменить размеры наших геометрических фигур. Но представь, что будет, если мы захотим изменить размер квадрата не один раз, не говоря уже о прямоугольниках! Есть способ получше.</p>
<p>Это <strong>переменные</strong>. Можно сообщить Python’у, что отныне при обращении к переменной имеется в виду что-то другое. Это похоже на символическую математику, где можно написать: <em>Пусть x равняется 5.</em> Тогда. очевидно, <em>x * 2</em> будет равняться 10.</p>
<p>С помощью Python’а сиё можно записать так:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
</pre></div>
</div>
<p>После этого, если ты выполнишь <code class="docutils literal"><span class="pre">print(x)</span></code>, будет выведено значение переменной — 5. x можно задействовать и в обращении к черепашке:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<p>Переменные могут быть чем угодно, не только числами. Типичной вещью, хранящейся в переменной, является <strong>строка</strong> - строка текста. Строки можно распознать по двойным кавычкам, с которых они начинаются и которыми они заканчиваются. Позже ты узнаешь больше об этом и других типах данных, их применении, и о том, как они называются в Python.</p>
<p>Ты можешь использовать переменную даже для того, чтобы дать черепашке имя:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">timmy</span> <span class="o">=</span> <span class="n">turtle</span>
</pre></div>
</div>
<p>Теперь, когда ты вводишь <code class="docutils literal"><span class="pre">timmy</span></code>, это будет означать <code class="docutils literal"><span class="pre">turtle</span></code>. Но ты всё ещё можешь продолжать использовать и непосредственно <code class="docutils literal"><span class="pre">turtle</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">timmy</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="n">timmy</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="a-variable-called-angle">
<h2>Переменная, зовущаяся углом<a class="headerlink" href="#a-variable-called-angle" title="Ссылка на этот заголовок">¶</a></h2>
<div class="section" id="exercise">
<h3>Упражнение<a class="headerlink" href="#exercise" title="Ссылка на этот заголовок">¶</a></h3>
<p>Как мы можем ускорить работу с нашей программой для рисования накренившихся квадратов, имея переменную <code class="docutils literal"><span class="pre">angle</span></code>?</p>
</div>
<div class="solution section" id="solution">
<h3>Решение<a class="headerlink" href="#solution" title="Ссылка на этот заголовок">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="n">angle</span> <span class="o">=</span> <span class="mi">20</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
</pre></div>
</div>
<p>... и так далее.</p>
</div>
<div class="section" id="bonus">
<h3>Дополнительно<a class="headerlink" href="#bonus" title="Ссылка на этот заголовок">¶</a></h3>
<p>Можешь ли ты проделать то же самое с размером квадрата?</p>
</div>
</div>
<div class="section" id="the-house-of-santa-claus">
<h2>Домик Деда Мороза<a class="headerlink" href="#the-house-of-santa-claus" title="Ссылка на этот заголовок">¶</a></h2>
<div class="section" id="id1">
<h3>Упражнение<a class="headerlink" href="#id1" title="Ссылка на этот заголовок">¶</a></h3>
<p>Нарисуй домик.</p>
<img alt="_images/house.png" src="_images/house.png" />
<p>You can calculate the length of the diagonal line with the Pythagorean
theorem. That value is a good candidate to store in a variable. To calculate
the square root of a number in Python, you’ll need to import the <em>math</em> module
and use the <code class="docutils literal"><span class="pre">math.sqrt()</span></code> function. The square of a number is calculated
with the <code class="docutils literal"><span class="pre">**</span></code> operator:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">math</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="pull-right">
<a href="#">Back to top</a>
<br/>
<ul id="sourcelink" class="list-inline">
<li>
<a href="https://github.com/opentechschool/python-beginners/edit/master/source/variables.rst" title="variables.rst">
Edit on Github
</a>
</li>
<li>
<a href="https://github.com/OpenTechSchool/python-beginners/issues/new?title=Problem+with+variables">
Report a problem
</a>
</li>
</ul>
</p>
<p>
© <a href="copyright.html">Авторские права</a> 2012–2014, OpenTechSchool and contributors.<br/>
Создано с помощью <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.<br/>
</p>
</div>
</footer>
</body>
</html>