You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 2ec8d62
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: source/modules/BasicPython.rst
+57-24Lines changed: 57 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,14 +29,16 @@ All of programming is really about manipulating values.
29
29
Literals for the Basic Value types:
30
30
------------------------------------
31
31
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
+
32
34
Numbers:
33
35
- floating point: ``3.4``
34
36
- integers: ``456``
35
37
36
38
Text:
37
39
- ``"a bit of text"``
38
40
- ``'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!)
40
42
41
43
Boolean values:
42
44
- ``True``
@@ -51,15 +53,18 @@ The nothing object:
51
53
Code structure
52
54
--------------
53
55
54
-
Each line is a piece of code.
56
+
Each line is a unit of code. Each line is made up of these components:
55
57
56
-
Comments:
58
+
**Comments:**
57
59
58
60
.. code-block:: ipython
59
61
60
62
In [3]: # everything after a '#' is a comment
61
63
62
-
Expressions:
64
+
65
+
**Expressions:**
66
+
67
+
An expression is a unit of code that evaluates to a value:
63
68
64
69
.. code-block:: ipython
65
70
@@ -69,8 +74,9 @@ Expressions:
69
74
Out[5]: 7
70
75
71
76
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...)
74
80
75
81
.. code-block:: ipython
76
82
@@ -80,9 +86,10 @@ statements carry out an action, but do not evaluate to a value, that is you can'
80
86
81
87
In [8]: return something
82
88
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.
83
90
84
91
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:
86
93
87
94
.. code-block:: ipython
88
95
@@ -97,7 +104,6 @@ You can print multiple things:
97
104
the value is 5
98
105
99
106
100
-
101
107
Any Python object can be printed (though it might not be pretty...)
102
108
103
109
.. code-block:: ipython
@@ -109,9 +115,10 @@ Any Python object can be printed (though it might not be pretty...)
109
115
In [2]: print(bar)
110
116
<class '__main__.bar'>
111
117
118
+
Code Blocks
119
+
...........
112
120
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.
115
122
116
123
.. code-block:: python
117
124
@@ -170,9 +177,8 @@ But they are not:
170
177
for i inrange(100):
171
178
\tprint i**2
172
179
173
-
**ALWAYS INDENT WITH 4 SPACES**
174
-
175
180
181
+
.. centered:: **ALWAYS INDENT WITH 4 SPACES**
176
182
177
183
178
184
Make sure your editor is set to use spaces only --
@@ -195,14 +201,38 @@ An *expression* is made up of values and operators.
195
201
* Integer vs. float arithmetic
196
202
197
203
* (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
+
199
206
200
-
* Type conversions.
207
+
.. code-block:: ipython
201
208
202
-
* This is the source of many errors, especially in handling text.
209
+
In [1]: 3 / 4
210
+
Out[1]: 0.75
203
211
204
-
* Type errors - checked at run time only.
212
+
In [2]: 3 // 4
213
+
Out[2]: 0
205
214
215
+
* Type conversions: You usually need to convert types explicitly:
TypeError: can't multiply sequence by non-int of type 'str'
206
236
207
237
Symbols
208
238
-------
@@ -213,13 +243,13 @@ Symbols are how we give names to values (objects).
213
243
* Symbols must begin with an underscore or letter.
214
244
* Symbols can contain any number of underscores, letters and numbers.
215
245
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``
221
251
222
-
* Symbols don't have a type; values do.
252
+
* Symbols (names) don't have a type; values do.
223
253
224
254
* This is why Python is "Dynamic".
225
255
@@ -250,7 +280,7 @@ it is bound.
250
280
In [26]: type(a)
251
281
Out[26]: float
252
282
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.
254
284
255
285
256
286
Assignment
@@ -282,6 +312,7 @@ Evaluating the name will return the value to which it is bound
282
312
In [31]: a_float
283
313
Out[31]: 3.14
284
314
315
+
285
316
Variables?
286
317
----------
287
318
@@ -300,6 +331,7 @@ Variables?
300
331
* A name can be bound to a value -- but that has nothing to do with a
301
332
location in memory.
302
333
334
+
303
335
In-Place Assignment
304
336
-------------------
305
337
@@ -536,7 +568,7 @@ To check if a name is bound to one of these, you use ``is``:
536
568
537
569
x isNone
538
570
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?"
540
572
541
573
542
574
Operator Precedence
@@ -554,6 +586,7 @@ To force statements to be evaluated out of order, use parentheses -- expressions
Copy file name to clipboardExpand all lines: source/modules/Functions.rst
+20-3Lines changed: 20 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,11 +30,11 @@ Defining a function:
30
30
31
31
32
32
Local vs. Global
33
-
----------------
33
+
................
34
34
35
35
Names bound in Python have a *scope*
36
36
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
38
38
given block.
39
39
40
40
.. code-block:: ipython
@@ -64,6 +64,8 @@ No -- they did not. The "y" and "z" names *inside* the function are completely s
64
64
65
65
The ones outside the function are "global" names.
66
66
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
+
67
69
**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!
68
70
69
71
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.
81
83
82
84
83
85
Global Gotcha
84
-
-------------
86
+
.............
85
87
86
88
Take a look at this function definition:
87
89
@@ -128,6 +130,12 @@ the global value already bound. So in the line that caused the error:
128
130
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.
129
131
130
132
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
+
131
139
Parameters
132
140
----------
133
141
@@ -143,6 +151,9 @@ These types of parameters are called *positional*
143
151
When you call a function, you **must** provide arguments for all *positional*
144
152
parameters *in the order they are listed*.
145
153
154
+
Defaults for parameters:
155
+
........................
156
+
146
157
You can provide *default values* for parameters in a function definition:
147
158
148
159
.. code-block:: ipython
@@ -177,6 +188,11 @@ Or, you can use the parameter name as a *keyword* to indicate which you mean:
177
188
In [29]: fun(y=4, x=1)
178
189
1 4 3
179
190
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
+
180
196
Once you've provided a *keyword* argument in this way, you can no longer
181
197
provide any *positional* arguments:
182
198
@@ -187,6 +203,7 @@ provide any *positional* arguments:
0 commit comments