Learning Python 4th edition Mark Lutz download
Learning Python 4th edition Mark Lutz download
https://ebookname.com/product/learning-python-4th-edition-mark-
lutz/
https://ebookname.com/product/learning-python-third-edition-mark-
lutz/
https://ebookname.com/product/programming-python-4th-edition-
mark-lutz/
https://ebookname.com/product/python-pocket-reference-4th-ed-
edition-mark-lutz/
https://ebookname.com/product/success-and-failure-in-public-
governance-a-comparative-analysis-new-horizons-in-public-policy-
first-edition-mark-bovens/
Working with Ethnicity Race and Culture in Mental
Health A Handbook for Practitioners 1st Edition Hari
Sewell
https://ebookname.com/product/working-with-ethnicity-race-and-
culture-in-mental-health-a-handbook-for-practitioners-1st-
edition-hari-sewell/
https://ebookname.com/product/analytical-characterization-of-
aluminum-steel-and-superalloys-1st-edition-d-scott-mackenzie/
https://ebookname.com/product/an-introduction-to-forensic-
linguistics-1st-edition-malcolm-coulthard/
https://ebookname.com/product/a-history-of-russian-thought-1st-
edition-william-leatherbarrow/
https://ebookname.com/product/water-distribution-systems-2nd-
revised-edition-edition-c-j-parsloe/
Operations Research 1st Edition R. Sivarethinamohan
https://ebookname.com/product/operations-research-1st-edition-r-
sivarethinamohan/
Learning Python
FOURTH EDITION
Learning Python
Mark Lutz
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or [email protected].
Printing History:
March 1999: First Edition.
December 2003: Second Edition.
October 2007: Third Edition.
September 2009: Fourth Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Learning Python, the image of a wood rat, and related trade dress are trademarks
of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-0-596-15806-4
[M]
1252944666
To Vera.
You are my life.
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
vii
Chapter Summary 18
Test Your Knowledge: Quiz 19
Test Your Knowledge: Answers 19
Table of Contents | ix
User-Defined Classes 101
And Everything Else 102
Chapter Summary 103
Test Your Knowledge: Quiz 103
Test Your Knowledge: Answers 104
x | Table of Contents
7. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
String Literals 157
Single- and Double-Quoted Strings Are the Same 158
Escape Sequences Represent Special Bytes 158
Raw Strings Suppress Escapes 161
Triple Quotes Code Multiline Block Strings 162
Strings in Action 163
Basic Operations 164
Indexing and Slicing 165
String Conversion Tools 169
Changing Strings 171
String Methods 172
String Method Examples: Changing Strings 174
String Method Examples: Parsing Text 176
Other Common String Methods in Action 177
The Original string Module (Gone in 3.0) 178
String Formatting Expressions 179
Advanced String Formatting Expressions 181
Dictionary-Based String Formatting Expressions 182
String Formatting Method Calls 183
The Basics 184
Adding Keys, Attributes, and Offsets 184
Adding Specific Formatting 185
Comparison to the % Formatting Expression 187
Why the New Format Method? 190
General Type Categories 193
Types Share Operation Sets by Categories 194
Mutable Types Can Be Changed In-Place 194
Chapter Summary 195
Test Your Knowledge: Quiz 195
Test Your Knowledge: Answers 196
Table of Contents | xi
More Dictionary Methods 211
A Languages Table 212
Dictionary Usage Notes 213
Other Ways to Make Dictionaries 216
Dictionary Changes in Python 3.0 217
Chapter Summary 223
Test Your Knowledge: Quiz 224
Test Your Knowledge: Answers 224
Table of Contents | xv
17. Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Python Scope Basics 407
Scope Rules 408
Name Resolution: The LEGB Rule 410
Scope Example 411
The Built-in Scope 412
The global Statement 414
Minimize Global Variables 415
Minimize Cross-File Changes 416
Other Ways to Access Globals 418
Scopes and Nested Functions 419
Nested Scope Details 419
Nested Scope Examples 419
The nonlocal Statement 425
nonlocal Basics 425
nonlocal in Action 426
Why nonlocal? 429
Chapter Summary 432
Test Your Knowledge: Quiz 433
Test Your Knowledge: Answers 434
Part V. Modules
xx | Table of Contents
Classes Versus Dictionaries 639
Chapter Summary 641
Test Your Knowledge: Quiz 641
Test Your Knowledge: Answers 641
Palæontological
Lithological Classification.
Classification.
Beds with Intermediate
Tremadoc Slate Series[64] Fauna
Lingula Flags Series Beds with Olenus Fauna
Menevian beds (formerly included in Beds with Paradoxides
Lingula Flags) Fauna
The strata of the Caerfai and Solva groups show the prevalence
of the shallow-water phase almost uninterruptedly through the
whole of the time occupied by their accumulation in the Welsh areas.
They consist chiefly of basal conglomerates, succeeded by
alternations of grits and shales, though the latter are often
converted into slates, owing to the subsequent production of
cleavage. The basal conglomerates of the Caerfai beds are
frequently marked by the existence of enormous pebbles, composed
of fragments of the rocks of the underlying Precambrian groups, and
the possibility of the occurrence of glacial action during their
accumulation as advocated by Dr Hicks must be taken into account.
Above these beds are various coloured grits, with alternations of
muddy sediments often coloured red[66]. The Solva group consists of
massive grits, of various colours, also with alternations of mud,
which have prevalent purple and green hues. The great thickness of
the strata of the Caerfai and Solva Series, which sometimes exceeds
10,000 feet, must also be noted.
[66] In giving this description the red (Glyn) slates of North Wales are treated
as belonging to the Caerfai series, though this correlation depends on lithological
characters only at present.
The Tremadoc Slates are about 1,000 feet thick. They are divided
into a lower and upper stage, of about equal thickness, and are
essentially composed of iron-stained slates, with a considerable
admixture of calcareous matter in some parts of South Wales, when
they furnish the nearest approach to a limestone which has been
found amongst the Welsh Cambrian strata. They were probably
formed in a fairly deep sea.
Much pyroclastic rock and some lava flows are intercalated
amongst the Welsh Cambrian sediments. Tuffs are formed in the
lower beds of St David's, and lavas and ashes have been found
amongst the Lingula Flags and Tremadoc Slates of North Wales,
while the Lingula Flags of South Wales have furnished several bands
of ash to the north of Haverfordwest. Much of the material of the
grits and muds may be derived from volcanic rocks, though how far
this is so cannot be stated in the absence of information obtained by
detailed petrological examination of the rocks.
The various isolated outcrops of Cambrian strata amongst the
counties of the Welsh borders and adjoining Midland counties
indicate a great thinning of the Cambrian rocks in this direction.
The probable equivalents of the Caerfai rocks occur at Nuneaton,
Comley, and on the flanks of the Wrekin and Malvern hills. The thin
basal conglomerates are succeeded by quartzites, and sometimes
red calcareous sandstones (Comley sandstone). These rocks are
succeeded by thin arenaceous and calcareous beds which represent
either the Solva or Menevian beds of Wales. The Lingula Flags are
represented by the Malvern Shales of the Malvern area and the
Stockingford Shales of Nuneaton, whilst the Tremadoc Slates have as
their equivalents the Shineton Shales. The exact thicknesses of these
deposits do not seem to have been recorded, but Prof. Lapworth
observes that in central Shropshire "the Comley and Shineton groups
which ... have a collective thickness of perhaps less than 3,000 feet,
we have apparently a condensed epitome of the entire Cambrian
system as at present generally defined."
The Cambrian rocks of the North-west Highlands consist of a thin
conglomerate succeeded by grits and flags with shaley beds, and
above these a mass of limestone, which may represent some of the
Ordovician deposits as well as those of Cambrian age. Pending a
complete description of the faunas of these rocks, it is sufficient to
state that the only fauna which has hitherto been described in detail
indicates the existence of Lowest Cambrian rocks. Further remarks
will be made on this head when describing the character of the
Cambrian faunas. The Cambrian rocks of the North-west Highlands
are also very thin as compared with those of Wales, so that the
Highland and Welsh borderland regions appear to have existed as a
deeper sea area than that which is indicated by the Cambrian rocks
of Wales, an inference which is to some extent borne out by study of
the Cambrian rocks of extra-British areas, to which we may now
turn.
The principal European developments of Cambrian rock are found
in Scandinavia, Russia, Bohemia and Spain, and of these the
Scandinavian one is by far the most fully developed, as there is a
complete sequence in the rocks of that peninsula. They occur both in
Norway and Sweden, but the Swedish exposures are the most
interesting in most respects, especially those of Westrogothia and
Scania. The rocks are of no great thickness, and consist essentially
of black carbonaceous shales, with inconstant bands of impure black
limestone composed almost entirely of the remains of trilobites or
more rarely of brachiopods. These Alum Shales, as they are termed,
rest unconformably upon Precambrian rocks, and have arenaceous
and conglomeratic deposits at the base. In Russia the rocks are still
further attenuated, and have not yielded the relics of so many
faunas as have been found in the Scandinavian Cambrian rocks.
The Bohemian development is incomplete, owing apparently to
an unconformity at the base of the overlying Ordovician rocks, while
the Spanish deposits which seem fairly thick and composed largely
of mechanical sediments have not been worked out in very great
detail.
The American development of Cambrian rocks resembles the
European one in many striking particulars, and as in the case of
Europe, there are lateral variations in the lithological characters of
the rocks, though in the opposite direction, the shallow-water
deposits occurring on the east coast, and the deep-water deposits
further west.
The general distribution of the different types of Cambrian strata
in Europe and North America has been accounted for on the
supposition that in Cambrian times a tract of land lay over much of
the present site of the North Atlantic Ocean, and that the detritus of
that land formed the shallow-water accumulations of Wales and the
east of Canada, whilst further away from it were deposited the open-
sea accumulations of Scandinavia and Russia on one side and of the
more westerly regions of North America on the other, as indicated in
Fig. 16.
Fig. 16.
P. Precambrian Rocks.
A. Land.
XX´. Sea level.
BB´. Shore deposits.
CC´. Deep-water deposits.
DD´. Abyssal deposits.
The Cambrian Faunas. The Cambrian Period has been termed the
age of trilobites, for they are the dominant forms of the time, but
they are associated with many other forms of invertebrata; indeed
all the great groups of this division are represented in the earliest
Cambrian fauna. Dr C. D. Walcott records representatives of
Spongiae, Hydrozoa, Echinodermata, Annelida, Brachiopoda,
Lamellibranchiata, Gastropoda, Pteropoda, Crustacea and Trilobita as
occurring in the Olenellus beds of North America and other groups
are represented in the rocks of this age in the Old World. The
Cambrian trilobites as a whole are of more generalised types than
those of the later systems which furnish their remains, as indicated
especially by the looseness of the body, and the large number of
body rings in many of the genera, while the tail or pygidium was
small and formed of only a few coalesced segments, as pointed out
by Barrande. In the later trilobites the test is more compact, there
are on the whole fewer body rings, as more of these have become
fused into a tail which is therefore larger than that of the average
tail of the Cambrian trilobite.
Taking the faunas in order, the oldest or Olenellus fauna has
furnished a great variety of forms in the North-west Highlands of
Scotland, Shropshire, Scandinavia, Esthonia, Sardinia, Canada, and
Newfoundland, whilst representative species of the fauna have been
recorded also from Worcestershire, Warwickshire, Pembrokeshire,
India, China, and Australia.
The dominant form is the trilobite of the genus or group
Olenellus, which contains a great variety of species referable to three
or four divisions which have been ranked as separate genera by
some writers. Associated with Olenellus are trilobites belonging to
other genera, which are found in higher deposits, though there
represented by different species.
Brachiopods are fairly abundant, especially those provided with a
horny shell; of these, the genus Kutorgina is widely distributed.
The zoological relationships of several of the fossils of this
horizon are as yet doubtful. The Archæocyathinæ show affinities
with certain corals; a number of tests, included in the genus
Hyolithes and its allies are doubtfully referred to the Pteropods, and
the position of the genus Volborthella is uncertain. Special attention
is directed to these doubtful relationships, as it is possible that a
number of 'generalised forms' of organisms occur in these strata[68].
[68] For an account of the Olenellus fauna see Walcott, C. D., "The Fauna of
the Lower Cambrian or Olenellus Zone," Tenth Annual Report of the Director of the
United States Geological Survey, Washington, 1890. It is possible that some of the
fossils mentioned in that report belong to strata above that containing Olenellus.
CHAPTER XV.
Fig. 17.
A = Arenig. L =
Llandeilo. C = Caradoc.
CHAPTER XVI.
The Silurian Faunas[81]. The Silurian period has been termed the
period of Crinoids, and this group of creatures certainly contained a
great variety of very remarkable forms, which are specially
numerous in the Wenlock Limestone of the Welsh borders, Gothland,
and North America, but many of the rocks of the system display few
traces of these organisms. The trilobites and graptolites still
contribute largely to the fauna, the latter becoming very scarce at
the summit of the system, though a few specimens have been
detected in the rocks of the succeeding system. The trilobites belong
to few genera though these are mostly more highly organised than
those of the Ordovician period. The genus Harpes has been taken as
fairly characteristic of the lower part of the system in Sweden, and it
occurs there abundantly in places in Britain, whilst Encrinurus is
more abundant in the upper series, but both of these genera range
from higher Ordovician beds into the Devonian. Mention has already
been made of the corals. Brachiopods are very abundant, and
Mollusca appear with considerable frequency. The appearance of
true insects is of importance, cockroaches have been recorded from
Silurian rocks and a number of other insects have lately been
recorded from Canada[82]. Eurypterids occur in considerable
abundance in the higher parts of the system, as do also the remains
of fish.
[81] For an account of the Silurian faunas the student may consult Sir R. I.
Murchison's Silurian System or the shorter Siluria and Lapworth's paper on the
Geological Distribution of the Rhabdophora.
[82] See an article by Dr G. F. Matthew, "Description of an extinct Palæozoic
Insect and a review of the Fauna with which it occurs," Bulletin XV. of the Natural
History Society of New Brunswick. The Silurian Rocks of the Little River Group of
St John, New Brunswick, have yielded species of land snails, two doubtful saw-
bugs, several arachnids, and myriopods, two insects of the order Thysanura
(Spring-tails), and eight Palæodictyoptera.
CHAPTER XVII.
N. Devon. S. Devon[83].
Pilton Beds
Entomis Slates
Cucullæa
Upper Devonian Goniatite Limestones and
(Marwood) Beds
(Clymenian) Slates
Pickwell Down
Massive Limestones
Sandstone
Middle Devonian
Limestones
Middle Devonian Morte Slates
Ashprington Volcanic Series
(Eifelian) Ilfracombe Beds
Eifelian Slates and Shaly
Limestones
Hangman Grits Lower Devonian Slates
Lower Devonian
Lynton Slates Lincombe and Warberry
(Coblenzian)
Foreland Grits Grits and Meadfoot Sands
[83] An account of the South Devon rocks by Mr Ussher will be found in the
Quart. Journ. Geol. Soc., vol. XLVI. p. 487; from it the above classification of the
rocks of S. Devon is taken.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com