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
Copy file name to clipboardExpand all lines: source/exercises/slicing.rst
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,8 @@ Write some functions that take a sequence as an argument, and return a copy of t
20
20
* with the elements reversed (just with slicing).
21
21
* with the last third, then first third, then the middle third in the new order.
22
22
23
+
- Example: ``(1,2,3,4,5,6)`` should return: ``(5,6,1,2,3,4)`` (start with a length that's a multiple of three, but make sure it doesn't crash for other lengths)
24
+
23
25
**NOTE:** These should work with ANY sequence -- but you can use strings to test, if you like.
Copy file name to clipboardExpand all lines: source/modules/Sequences.rst
+56-19Lines changed: 56 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -771,39 +771,75 @@ Other Gotchas
771
771
772
772
Easy container setup, or deadly trap?
773
773
774
-
(note: you can nest lists to make a 2D-ish array)
774
+
Say you want something like sort of like a 2D array -- one way to do that is to nest lists -- make a list of lists.
775
+
776
+
ONe seemingobvious way to create an empty list of lists would be to use multiplcation of lists -- make a list with one list in it, and then multiply it by the number of lists you want:
775
777
776
778
.. code-block:: ipython
777
779
778
-
In [13]: bins = [ [] ] * 5
780
+
In [12]: bins = [ [] ] * 5
779
781
780
-
In [14]: bins
781
-
Out[14]: [[], [], [], [], []]
782
+
In [13]: bins
783
+
Out[13]: [[], [], [], [], []]
782
784
783
-
In [15]: words = ['one', 'three', 'rough', 'sad', 'goof']
785
+
OK -- that worked -- you have a list with five empty lists in it. So let's try using that. This is a very contrived example, but say you have list of words:
784
786
785
-
In [16]: for word in words:
786
-
....: bins[len(word)-1].append(word)
787
-
....:
787
+
.. code-block:: ipython
788
788
789
-
So, what is going to be in ``bins`` now?
789
+
In [14]: words = ['one', 'three', 'rough', 'sad', 'goof']
790
+
791
+
and you want to put one in each of the "inside" lists:
792
+
793
+
.. code-block:: ipython
794
+
795
+
796
+
In [15]: # loop five times
797
+
...: for i in range(5):
798
+
...: # add a word to the corresponding bin
799
+
...: bins[i].append(words[i])
800
+
801
+
So, what is going to be in ``bins`` now? Think for a bit first -- you added one word to each bin, yes? But are those "sublists" independent?
790
802
791
803
There is only **One** bin
792
804
-------------------------
793
805
794
806
.. code-block:: ipython
795
807
796
-
In [65]: bins
797
-
Out[65]:
808
+
In [16]: bins
809
+
Out[16]:
798
810
[['one', 'three', 'rough', 'sad', 'goof'],
799
811
['one', 'three', 'rough', 'sad', 'goof'],
800
812
['one', 'three', 'rough', 'sad', 'goof'],
801
813
['one', 'three', 'rough', 'sad', 'goof'],
802
814
['one', 'three', 'rough', 'sad', 'goof']]
803
815
804
-
We multiplied a sequence containing a single *mutable* object.
816
+
Whoa! So we don't have 5 lists -- we have five *references* to the same list. Remember that in Python you can have any number of names "bound" to any object -- and any object can be contained in any number of containers, or multiple times in one container.
805
817
806
-
We got a list containing five references to a single *mutable* object.
818
+
So when we multiplied a sequence containing a single *mutable* object. We got a list containing five references to a single *mutable* object.
819
+
820
+
Since it's mutable -- you can change it "in place", and when you change it -- the change shows everywhere that list is referenced.
821
+
822
+
So how to make a list of independent lists? You need to loop and call that code that makes an empty list each time in the loop, something like this:
@@ -858,12 +894,13 @@ This will ensure that a new list will be created if one is not passed-in.
858
894
Mutable Sequence Methods
859
895
========================
860
896
861
-
In addition to all the methods supported by sequences we've seen above, mutable sequences (the List), have a number of other methods that are used to change it in place.
897
+
In addition to all the methods supported by sequences we've seen above, mutable sequences (the list), have a number of other methods that are used to change it in place.
862
898
863
899
You can find all these in the Standard Library Documentation:
``a_class_method`` is defined in the ``Classy`` class (see above).
99
+
And it prints the class that it is called on. But despite being defined in ``Classy``, it gets the ``SubClassy`` class object as the first parameter (``cls``).
100
+
So a classmethod will "do the right thing" when used in a subclass.
105
101
106
102
Alternate Constructors
107
103
-----------------------
@@ -123,7 +119,6 @@ keys:
123
119
TypeError: cannot convert dictionary update sequence element #0 to a sequence
124
120
125
121
126
-
127
122
The stock constructor for a dictionary won't work this way. So the dict object
128
123
implements an alternate constructor that *can*.
129
124
@@ -144,8 +139,7 @@ implements an alternate constructor that *can*.
144
139
See also ``datetime.datetime.now()``, etc....
145
140
146
141
147
-
Properties, Static Methods and Class Methods are powerful features of Python's
148
-
OO model.
142
+
Properties, Static Methods and Class Methods are powerful features of Python's OO model.
149
143
150
144
They are implemented using an underlying structure called *descriptors*
Actually, dealing with numbers rather than bytes is big
86
86
87
-
-- but we take that for granted
87
+
-- but we take that for granted.
88
88
89
89
90
90
Mechanics
@@ -98,11 +98,11 @@ Py2 strings were simply sequences of bytes. When text was one per character tha
98
98
Py3 strings (or Unicode strings in py2) are sequences of "platonic characters".
99
99
100
100
It's almost one code point per character -- there are complications
101
-
with combined characters: accents, etc -- but we can mostly ignore those -- you will get far thiking of a code point as a character.
101
+
with combined characters: accents, etc -- but we can mostly ignore those -- you will get far thinking of a code point as a character.
102
102
103
103
Platonic characters cannot be written to disk or network!
104
104
105
-
(ANSI: one character == one byte -- so easy!)
105
+
(ANSI: one character == one byte -- it was so easy!)
106
106
107
107
108
108
Strings vs Unicode
@@ -132,7 +132,7 @@ And two ways to work with binary data:
132
132
py3 is more clear:
133
133
134
134
``str`` for text
135
-
``byte`` for binary data
135
+
``bytes`` for binary data
136
136
137
137
Unicode
138
138
--------
@@ -154,12 +154,12 @@ If you need to deal with the actual bytes for some reason, you may need to conve
154
154
155
155
And can get even more confusing with py2 strings being *both* text and bytes!
156
156
157
-
This is actually one of the biggest differences between Python 2 and Python 3. As an ordinary user (particularly one that used English...), you may not notice -- text is text, and things generally "just work", but under the hood it is very different, and folks writing libraries for things like Internet protocols struggle with the differences.
157
+
This is actually one of the biggest differences between Python 2 and Python 3. As an ordinary user (particularly one that used English...), you may not notice -- text is text, and things generally "just work", but under the hood it is very different, and folks writting libraries for things like Internet protocols struggled with the differences.
158
158
159
159
Using Unicode in Py2
160
-
---------------------
160
+
--------------------
161
161
162
-
If you do need to write Python2 code, you really should use Unicode.
162
+
If you do need to write Python2 code, you really should use Unicode. If you don't -- skip ahead to "Unicode Literals".
163
163
164
164
Here are the basics:
165
165
@@ -214,7 +214,9 @@ Encoding and Decoding
214
214
215
215
216
216
Unicode Literals
217
-
------------------
217
+
----------------
218
+
219
+
How do you get text that isn't plain English text?
218
220
219
221
1) Use Unicode in your source files:
220
222
@@ -226,6 +228,8 @@ Unicode Literals
226
228
227
229
2) Escape the Unicode characters:
228
230
231
+
Either by its hexadecimal value, or its name:
232
+
229
233
.. code-block:: python
230
234
231
235
printu"The integral sign: \u222B"
@@ -239,7 +243,7 @@ One example: http://inamidst.com/stuff/unidata/
239
243
240
244
241
245
Using Unicode
242
-
--------------
246
+
-------------
243
247
244
248
Use ``unicode`` objects in all your code
245
249
@@ -251,14 +255,17 @@ Many packages do this for you: *XML processing, databases, ...*
251
255
252
256
**Gotcha:**
253
257
254
-
Python has a default encoding (usually ascii)
258
+
Python has a default encoding. On Mac and Unix systems, it's usually utf-8 -- Windows? not nearly as consistent.
255
259
256
260
.. code-block:: ipython
257
261
258
-
In [2]: sys.getdefaultencoding()
259
-
Out[2]: 'ascii'
262
+
In [7]: sys.getdefaultencoding()
263
+
Out[7]: 'utf-8'
264
+
265
+
Try this on your machine, and see what you get.
260
266
261
-
The default encoding will get used in unexpected places!
267
+
The default encoding will get used in unexpected places!
268
+
Notably in text files by default.
262
269
263
270
Using Unicode Everywhere
264
271
-------------------------
@@ -329,12 +336,13 @@ UTF-16
329
336
330
337
Kind of like UTF-8, except it uses at least 16bits (2 bytes) for each character: NOT ASCII compatible.
331
338
332
-
But is still needs more than two bytes for some code points, so you still can't process it as two bytes per character.
339
+
But it still needs more than two bytes for some code points, so you still can't simply process it as two bytes per character.
333
340
334
-
In C/C++ held in a "wide char" or "wide string".
341
+
In C/C++, it is held in a "wide char" or "wide string".
335
342
336
343
MS Windows uses UTF-16, as does (I think) Java.
337
344
345
+
338
346
UTF-16 criticism
339
347
-----------------
340
348
@@ -362,7 +370,7 @@ A 1-byte per char encoding.
362
370
363
371
* The most common one-byte per char encoding for European text.
364
372
365
-
* Nice property -- every byte value from 0 to 255 is a valid character ( at least in Python )
373
+
* Nice property -- every byte value from 1 to 255 is a valid character ( at least in Python )
366
374
367
375
* You will never get an UnicodeDecodeError if you try to decode arbitrary bytes with latin-1.
(by the way, the recent implementations are very efficient...)
457
463
458
464
So you can pretty much ignore encodings and all that for most basic text processing.
459
-
If you do find yourself needing to deal with binary data, you ay need to encode/decode stuff yourself. IN which case, Python provides an ``.encode()`` method on strings that encode the string to a bytes object with the encoding you select:
465
+
If you do find yourself needing to deal with binary data, you ay need to encode/decode stuff yourself.
466
+
In which case, Python provides an ``.encode()`` method on strings that encode the string to a bytes object with the encoding you select:
460
467
461
468
.. code-block:: ipython
462
469
@@ -477,7 +484,7 @@ It's all quite simple an robust.
477
484
478
485
But there are a couple key points to remember:
479
486
480
-
* The primary people struggling were those that wrote (or wored with) libraries that had to deal with protocols that used both binary and text data in the same data stream.
487
+
* The primary people struggling were those that wrote (or worked with) libraries that had to deal with protocols that used both binary and text data in the same data stream.
481
488
482
489
* As of Python 3.4 or so, the python string object had grown the features it needed to support even those ugly binary+text use cases.
0 commit comments