-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page2.html
263 lines (238 loc) · 16.7 KB
/
Page2.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
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html>
<HTML>
<head>
<title> Alfia's notes - Telling computers what to do</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css">
<meta charset="utf-8"/>
</head>
<body>
<div class="large_box">
<div class="large_title">
<h1>Introduction to "Serious" Programming</h1>
</div>
</div>
<div class="large_box">
<div class="lesson_highlights">
<h2>Lesson 1 Hignlights</h2>
<br><a href="#CPPL"><b>I. Computers, programs and programming languages</b></a><br>
<br><a href="#P"><b>II. Python</b></a><br>
<br><a href="#D"><b>III. Debugging</b></a><br>
<br><a href="#WRC"><b> IV. Writing a readable code.</b></a><br>
</div>
<div class="lesson_notes">
<h2>Lesson 1 Notes - Telling computers what to do</h2>
<h3 ID="CPPL"> I. Computers, programs and programming languages</h3>
<br> <b>Computers</b>
<p> Are electronic devices that can store and process data by following instructions given to them in a program.</p>
<br><b>Programs</b>
<p>Computer programs are sequences of steps for the computers to follow. </p>
<br><b>Programming languages</b>
<p>Multiple languages, such as Fortran, Cobol, C++, Python</p>
We do not use natural languages for programming because of
<ol>
<li>Ambiguity</li>
<li>Verbosity</li>
</ol>
<h3 ID="P"> II. Python</h3>
<p>Python is on of programming languages. A Python Interpreter converts the programs written in a Python languate to a set of binary instructions that the computer can understand and execute.</p>
<p>Python grammar for arithmetic expressions:<br>
Expression -> expression operator expression
For example: <br>
Expression -> number (2) operator (+, *,/) number (4)
Parenthesis around expressions indicate priority of this expression.
</p>
<h3 ID="D"> III. Debugging</h3>
<p> Debugging is one of the five ways <a href="index.html""#TLP">programmers think</a>. Python requires code be written with a very specific syntax. This means that small typos can lead to program not running at all.<br>
The trick to dealing with programming mistakes (called "bugs") is not to avoid making them entirely, because it is impossible. The trick is to develop a system for finding and fixing them.<br>
HINT: It's often a good idea to start at the last line of the error message.
</p>
<h3 ID="WRC"> IV. Writing a readable code.</h3>
<ol>
<li> <b> What is a variable?</b></li>
A variable can be assigned a name via an assignment statement:<br>
<p style=justified:center>name = expression</p>
<br>
<li> <b>What does it mean to assign a value to a variable?</b></li>
Assigning value to a variable is putting the value in a memory cell that has a name on it.<br>
<br>
<li> <b>What is the difference between what the equals = means in math versus in programming.</b> What's the difference between this: 2 + 3 = 5 and this: my_variable = 5?</li>
</ol>
</div>
</div>
<div class="large_box">
<div class="lesson_highlights">
<h2>Lesson 2 Hignlights</h2>
<br><a href="#var"><b>I. Variables</b></a><br>
<br><a href="#str"><b>II. Strings</b></a><br>
</div>
<div class="lesson_notes">
<h2>Lesson 2 Notes - Variables and Strings</h2>
<h3 ID="var"> I. Variables </h3>
<p>Values can be assigned to variables, using a <span class="code"> = </span> sign. This eauals sign is different from an arithmetic equals sign, which is Python actually looks like this: <span class="code"> == </span>. For example, the difference between the equals sign means in <span class="code">2 + 3 = 5</span> and <span class="code">my_variable = 5</span> is:
<p>
Here <span class="code">2 + 3 = 5</span>, the equals sign means "is the same as".
</p>
<p>
And here <span class="code">my_variable = 5</span>, the equals sign means "is assigned the value of".
</p>
<p>Variables are a convenient way to store data, they are helpful in remembering where the data are stored (when using mnemonic variable names), and they help to save time when data are operated on.</p>
<br>The values assigned to the variables can be:</p>
<ul>
<li> Strings (a sequence of characters in either single or double quotes, but not mixed). For example, <span class="code">"3"</span> and <span class="code">"3"</span> are strings, so <span class="code">"3"+"3"</span> will result in <span class="code">33</span></li><br>
<li> Integers (round numbers). For example, <span class="code">3</span> and <span class="code">3</span> are integers, so <span class="code">3+3</span> will result in <span class="code">6</span></li><br>
<li> Floats (numbers with decimal points).For example, <span class="code">3.0</span> is a float, <span class="code">3</span> is an integer, so <span class="code">3.0+3</span> will result in <span class="code">6.0</span>. If one of the variables is a float, the result of an arithmetic operation will be a float.</li>
</ul>
<p> Using variables is a way to name values. There are several name rules in Python:</p>
<ul>
<li>You can use any letter</li>
<li>You can use the special characters “_"</li>
<li>You can use any number, provided you do not start with it.</li>
<li>White spaces and signs with special meanings in Python, as “+” and “-” are not allowed.</li>
<li>Python variable names are case-sensitive</li>
<li>A convention is not to start a variable name with a capital charachter (even though this is not a hard rule)</li>
</ul>
<h3 ID="str"> II. Strings </h3>
<p>There is a reason for the strings to have either double or single quotes. Sometimes a meaningful text withing the string can have double quotes: <span class="code">variable='Print"Hello"'</span>. In this case, single quotes are appropriate. The string can also have a single quote within: <span class="code">variable2="It's a nice day!"</span> - in this case double quotes can be used.
In addition, triple quotes can be used for creating multi-line strings:</p>
<span class="code">print """I am a string<br>
that takes up <br>
multiple lines"""<br>
print '''I am also a string<br>
that makes up multiple lines!'''<br> </span>
<ul>
<li> Strings can be operated on. For example an <u>operator</u> "+" will <b>concatenate</b> two or more strings (just paste them one after another). Or "*" will multiply a string (for example name="5"*4, print name will give 5555).</li><br>
<li>Strings can also be indexed, which means we can extract a part of the sequence of the characters. For example string variable ="udacity", variable[0] will return "u", variable[1+1] will return "a" etc. The index value can be a negative one, where -1 is the last character, -2 is the one before the last, etc.</li><br>
<li> Selecting subsequences of strings - instead of having just one number is square brackets, we can have two, separated by a colon. The first number will indicate a start, and the second - a stop indices. The returned value will be a string between these numbers, including the start, and not including the stop number. Leaving an empty space instead of the stop number will include all the charachters from the start to the end. Inversley, leaving out the start number, the selection will include all the characters from the beginning to the stop.</li><br>
<li>Using a "find" method in strings.
<img class="screenshot" src="images/findmethod.jpg" alt = "find method">
A <b>method</b> is a built-in procedure, provided by Python.
When using the .find method, the output is the first number of the search string. If the search string is not found, the output will be -1. To make .find even more powerful, we can add a second "parameter". A <b>parameter</b> is what goes inside the parentheses. In this code: "text".find("t") the "t" is a parameter; in "text".find("t", 1) there are two parameters, "t" and 1. The second parameter (a number) indicates a position in the string, <u>after</u> which the .find will look for a search string </li><br>
</ul>
</div>
</div>
<div class="large_box">
<div class="lesson_highlights">
<h2>Lesson 3 Highlights </h2>
<br><a href="#FD"><b>I. Function Definition</b></a><br>
<br><a href="#MUF"><b>II. Making and Using Functions</b></a><br>
<br><a href="#SF"><b> III. Summary of the functions lesson.</b></a><br>
<br><a href="#AHP"><b> IV. Automate HTML using Python.</b></a><br>
</div>
<div class="lesson_notes">
<h2>Lesson 3 Notes - Input -> Function -> Outputs</h2>
<h3 ID="FD"> I. Function definition</h3>
<p> A <b>function</b> or a <b>procedure</b> is a sequence of actions. A function has a <u>name</u> (type), by which one can "call" the function later. A function also has an expression in parenthesis, which is called the <u>parameters</u> (or argument) of the function. The argument is an <u>input </u> to the function, an it can be either a value, a variable, or another function.
It is common to say that a function “takes” an argument and “returns” a result. The result is called the <u>return value</u>.
Note that a function does not change the value of the used variable, but creates a changed copy of it. So it is important to use a <u> return</u> of the changed value in order not to lose it.
</p>
<p>Functions are very useful tools that allow programmers avoid repetitions and write efficient code.</p>
<h3 ID="MUF"> II. Making and Using Functions.</h3>
<p> To make a function, one starts with <span class="code">def</span>, followed by the name of the function (use mnemonic names, avoid reserved names) and parameters of the function in parenthesis. When the function is called (used), the parameters are replaced by actual values.
</p>
<p> As an example, to create a function that calculates the conversion of inches to cantimeters, we would do the following: </p>
<span class="code">def conver(inch):</span>
<p style=text-indent:50px;><span class="code"> cm=inch*2.54</span><p>
<p style=text-indent:50px;><span class="code">return cm</span></p>
<p> To call a function, write the name with the value in parentheses.</p>
<p style=text-indent:10px;><span class="code"> print conver(4.0)<br>
>>>>10.16</span><p>
<h3 ID="SF"> III. Summary of the functions lesson</h3>
To summurize:
<p><b>On the first line of your function definition.</b></p>
<ul>
<li>You must begin the line with def (lowercase).</li>
<li>After def you must give a function name (in this case it will be udacify.</li>
<li>Next, you must have a set of parentheses with the required parameters inside.</li>
<li>The line must end with a : colon.</li>
</ul>
<p><b>In the body of the function</b></p>
<ul>
<li>Every line in the function must be indented.</li>
<li>If you want your function to produce output, it must end with a return statement.</li>
</ul>
<p><b>To call a function:</b></p>
<ul>
<li>The function name,</li>
<li>followed by...an open paren (, </li>
<li>followed by...the values for the required parameters, </li>
<li>followed by...</li>
<li>...a closed paren ).
<li>To display the results of a function call use print command.</li>
</ul>
<h3 ID="AHP"> IV. Automate HTML using Python</h3>
<img class="screenshot" src="http://i.imgur.com/FF6GuIR.png" alt = "example of HTML-generating Python code">
</div>
</div>
<div class="large_box">
<div class="lesson_highlights">
<h2>Lesson 4 Hignlights</h2>
<br><a href="#MD>"<b>I. Making decisions.</b></a><br>
</div>
<div class="lesson_notes">
<h2>Lesson 4 Notes - Decisions and Repetitions - 'if' and 'while'.</h2>
<h3 ID="MD"> I. Making decisions.</h3>
<p><b>Comparison operators</b>, such as less than, more than, less or equal, more or equal, == (equal), != not equal to, work on numbers. The output is not a number, but a Boolean value, which can be ether "True" or "False". </p>
<img class=screenshot src="images/comparison.jpg" alt = "example of comparison operators in Python">
</div>
</div>
<div class="large_box">
<div class="lesson_highlights">
<h2>Lesson 5 Hignlights</h2>
<br><a href="#SvsL"><b>I. Differences and similarities between strings and lists.</b></a><br>
<br><a href="#Mut"><b>II. Mutation.</b></a><br>
<br><a href="#Ali"><b>III. Aliasing.</b></a><br>
<br><a href="#OLO"><b>IV. Other list operations.</b></a><br>
<br><a href="#LOL"><b>V. Loops on lists.</b></a><br>
</div>
<div class="lesson_notes">
<h2>Lesson 5 Structured Data</h2>
<h3 ID="SvsL"> I. Differences and similarities between strings and lists.</h3>
<p>Strings and lists are similar, though lists are more poweful. Here are where they are similar and different:</p>
<ul>
<li> Both are sequences, though a String is a sequence of characters, while list is a sequence of anything, including strings and other lists.</li>
<li> Both can be assigned to a variable; string - in quotes, list - in square brackets.</li>
<li> Sub-sequences of both can be accessed using an index in square brackets. They return strings and lists, corresponding to the origin</li>
</ul>
<img class=screenshot src="images/strings_and_lists.jpg" alt = "Differences and similarities between strings and lists">
<p> The general grammar for creation of a list is to have a name for a list, assignment sign, square brackets, and the expressions of the list, separated by a comma.</p>
<h3 ID="Mut"> II. Mutation.</h3>
<p>Lists support mutation, while strings do not(they are "immutable"). When we concotinate strings, this does not modify an existing string, but creates a brand new one. Mutation allows to change a selected expression within a list, without creating a new one:</p>
<img class="screenshot" src="images/mutation.jpg" alt="example of mutation">
<h3 ID="Ali"> III. Aliasing.</h3>
<p>When we refer to the same object using different ways, it is called aliasing. For example, we have a variable 'p' that is assigned to a list. If then we assign a variable 'q' to the variable 'p', whatever we do with the list will be reflected in both p and q. However, if we assign p to a new object, the value of q will remain the same, as before.</p>
<h3 ID="OLO"> IV. Other list operations.</h3>
<h4>Append</h4>
<p>Append is like a procedure, but it is a method. It mutates the list after execution. </p>
<img class="screenshot" src="images/append.jpg" alt="Example of how append works">
<h4>Plus (+) operator</h4>
<p>Plus works similar to concotination operation of strings. It produces a new list after execution, and it does not mutate any of the input lists. </p>
<img class="screenshot" src="images/plus.jpg" alt="Example of how a plus works">
<h4>Length (len) operator</h4>
<p>len operator works on any object that is a collection of things, such as strings and lists.</p>
<h4>Index and in on lists</h4>
<img class="screenshot" src="images/index_and_in.jpg" alt="index and in on loops">
<img class="screenshot" src="images/len.jpg" alt="how len works">
<h3 ID="LON"> V. Loops on lists.</h3>
<h4>While on lists.</h4>
<p> While works on lists the same way as on strings. We have a test statement in the block, which will return the the while until the test is no longer true: </p>
<img class="screenshot" src="images/while.jpg" alt= "While on Loops">
<h4>For on lists.</h4>
<p> For in loop on lists goes through each element of the list ('name' introduces a new variable for the list expressions), performing an action, specified in a block of the loop:</p>
<img class="screenshot" src="images/for-in.jpg" alt="For on loops">
</div>
</div>
<div class="large_box">
<div class="lesson_highlights">
<h2>Lesson 2 Hignlights</h2>
<br><a href="#VS"><b>I. Variables and Strings</b></a><br>
<br><a href="#P"><b>II. Python</b></a><br>
<br><a href="#D"><b>III. Debugging</b></a><br>
<br><a href="#WRC"><b> IV. Writing a readable code.</b></a><br>
</div>
<div class="lesson_notes">
<h2>Lesson 1 Notes - Telling computers what to do</h2>
<h3 ID="VS"> I. I. Variables and Strings</h3>
</div>
</div>
</body>
</HTML>