Skip to content

Commit 2ec8d62

Browse files
cleanup and de-duplication of installation instructions.
1 parent 884b27c commit 2ec8d62

8 files changed

Lines changed: 191 additions & 364 deletions

File tree

‎source/modules/BasicPython.rst‎

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ All of programming is really about manipulating values.
2929
Literals for the Basic Value types:
3030
------------------------------------
3131

32+
A "literal" is something you can put in your code to directly get a value. Python has literals for the key built in types.
33+
3234
Numbers:
3335
- floating point: ``3.4``
3436
- integers: ``456``
3537

3638
Text:
3739
- ``"a bit of text"``
3840
- ``'a bit of text'``
39-
- (either single or double quotes work -- why? If you don't know try looking it up in one of the referenced sources!)
41+
- (either single or double quotes work -- why? If you don't know, try looking it up in one of the referenced sources!)
4042

4143
Boolean values:
4244
- ``True``
@@ -51,15 +53,18 @@ The nothing object:
5153
Code structure
5254
--------------
5355

54-
Each line is a piece of code.
56+
Each line is a unit of code. Each line is made up of these components:
5557

56-
Comments:
58+
**Comments:**
5759

5860
.. code-block:: ipython
5961
6062
In [3]: # everything after a '#' is a comment
6163
62-
Expressions:
64+
65+
**Expressions:**
66+
67+
An expression is a unit of code that evaluates to a value:
6368

6469
.. code-block:: ipython
6570
@@ -69,8 +74,9 @@ Expressions:
6974
Out[5]: 7
7075
7176
72-
Statements:
73-
statements carry out an action, but do not evaluate to a value, that is you can't assign to them (or put them in a lamda, or...)
77+
**Statements:**
78+
79+
statements carry out an action, but do not evaluate to a value, that is, you can't assign to them (or put them in a lamda, or...)
7480

7581
.. code-block:: ipython
7682
@@ -80,9 +86,10 @@ statements carry out an action, but do not evaluate to a value, that is you can'
8086
8187
In [8]: return something
8288
89+
Statements include function (and class) definitions (``def``), loop constructs (``for``, ``while``), code forking constructs (``if``), exception handling (``try``, ``except``), and a handful of other more advanced constructs.
8390

8491

85-
It is somewhat obvious, but handy when playing with code:
92+
The ``print()`` function does what you'd expect, and is very handy when playing with code:
8693

8794
.. code-block:: ipython
8895
@@ -97,7 +104,6 @@ You can print multiple things:
97104
the value is 5
98105
99106
100-
101107
Any Python object can be printed (though it might not be pretty...)
102108

103109
.. code-block:: ipython
@@ -109,9 +115,10 @@ Any Python object can be printed (though it might not be pretty...)
109115
In [2]: print(bar)
110116
<class '__main__.bar'>
111117
118+
Code Blocks
119+
...........
112120

113-
114-
Blocks of code are delimited by a colon and indentation:
121+
Separate blocks of code are delimited by a colon and indentation. Everything indented after a colon is "inside" that block. It can be a function definition, or a loop construct, or a handful of other more advanced constructs.
115122

116123
.. code-block:: python
117124
@@ -170,9 +177,8 @@ But they are not:
170177
for i in range(100):
171178
\tprint i**2
172179
173-
**ALWAYS INDENT WITH 4 SPACES**
174-
175180
181+
.. centered:: **ALWAYS INDENT WITH 4 SPACES**
176182

177183

178184
Make sure your editor is set to use spaces only --
@@ -195,14 +201,38 @@ An *expression* is made up of values and operators.
195201
* Integer vs. float arithmetic
196202

197203
* (Python 3 smooths this out).
198-
* Always use ``/`` when you want division with float results, ``//`` when you want floored (integer) results (no remainder).
204+
* Always use ``/`` when you want division with float results, ``//`` when you want floored (integer) results (no remainder):
205+
199206

200-
* Type conversions.
207+
.. code-block:: ipython
201208
202-
* This is the source of many errors, especially in handling text.
209+
In [1]: 3 / 4
210+
Out[1]: 0.75
203211
204-
* Type errors - checked at run time only.
212+
In [2]: 3 // 4
213+
Out[2]: 0
205214
215+
* Type conversions: You usually need to convert types explicitly:
216+
217+
.. code-block:: ipython
218+
219+
In [4]: 3 * "4"
220+
Out[4]: '444'
221+
222+
In [5]: 3 * int("4")
223+
Out[5]: 12
224+
225+
* Type errors - checked at run time only:
226+
227+
.. code-block:: ipython
228+
229+
In [10]: '3' * '4'
230+
---------------------------------------------------------------
231+
TypeError Traceback (most recent call last)
232+
<ipython-input-10-1e6fdc328f08> in <module>
233+
----> 1 '3' * '4'
234+
235+
TypeError: can't multiply sequence by non-int of type 'str'
206236
207237
Symbols
208238
-------
@@ -213,13 +243,13 @@ Symbols are how we give names to values (objects).
213243
* Symbols must begin with an underscore or letter.
214244
* Symbols can contain any number of underscores, letters and numbers.
215245

216-
* this_is_a_symbol
217-
* this_is_2
218-
* _AsIsThis
219-
* 1butThisIsNot
220-
* nor-is-this
246+
* ``this_is_a_symbol``
247+
* ``this_is_2``
248+
* ``_AsIsThis``
249+
* ``1butThisIsNot``
250+
* ``nor-is-this``
221251

222-
* Symbols don't have a type; values do.
252+
* Symbols (names) don't have a type; values do.
223253

224254
* This is why Python is "Dynamic".
225255

@@ -250,7 +280,7 @@ it is bound.
250280
In [26]: type(a)
251281
Out[26]: float
252282
253-
*wait!* ``a`` has a different type?!? -- yes, because it's the type of the value: "3.14", names don't actually have a type, the same name can refer to any type.
283+
*wait!* ``a`` has a different type?!? -- yes, because it's the type of the value: ``3.1``, names don't actually have a type, the same name can refer to any type.
254284

255285

256286
Assignment
@@ -282,6 +312,7 @@ Evaluating the name will return the value to which it is bound
282312
In [31]: a_float
283313
Out[31]: 3.14
284314
315+
285316
Variables?
286317
----------
287318

@@ -300,6 +331,7 @@ Variables?
300331
* A name can be bound to a value -- but that has nothing to do with a
301332
location in memory.
302333

334+
303335
In-Place Assignment
304336
-------------------
305337

@@ -536,7 +568,7 @@ To check if a name is bound to one of these, you use ``is``:
536568
537569
x is None
538570
539-
Note that in contrast to English -- "is" is asking a question, not making an assertion -- ``a is True`` means "is a set to the value True?"
571+
Note that in contrast to English -- "is" is asking a question, not making an assertion -- ``a is True`` means "is a set to the True object?"
540572

541573

542574
Operator Precedence
@@ -554,6 +586,7 @@ To force statements to be evaluated out of order, use parentheses -- expressions
554586

555587
Python follows the "usual" rules of algebra.
556588

589+
557590
Python Operator Precedence
558591
--------------------------
559592

‎source/modules/Functions.rst‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Defining a function:
3030

3131

3232
Local vs. Global
33-
----------------
33+
................
3434

3535
Names bound in Python have a *scope*
3636

37-
That *scope* determines where a symbol is visible, and what value it has in a
37+
That *scope* determines where a name is visible, and what value it has in a
3838
given block.
3939

4040
.. code-block:: ipython
@@ -64,6 +64,8 @@ No -- they did not. The "y" and "z" names *inside* the function are completely s
6464

6565
The ones outside the function are "global" names.
6666

67+
When you use a name in Python, it first checks if it's a local name. Then, if that name is not in the local scope, it will look in the global scope for it.
68+
6769
**NOTE:** "global" in Python means global to the module (generally a single file), not global to an entire program. Which is really good, as you have little way of knowing what names are being used in packages you are using, but are not writing yourself!
6870

6971
In general, you should use global names mostly for constants.
@@ -81,7 +83,7 @@ This is just a convention, but it's a good one to follow.
8183

8284

8385
Global Gotcha
84-
-------------
86+
.............
8587

8688
Take a look at this function definition:
8789

@@ -128,6 +130,12 @@ the global value already bound. So in the line that caused the error:
128130
Python knows that x is a local name, as it is assigned on the next line. But on this line, x has not yet been given a value -- hence the error.
129131

130132

133+
Globals are "read only"
134+
.......................
135+
136+
While you have access to the global names in side a function, you can't change what those names are bound to. Take a look at the previous examples -- when we set a new value to a name (using the equal sign), that makes the name local -- so it will not change what the global name refers to.
137+
138+
131139
Parameters
132140
----------
133141

@@ -143,6 +151,9 @@ These types of parameters are called *positional*
143151
When you call a function, you **must** provide arguments for all *positional*
144152
parameters *in the order they are listed*.
145153

154+
Defaults for parameters:
155+
........................
156+
146157
You can provide *default values* for parameters in a function definition:
147158

148159
.. code-block:: ipython
@@ -177,6 +188,11 @@ Or, you can use the parameter name as a *keyword* to indicate which you mean:
177188
In [29]: fun(y=4, x=1)
178189
1 4 3
179190
191+
192+
This allows you to specify only those optional parameters that you need to, and keep using the defaults for the rest.
193+
This is a very powerful feature of Python -- you'll find it's common to have a pretty long optional parameter list to functions.
194+
It allows a lot of flexibility (the hard stuff is possible), while in common use, it's easy to use (the easy stuff is easy).
195+
180196
Once you've provided a *keyword* argument in this way, you can no longer
181197
provide any *positional* arguments:
182198

@@ -187,6 +203,7 @@ provide any *positional* arguments:
187203
fun(x=5, 6)
188204
SyntaxError: non-keyword arg after keyword arg
189205
206+
190207
Recursion
191208
---------
192209

0 commit comments

Comments
 (0)