-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec-variable.ptx
223 lines (194 loc) · 6.28 KB
/
sec-variable.ptx
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
<?xml version="1.0" encoding="UTF-8"?>
<!--*****************************************
This is part of Basic Programming
Copyright (C) 2024
Phạm Công Vinh
See the file COPYING for copying conditions.
******************************************-->
<section xml:id="sec-variable" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Variables</title>
<objectives>
<ul>
<li>Learn about variables and how to use them.</li>
<li>Understand how the equal sign <c>=</c> isn't associated with equality in programming.</li>
</ul>
</objectives>
<p>
<term>Variables</term> are one of the most fundamental concepts in programming.
</p>
<p>
In mathematics, we often see the variable <m>x</m> in functions such as: <me>f(x) = x^2 + 2x + 1</me> Here <q>variable</q> means can be changed and is not fixed.
</p>
<definition xml:id="def-variable">
<idx><h>Definitions</h><h>of variables</h></idx>
<statement>
<p>
In programming, <term>variables</term> are used to store data.
</p>
<p>
The data stored inside a variable is called <term>value</term>, which is <em>often updated</em> as new data needs to be stored.
</p>
<p>
To <term>declare</term> a variable is to create a new variable.
</p>
<p>
To <term>assign</term> data to an existing variable is to <term>update</term> its value to that data.
</p>
</statement>
</definition>
<technology>
<title>Declaring and assigning</title>
<idx><h>Syntax</h><h>of declaration</h></idx>
<idx><h>Syntax</h><h>of assignment</h></idx>
<p>
In Python, to declare or assign values to a variable, we use the equal sign <c>=</c>: <me>{\color{blue} \text{name}} = \text{value}</me>
</p>
<p>
Therefore, the equal sign <c>=</c> in programming should <em>never</em> be used for equality.
</p>
</technology>
<p>
For example (using CodeLens is recommended):
</p>
<program language="python" interactive="activecode">
<input>
a = 1
b = a
print(a)
print(b)
# Update variable
a = 2
print(a)
print(b)
</input>
</program>
<problem>
<pre>
1
1
2
1
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>declaration</h></idx>
<idx><h>Code examples</h><h>assignment</h></idx>
<p></p>
<p>
Firstly, we declare the variable <c>a</c> and assign it the value <c>1</c>.
</p>
<p>
Next, we declare and assign <c>b</c> the current value of <c>a</c>, thus <c>b</c> also has the value <c>1</c>.
</p>
<p>
We then use the command <c>print()</c> to access and display their current values in the terminal.
</p>
<p>
Next, we assign <c>a</c> a new value of <c>2</c>, but we do not touch <c>b</c>.
</p>
<aside>
<title>Try It Out</title>
<p>
If after updating <c>a</c>, we want <c>b</c> to again have the same value as <c>a</c>, then we have to update <c>b</c> accordingly.
</p>
</aside>
<p>
<idx><h>Notes</h><h>on equality</h></idx>
<alert>Note</alert>: If at this step you think that the values of <c>a</c> and <c>b</c> should both be <c>2</c>, then you are still associating <c>=</c> with equality, which is not the case for programming.
</p>
<p>
Finally, we use <c>print()</c> again to see the difference.
</p>
</investigation>
<insight>
<idx><h>Insights</h><h>equality</h></idx>
<p>
The above example, though simple, confuses many beginners, especially those with a mathematical brain. Because they see <c>b = a</c>, they think it's an equality that holds true throughout the program's lifecycle. That mindset does <em>not</em> apply to programming.
</p>
</insight>
<note>
<title>Naming a variable</title>
<idx><h>Notes</h><h>on variable names</h></idx>
<idx><h>Conventions</h><h>of variable names</h></idx>
<aside>
<title>Topic(s) you might be interested in:</title>
<p>
<ul>
<li>
<p>
<idx><h>Links</h><h>legal variable names</h></idx>
<url href="https://www.google.com/search?q=what+variable+names+are+allowed+in+Python" visual="google.com/search?q=what+variable+names+are+allowed+in+Python">"what variable names are allowed in Python"</url>
</p>
</li>
<li>
<p>
<idx><h>Links</h><h>Python naming conventions</h></idx>
<url href="https://www.google.com/search?q=variable+naming+conventions+in+Python" visual="google.com/search?q=variable+naming+conventions+in+Python">"variable naming conventions in Python"</url>
</p>
</li>
</ul>
</p>
</aside>
<p>
A variable's name has some specifications. The following names will raise a syntax error:
<ul>
<li>
<c>my var = 5</c> (contains white-spaces)
</li>
<li>
<c>#my_var = 5</c> (contains a special character)
</li>
<li>
<c>1st_var = 5</c> (starts with a number)
</li>
<li>
<c>print = 5</c> (matches a reserved keyword)
</li>
</ul>
</p>
<p>
Avoid those, and you can name a variable whatever you'd like. That's why, depending on the individual, variable names can vary.
<ul>
<li>
house_address
</li>
<li>
houseAddress
</li>
<li>
HouseAddress
</li>
<li>
ha
</li>
<li>
hAddress
</li>
<li>
house_add
</li>
<li>
<etc></etc>
</li>
</ul>
</p>
<p>
In the world of programming, there are different conventions for naming a variable.
</p>
<p>
It's a skill to know how to name a variable appropriately, but this article will not instruct you in that. Variables in our code examples are named in a <em>short and simple</em> fashion.
</p>
</note>
<p>
<cd>
</cd>
</p>
<exploration>
<title>Basic Programming <mdash /> Part 5: Variables</title>
<idx><h>Videos</h><h>part 05</h></idx>
<p>
Coming soon.
</p>
<video youtubeplaylist="PLBLdRr-v59vwnKvmvLtcgmAnsb2K1Ta_M" />
</exploration>
</section>