-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.html
More file actions
executable file
·363 lines (345 loc) · 15 KB
/
Copy pathvariables.html
File metadata and controls
executable file
·363 lines (345 loc) · 15 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description" content="Learn variables and data types with interactive Python programming practice questions and examples." />
<meta name="keywords" content="python programming, variables, data types, practice questions, coding exercises, python tutorial, string, integer, float" />
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"
rel="stylesheet"
/>
<title>Programming Practice - Variables</title>
<!-- external CSS link -->
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div>
<header>
<h1>Programming practice</h1>
</header>
<main>
<div class="page-header">
<a href="index.html" class="btn btn--nav btn--nav-left" title="Previous page (Input & Output)">‹</a>
<h2 class="page-title">Variables & Casting</h2>
<a href="selection.html" class="btn btn--nav btn--nav-right" title="Next page (Selection)">›</a>
</div>
<nav>
<a href="#" class="btn" id="background">Background</a>
<a href="#" class="btn" id="inspire">Inspire me!</a>
<a href="#" class="btn" id="help">Help me!</a>
</nav>
<section class="background hidden">
<div class="tab-container">
<div class="tab-buttons">
<button class="tab-button active" data-tab="intro">
Introduction
</button>
<button class="tab-button" data-tab="data-types">
Data Types
</button>
<button class="tab-button" data-tab="input">Input</button>
<button class="tab-button" data-tab="casting">Casting</button>
<button class="tab-button" data-tab="math-operators">
Math Operators
</button>
<button class="tab-button" data-tab="updating-variables">
Updating Variables
</button>
<button class="tab-button" data-tab="together">
Putting It All Together
</button>
</div>
<div class="tab-content active" id="intro">
<h1>Variables and Data Types in Python</h1>
<p>
Like you learnt last lesson, variables are like boxes that can hold different types of
data. Let's explore some of the most common ones.
</p>
</div>
<div class="tab-content" id="data-types">
<h2>Data Types: Different Flavours of Information</h2>
<p>
Just like ice cream comes in different flavours, data in Python
comes in different types:
</p>
<table class="summary-table">
<thead>
<tr>
<th>Data types</th>
<th>Meaning</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>Strings</td>
<td>Alphanumeric characters - text wrapped in quotes</td>
<td><code>"Hello, Python!"</code></td>
</tr>
<tr>
<td>Integers</td>
<td>Whole numbers</td>
<td><code>42</code> or <code>-10</code></td>
</tr>
<tr>
<td>Floats</td>
<td>Numbers with decimal points</td>
<td><code>3.14</code> or <code>-0.5</code></td>
</tr>
<tr>
<td>Boolean</td>
<td>True or False values</td>
<td><code>True</code> or <code>False</code></td>
</tr>
</tbody>
</table>
</div>
<div class="tab-content" id="input">
<h2>Input: Getting Information from Users</h2>
<p>
Remember the <code>input()</code> function? It's super helpful,
but it has a secret: it always gives us a <span class="highlight">string</span>, even if the
user has entered a number!
</p>
<div class="code-example">
<code>age = input("How old are you? ")</code>
</div>
<p>
For example, if the user enters 12, the variable <code>age</code> will hold the
string <code>"12"</code>, not the integer <code>12</code>.
</p>
</div>
<div class="tab-content" id="casting">
<h2>Casting: Changing Data Types</h2>
<p>
Sometimes we need to change the type of our data e.g. from a string to an integer. We can do this
with <span class="highlight">casting</span>.
</p>
<div class="code-example">
<code>age = int(input("How old are you? "))</code>
</div>
<p>
Now if the user enters 12, the variable <code>age</code> will hold
the integer <code>12</code>, which we can use for example in maths.
</p>
<span class="background-plus">
<p>
If we want to output the value, <code>print()</code> expects <code>age</code> to be a string. This means we need to use
<code>str(age)</code> to cast <code>12</code>back to the string <code>"12"</code> for us to output it.
</p>
<div class="code-example">
<code><div>print("You are " + str(age) + " years old.")</div>
<div>print("Next year you will be " + str(age + 1) + " years old.")</div></code>
</div>
</span>
</div>
<div class="tab-content" id="math-operators">
<h2>Mathematical Operators: Doing Maths with Python</h2>
<p>
Python is great at maths. Here are some <span class="highlight">operators</span> (symbols) you can use:
</p>
<table class="summary-table">
<thead>
<tr>
<th>Operator</th>
<th>Function</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>+</code></td>
<td>addition</td>
</tr>
<tr>
<td><code>-</code></td>
<td>subtraction</td>
</tr>
<tr>
<td><code>*</code></td>
<td>multiplication</td>
</tr>
<tr>
<td><code>/</code></td>
<td>division</td>
</tr>
</tbody>
</table>
</div>
<div class="tab-content" id="updating-variables">
<h2>Updating Variables: Changing Values</h2>
<p>
Variables are not only for storing values - we can also <span class="highlight">change
them</span> as our program runs. They can vary. This is super useful for keeping track
of data that changes, like scores in a game.
</p>
<p>Here's how we can update a variable:</p>
<div class="code-example">
<code>
<div>num = 4</div>
<div># num is 4</div>
<div>num = num + 2</div>
<div># now num is 6</div>
</code>
</div>
<p>
In this example, <code>num</code> starts as 4. Then we add 2 to
it and store the result back in <code>num</code>. So
<code>num</code> becomes 6!
</p>
<p>
We can do this with any mathematical operation. Here are a few
more examples:
</p>
<div class="code-example">
<code>
num = num * 3 # Multiplies num by 3 <br />
num = num - 1 # Subtracts 1 from num <br />
num = num / 2 # Divides num by 2 <br />
</code>
</div>
<p>
This is really powerful because it lets our variables change as our program runs. Try using them in your next program!
</p>
</div>
<div class="tab-content" id="together">
<h2>Putting It All Together</h2>
<p>Let's use everything we've learned to make a program:</p>
<div class="code-example">
<code>
days = int(input("Give me a number of days: ")) <br />
hours = days * 24 <br />
<span class="background-comma">print("There are", hours, "hours in", days, "days") <br /></span>
<span class="background-plus">print("There are " + str(hours) + " hours in " + str(days) + " days") <br /></span>
</code>
</div>
<p>
This program:
<ol>
<li>Asks the user to <span class="highlight">input</span> a number of days,</li>
<li><span class="highlight">Casts</span> the response to an integer,</li>
<li><span class="highlight">Calculates</span> the number of hours,</li>
<li><span class="highlight">Outputs</span> a final message with the numbers concatenated in.</li>
</ol>
<span class="background-comma">Note - It uses commas to concatenate the strings and variables.</span>
<span class="background-plus">Note - It uses plus signs to concatenate the strings and variables. It used casting to convert the integers back to strings.</span>
</p>
<p>
Now it's your turn! Can you make a program that asks for
someone's age and then calculates how many months they've been
alive? (Hint: multiply their age by 12!) Press <a href="#" class="inspire-link">inspire me!</a> for
more ideas of what to code.
</p>
</div>
</div>
</section>
<section class="example hidden">
<h3 class="question-header">
<button id="prevQuestion" class="btn btn--nav btn--nav-left" title="Previous question (← arrow key)">‹</button>
<span class="question-text">
Write a program to...
<span id="topic"></span>
</span>
<button id="nextQuestion" class="btn btn--nav btn--nav-right" title="Next question (→ arrow key)">›</button>
</h3>
<section class="cartoon1">
<img id="questionImage1" src="img/baseImage.webp" alt="" />
<p id="exampleQuestion"></p>
<p id="exampleAnswer"></p>
<p id="exampleResponse"></p>
</section>
<section class="cartoon2">
<img id="questionImage2" src="img/baseImageTwoQs.webp" alt="" />
<p id="exampleQuestion1"></p>
<p id="exampleQuestion2"></p>
<p id="exampleAnswer1"></p>
<p id="exampleAnswer2"></p>
<p id="exampleResponse2"></p>
</section>
<section class="code hidden">
<button id="copyMe" class="btn btn--small btn--copy" pre="Click to copy code">
📋
</button>
<p id="codeQuestion1"></p>
<p id="codeQuestion2"></p>
<p id="codeCalculation"></p>
<p id="response"></p>
</section>
</section>
<div class="concatenation-toggle">
<label class="toggle-label">
<span class="toggle-text">Concatenation Mode:</span>
<div class="toggle-modes">
<span class="mode-comma">Comma (,)</span>
<input type="checkbox" id="concatToggle" class="toggle-checkbox">
<span class="toggle-slider"></span>
<span class="mode-plus">Plus (+)</span>
</div>
</label>
</div>
</main>
<button id="forceSpongebob" class="btn small-btn" pre="Spongebob">
🧽
</button>
<button id="keyboardShortcuts" class="btn small-btn" pre="Keyboard Shortcuts">
⌨️
</button>
<button id="themeToggle" class="btn small-btn" pre="Toggle Light/Dark Mode">
🌙
</button>
<!-- Keyboard Shortcuts Modal -->
<dialog id="shortcutsModal" class="shortcuts-modal">
<div class="modal-content">
<div class="modal-header">
<h2>Keyboard Shortcuts</h2>
<button class="modal-close" aria-label="Close modal">×</button>
</div>
<div class="modal-body">
<div class="shortcut-section">
<h3>Global Shortcuts</h3>
<div class="shortcut-item">
<kbd>i</kbd>
<span>Inspire me (generate random question)</span>
</div>
<div class="shortcut-item">
<kbd>b</kbd>
<span>Toggle background information</span>
</div>
<div class="shortcut-item">
<kbd>t</kbd>
<span>Toggle concatenation mode (comma/plus)</span>
</div>
<div class="shortcut-item">
<kbd>k</kbd>
<span>Show keyboard shortcuts</span>
</div>
</div>
<div class="shortcut-section">
<h3>When Question is Showing</h3>
<div class="shortcut-item">
<kbd>←</kbd>
<span>Previous question</span>
</div>
<div class="shortcut-item">
<kbd>→</kbd>
<span>Next question</span>
</div>
<div class="shortcut-item">
<kbd>h</kbd>
<span>Show/hide code help</span>
</div>
<div class="shortcut-item">
<kbd>c</kbd>
<span>Copy code to clipboard</span>
</div>
</div>
</div>
</div>
</dialog>
</div>
<script type="module" src="js/variables.js"></script>
</body>
</html>