Skip to content

Commit 0cf06fc

Browse files
committed
Day 1 updates
1 parent 455a3ef commit 0cf06fc

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

index.rst

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ Introduction to Python
2828

2929
**Exercises**
3030

31-
Exercises for this book are taken from four primary sources. These are:
31+
Exercises for this book are taken from five primary sources. These are:
3232
* Zed Shaw's excellent book, `Learn Python the Hard Way`_, known affectionately as LPTHW
3333
* `Google's Python Class`_, available on Google Code
3434
* `Intermediate and Advanced Software Carpentry with Python`_, an online course by C. Titus Brown
3535
* `Dive Into Python`_, by Mark Pilgrim
36+
* `Python Cookbook, 3rd Edition`_, by David Beazley and Brian K. Jones
3637

3738
.. _Learn Python the Hard Way: http://learnpythonthehardway.org
3839
.. _Google's Python Class: http://code.google.com/edu/languages/google-python-class/
3940
.. _Intermediate and Advanced Software Carpentry with Python: http://ivory.idyll.org/articles/advanced-swc/
4041
.. _Dive Into Python: http://diveintopython.org/
42+
.. _Python Cookbook, 35rd Edition: http://shop.oreilly.com/product/0636920027072.do
4143

4244
Read slides on your own
4345
-----------------------
@@ -374,25 +376,16 @@ For this course, I've suggested your company set up the following tools:
374376
.. class:: incremental
375377

376378
* **Python 2.7**: the latest "stable/prod" version of Python
377-
* **Console2**: a better console for Windows
378-
* **Cream**: an easy-to-use extension of the ViM text editor
379-
* **WingIDE 101**: a full-fledged Python IDE
380379
* **IPython**: an interactive Python shell
380+
* **pip**: python's package manager
381+
* **IPython Notebook**: a browser-based Python REPL and interactive environment
381382

382-
Quick note on WingIDE
383-
---------------------
384-
385-
.. image:: img/02_ide.png
386-
:class: scaled
387-
388-
Python, IPython, BPython, DreamPie
389-
----------------------------------
383+
Python, IPython, IPython Notebook
384+
---------------------------------
390385

391386
One of Python's best features is the REPL loop, or **Read**, **Evaluate**, **Print** **Loop**.
392387

393-
Open source developers have created a number of "enhanced" REPLs over the years. The most common of these is **IPython**, but there is also **BPython** and **DreamPie**.
394-
395-
WingIDE has its own REPL that is integrated with the IDE and works in debug mode, as well.
388+
Open source developers have created a number of "enhanced" REPLs over the years. The most common of these is **IPython Notebook**, but there is also regular old **IPython** as well as others.
396389

397390
BREAK: Confirm system settings
398391
------------------------------
@@ -479,7 +472,7 @@ Minimal automatic coercion
479472

480473
>>> string = "two"
481474
>>> number = 3
482-
>>> string, integer = integer, string # swap values
475+
>>> string, number = number, string # swap values
483476
>>> print string + number
484477
Traceback (most recent call last)
485478
string + number
@@ -512,7 +505,7 @@ Why indentation?
512505

513506
Better answer: because some of us don't.
514507

515-
`Python Style Guide (PEP 8)`_ recommends 4 spaces, and no tab characters.
508+
`Python Style Guide (PEP 8)`_ specifies 4 spaces, and no tab characters.
516509

517510
.. _Python Style Guide (PEP 8): http://www.python.org/dev/peps/pep-0008/
518511

@@ -627,6 +620,9 @@ Comments
627620
# Written by John Doe, 6/5/2011
628621
#
629622
if __name__ == "__main__": # only runs when script is executed
623+
"""This comment can span multiple
624+
lines
625+
"""
630626
print "Hello, World"
631627

632628
Math and Numbers
@@ -674,32 +670,27 @@ When dealing with non-scientific computations, you probably want to use the ``De
674670

675671
To make it a little easier to use, I imported it using a module alias ``D``.
676672

677-
Fractions
678-
---------
679-
680-
.. sourcecode:: python
681-
682-
>>> from fractions import Fraction as F
683-
>>> F(1, 3) + F(1, 2)
684-
Fraction(5, 6)
685-
686673
Formatting
687674
----------
688675

689-
There are two simple ways to do string formatting in Python, and both are very popular.
676+
There are a few ways to do string formatting in Python, though one is preferred.
677+
``str.format`` is a method on ``str`` that provides a full complement of
678+
string formatting features.
690679

691-
* ``%``, defined on ``str`` objects.
692-
* ``str.format``, a more verbose method available on ``str``.
680+
.. sourcecode:: python
681+
682+
>>> "{num} cars crossed the intersection \
683+
in the last {hrs} hours".format(num=5, hrs=24)
684+
5 cars crossed the intersection in the last 24 hours
693685

686+
``%``, defined on ``str`` objects, is sometimes used in older code for basic
687+
string formatting.
694688

695689
.. sourcecode:: python
696690

697691
>>> "%s cars crossed the intersection \
698692
in the last %s hours" % (5, 24)
699693
5 cars crossed the intersection in the last 24 hours
700-
>>> "{num} cars crossed the intersection \
701-
in the last {hrs} hours".format(num=5, hrs=24)
702-
5 cars crossed the intersection in the last 24 hours
703694

704695

705696
Strings
@@ -928,6 +919,7 @@ Using an elif chain
928919
else:
929920
return None
930921

922+
931923
Lists
932924
-----
933925

@@ -971,15 +963,21 @@ Lists can be concatenated with other lists using ``extend``, which creates a new
971963
List slicing
972964
------------
973965

974-
Lists have a convenient syntax for accessing single elements or even ranges of elements. This is called *slicing* and can also be done with the ``slice`` builtin function or ``[i:j:stride]`` syntax.
966+
Lists (and other "sequences") have a convenient syntax for accessing single elements or even ranges of elements. This is called *slicing* and can also be done with the ``slice`` builtin function or ``[i:j:step]`` syntax.
975967

976968
.. image:: img/10_listslice.png
977969
:align: center
978970

971+
.. sourcecode:: python
972+
973+
record = '....................100 .......513.25 ..........'
974+
cost = int(record[20:32]) * float(record[40:48])
975+
976+
979977
List aliasing
980978
-------------
981979

982-
Lists can be aliased simply by binding a new label to the list. This sometimes leads to bugs!
980+
Lists and other so-called "mutable objects" can be aliased simply by binding a new label to the list. This sometimes leads to bugs!
983981

984982
.. image:: img/11_listalias.png
985983
:align: center
@@ -1028,18 +1026,17 @@ Switch statement to dictionary
10281026
"application/ms-word": "MS Word"
10291027
"application/ms-excel": "MS Excel"
10301028
}
1031-
return long2short.get(long, None)
1029+
print long2short.get(long, None)
10321030

10331031
Sets
10341032
----
10351033

10361034
.. sourcecode:: python
10371035

1038-
>>> ppl = ["Andrew", "Joe", "Bob", "Joe", "Bob", "Andrew"]
1039-
>>> ppl = set(ppl)
1036+
>>> ppl = {"Andrew", "Joe", "Bob", "Joe", "Bob", "Andrew"}
10401037
>>> print ppl
10411038
set(["Andrew", "Joe", "Bob"])
1042-
>>> trainers = set(["Andrew"])
1039+
>>> trainers = {"Andrew"}
10431040
>>> print ppl - trainers
10441041
set(["Joe", "Bob"])
10451042

@@ -1078,11 +1075,11 @@ Use ``in`` where possible (2)
10781075

10791076
# do this:
10801077
if key in d:
1081-
...do something with d[key]
1078+
text = d[key]
10821079

10831080
# not this:
10841081
if d.has_key(key):
1085-
...do something with d[key]
1082+
text = d[key]
10861083

10871084
Think data, not code
10881085
--------------------
@@ -1171,7 +1168,7 @@ A Python *sequence* is any type that supports these operations:
11711168
* ``len``, for counting number of items in container
11721169
* ``s[i]``, for getting the i'th element of sequence
11731170
* ``s[i:j]``, for slicing from i to j in container
1174-
* ``s[i:j:stride]``, for slicing from i to j, using stride
1171+
* ``s[i:j:step]``, for slicing from i to j, skipping to every index that is a multiple of 'step'
11751172

11761173
.. image:: img/24_slicing.png
11771174
:align: center
@@ -1291,7 +1288,7 @@ Argument types
12911288
* Instead, Pythonistas prefer what is known as *duck typing*
12921289

12931290
Type checking anti-pattern
1294-
-----------------------------------
1291+
--------------------------
12951292

12961293
.. sourcecode:: python
12971294

@@ -1326,8 +1323,10 @@ Take a deep breath
13261323

13271324
This concept, *duck typing*, is near and dear to many a Pythonista's heart.
13281325

1326+
"If it looks like a duck and quacks like a duck, then it must be a duck!"
1327+
13291328
Eager checking anti-pattern
1330-
--------------------------------------
1329+
---------------------------
13311330

13321331
.. sourcecode:: python
13331332

@@ -1464,7 +1463,7 @@ URL Parser
14641463
host, port = host.split(":", 1)
14651464
else:
14661465
port = None
1467-
return dict(
1466+
return (
14681467
Scheme=scheme, Host=host,
14691468
Port=port, Path=path)
14701469

@@ -1510,11 +1509,6 @@ EXERCISES: Strings and Lists
15101509
* http://code.google.com/edu/languages/google-python-class/strings.html
15111510
* http://code.google.com/edu/languages/google-python-class/lists.html
15121511

1513-
HOMEWORK: Chapters 1-5
1514-
----------------------
1515-
1516-
Will give more detail and color on the topics we covered today.
1517-
15181512
Day 2
15191513
------
15201514

0 commit comments

Comments
 (0)