Skip to content

Commit fee198b

Browse files
committed
a little more explaination for slicing lab.
1 parent 1245eb9 commit fee198b

File tree

6 files changed

+164
-88
lines changed

6 files changed

+164
-88
lines changed

source/exercises/slicing.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Write some functions that take a sequence as an argument, and return a copy of t
2020
* with the elements reversed (just with slicing).
2121
* with the last third, then first third, then the middle third in the new order.
2222

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+
2325
**NOTE:** These should work with ANY sequence -- but you can use strings to test, if you like.
2426

2527
Your functions should look like:

source/modules/Sequences.rst

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -771,39 +771,75 @@ Other Gotchas
771771

772772
Easy container setup, or deadly trap?
773773

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:
775777

776778
.. code-block:: ipython
777779
778-
In [13]: bins = [ [] ] * 5
780+
In [12]: bins = [ [] ] * 5
779781
780-
In [14]: bins
781-
Out[14]: [[], [], [], [], []]
782+
In [13]: bins
783+
Out[13]: [[], [], [], [], []]
782784
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:
784786

785-
In [16]: for word in words:
786-
....: bins[len(word)-1].append(word)
787-
....:
787+
.. code-block:: ipython
788788
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?
790802

791803
There is only **One** bin
792804
-------------------------
793805

794806
.. code-block:: ipython
795807
796-
In [65]: bins
797-
Out[65]:
808+
In [16]: bins
809+
Out[16]:
798810
[['one', 'three', 'rough', 'sad', 'goof'],
799811
['one', 'three', 'rough', 'sad', 'goof'],
800812
['one', 'three', 'rough', 'sad', 'goof'],
801813
['one', 'three', 'rough', 'sad', 'goof'],
802814
['one', 'three', 'rough', 'sad', 'goof']]
803815
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.
805817

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:
823+
824+
.. code-block:: ipython
825+
826+
In [21]: bins = []
827+
828+
In [22]: for i in range(5):
829+
...: bins.append([])
830+
...:
831+
832+
In [23]: bins
833+
Out[23]: [[], [], [], [], []]
834+
835+
In [24]: # loop five times
836+
...: for i in range(5):
837+
...: # add a word to the corresponding bin
838+
...: bins[i].append(words[i])
839+
...:
840+
841+
In [25]: bins
842+
Out[25]: [['one'], ['three'], ['rough'], ['sad'], ['goof']]
807843
808844
809845
Mutable Default Argument
@@ -858,12 +894,13 @@ This will ensure that a new list will be created if one is not passed-in.
858894
Mutable Sequence Methods
859895
========================
860896

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.
862898

863899
You can find all these in the Standard Library Documentation:
864900

865901
https://docs.python.org/3/library/stdtypes.html#typesseq-mutable
866902

903+
867904
Assignment
868905
-----------
869906

@@ -913,11 +950,11 @@ You can pass any sequence to ``.extend()``:
913950
['beans', 'spam', 'eggs', 'ham', 'sushi', 'bread', 'water',
914951
's', 'p', 'a', 'g', 'h', 'e', 't', 't', 'i']
915952
916-
So be careful -- a string is a single object --but also a sequence of characters.
953+
So be careful -- a string is a single object -- but also a sequence of characters (technically a sequence of length-1 strings).
917954

918955

919-
Shrinking the List
920-
------------------
956+
Shrinking a list
957+
----------------
921958

922959
``.pop()``, ``.remove()``
923960

@@ -1130,8 +1167,8 @@ Otherwise ... taste and convention.
11301167
Convention
11311168
----------
11321169

1133-
Lists are homogeneous collections:
1134-
-- they alway contain values of the same type
1170+
Lists are typically homogeneous collections:
1171+
-- they always contain values of the same type
11351172
-- they simplify iterating, sorting, etc
11361173

11371174
Tuples are mixed types:

source/modules/StaticAndClassMethods.rst

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ Static and Class Methods
88
You've seen how methods of a class are *bound* to an instance when it is
99
created.
1010

11-
And you've seen how the argument ``self`` is then automatically passed to
12-
the method when it is called.
11+
And you've seen how the argument ``self`` is then automatically passed to the method when it is called.
1312

1413
And you've seen how you can call *unbound* methods on a class object so
1514
long as you pass an instance of that class as the first argument.
@@ -36,9 +35,6 @@ A *static method* is a method that doesn't get self:
3635
Out[37]: 9
3736
3837
39-
.. [demo: :download:`static_method.py <../../Examples/Session08/static_method.py>`]
40-
41-
4238
Where are static methods useful?
4339

4440
Usually they aren't. It is often better just to write a module-level function.
@@ -80,9 +76,6 @@ argument
8076
Out[42]: 16
8177
8278
83-
.. [demo: :download:`class_method.py <../../Examples/Session08/class_method.py>`]
84-
85-
8679
Why?
8780
----
8881

@@ -102,6 +95,9 @@ Consider this:
10295
in a class method: <class '__main__.SubClassy'>
10396
Out[45]: 64
10497
98+
``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.
105101

106102
Alternate Constructors
107103
-----------------------
@@ -123,7 +119,6 @@ keys:
123119
TypeError: cannot convert dictionary update sequence element #0 to a sequence
124120
125121
126-
127122
The stock constructor for a dictionary won't work this way. So the dict object
128123
implements an alternate constructor that *can*.
129124

@@ -144,8 +139,7 @@ implements an alternate constructor that *can*.
144139
See also ``datetime.datetime.now()``, etc....
145140

146141

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.
149143

150144
They are implemented using an underlying structure called *descriptors*
151145

source/modules/Unicode.rst

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ What the heck is Unicode anyway?
2626
* First there was chaos...
2727

2828
* Different machines used different encodings -- different ways of mapping
29-
binary data that the computer stores to letters.
29+
the binary data that the computer stores to letters.
3030

3131
* Then there was ASCII -- and all was good (7 bit), 127 characters
3232

@@ -84,7 +84,7 @@ http://www.joelonsoftware.com/articles/Unicode.html
8484

8585
Actually, dealing with numbers rather than bytes is big
8686

87-
-- but we take that for granted
87+
-- but we take that for granted.
8888

8989

9090
Mechanics
@@ -98,11 +98,11 @@ Py2 strings were simply sequences of bytes. When text was one per character tha
9898
Py3 strings (or Unicode strings in py2) are sequences of "platonic characters".
9999

100100
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.
102102

103103
Platonic characters cannot be written to disk or network!
104104

105-
(ANSI: one character == one byte -- so easy!)
105+
(ANSI: one character == one byte -- it was so easy!)
106106

107107

108108
Strings vs Unicode
@@ -132,7 +132,7 @@ And two ways to work with binary data:
132132
py3 is more clear:
133133

134134
``str`` for text
135-
``byte`` for binary data
135+
``bytes`` for binary data
136136

137137
Unicode
138138
--------
@@ -154,12 +154,12 @@ If you need to deal with the actual bytes for some reason, you may need to conve
154154

155155
And can get even more confusing with py2 strings being *both* text and bytes!
156156

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.
158158

159159
Using Unicode in Py2
160-
---------------------
160+
--------------------
161161

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".
163163

164164
Here are the basics:
165165

@@ -214,7 +214,9 @@ Encoding and Decoding
214214
215215
216216
Unicode Literals
217-
------------------
217+
----------------
218+
219+
How do you get text that isn't plain English text?
218220

219221
1) Use Unicode in your source files:
220222

@@ -226,6 +228,8 @@ Unicode Literals
226228

227229
2) Escape the Unicode characters:
228230

231+
Either by its hexadecimal value, or its name:
232+
229233
.. code-block:: python
230234
231235
print u"The integral sign: \u222B"
@@ -239,7 +243,7 @@ One example: http://inamidst.com/stuff/unidata/
239243

240244

241245
Using Unicode
242-
--------------
246+
-------------
243247

244248
Use ``unicode`` objects in all your code
245249

@@ -251,14 +255,17 @@ Many packages do this for you: *XML processing, databases, ...*
251255

252256
**Gotcha:**
253257

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.
255259

256260
.. code-block:: ipython
257261
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.
260266

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.
262269

263270
Using Unicode Everywhere
264271
-------------------------
@@ -329,12 +336,13 @@ UTF-16
329336

330337
Kind of like UTF-8, except it uses at least 16bits (2 bytes) for each character: NOT ASCII compatible.
331338

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.
333340

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".
335342

336343
MS Windows uses UTF-16, as does (I think) Java.
337344

345+
338346
UTF-16 criticism
339347
-----------------
340348

@@ -362,7 +370,7 @@ A 1-byte per char encoding.
362370

363371
* The most common one-byte per char encoding for European text.
364372

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 )
366374

367375
* You will never get an UnicodeDecodeError if you try to decode arbitrary bytes with latin-1.
368376

@@ -384,27 +392,25 @@ http://docs.python.org/howto/unicode.html
384392

385393
"Reading Unicode from a file is therefore simple"
386394

387-
use io.open:
395+
use plain old open:
388396

389397
.. code-block:: python
390398
391-
from io import open
392-
io.open('unicode.rst', encoding='utf-8')
399+
open('unicode.rst', encoding='utf-8')
393400
for line in f:
394401
print repr(line)
395402
396-
(https://docs.python.org/2/library/io.html#module-interface)
397-
398-
.. note: This is all for Python 2 -- the built in ``open`` in Py3 does utf-8 by default.
399403
400404
Encodings Built-in to Python:
401-
http://docs.python.org/2/library/codecs.html#standard-encodings
405+
http://docs.python.org/3/library/codecs.html#standard-encodings
402406

403407

404408
Gotchas in Python 2
405-
--------------------
409+
-------------------
406410

407-
file names, etc:
411+
(Probaly do'nt need to worry about this anymore!)
412+
413+
File names, etc:
408414

409415
If you pass in unicode, you get unicode
410416

@@ -438,7 +444,7 @@ Exception messages:
438444
:download:`exception_test.py <../examples/unicode/exception_test.py>`.
439445

440446
Unicode in Python 3
441-
----------------------
447+
-------------------
442448

443449
The "string" object **is** Unicode (always).
444450

@@ -456,7 +462,8 @@ It's all much cleaner.
456462
(by the way, the recent implementations are very efficient...)
457463

458464
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:
460467

461468
.. code-block:: ipython
462469
@@ -477,7 +484,7 @@ It's all quite simple an robust.
477484

478485
But there are a couple key points to remember:
479486

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.
481488

482489
* 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.
483490

0 commit comments

Comments
 (0)