Related skills
- Python
Associated roles+10 more
- AI engineer
- Analytics engineer
- Backend developer
- Business analyst
- Business intelligence analyst
- Data analyst
- Data architect
- Database administrator
- Data engineer
- Data scientist
- ML/AI architect
- Penetration tester
- Python developer
- QA engineer
Contents
- This Book’s “Ecosystem”About This Fifth EditionThe Python 2.X and 3.X LinesThe 2.X/3.X Story TodayCoverage for Both 3.X and 2.XWhich Python Should I Use?This Book’s Prerequisites and EffortThis Book’s StructureWhat This Book Is NotIt’s Not a Reference or a Guide to Specific ApplicationsIt’s Not the Short Story for People in a HurryIt’s as Linear as Python AllowsThis Book’s ProgramsPython VersionsPlatformsFetching This Book’s CodeUsing This Book’s CodeFont ConventionsBook Updates and ResourcesAcknowledgmentsThe BackstoryPython ThanksPersonal Thanks
- Why Do People Use Python?Software QualityDeveloper ProductivityIs Python a “Scripting Language”?OK, but What’s the Downside?Who Uses Python Today?What Can I Do with Python?Systems ProgrammingGUIsInternet ScriptingComponent IntegrationDatabase ProgrammingRapid PrototypingNumeric and Scientific ProgrammingAnd More: Gaming, Images, Data Mining, Robots, Excel...How Is Python Developed and Supported?Open Source TradeoffsWhat Are Python’s Technical Strengths?It’s Object-Oriented and FunctionalIt’s FreeIt’s PortableIt’s PowerfulIt’s MixableIt’s Relatively Easy to UseIt’s Relatively Easy to LearnIt’s Named After Monty PythonHow Does Python Stack Up to Language X?Chapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Introducing the Python InterpreterProgram ExecutionThe Programmer’s ViewPython’s ViewExecution Model VariationsPython Implementation AlternativesExecution Optimization ToolsFrozen BinariesFuture Possibilities?Chapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- The Interactive PromptStarting an Interactive SessionThe System PathNew Windows Options in 3.3: PATH, LauncherWhere to Run: Code DirectoriesWhat Not to Type: Prompts and CommentsRunning Code InteractivelyWhy the Interactive Prompt?Usage Notes: The Interactive PromptSystem Command Lines and FilesA First ScriptRunning Files with Command LinesCommand-Line Usage VariationsUsage Notes: Command Lines and FilesUnix-Style Executable Scripts: #!Unix Script BasicsThe Unix env Lookup TrickThe Python 3.3 Windows Launcher: #! Comes to WindowsClicking File IconsIcon-Click BasicsClicking Icons on WindowsThe input Trick on WindowsOther Icon-Click LimitationsModule Imports and ReloadsImport and Reload BasicsThe Grander Module Story: AttributesUsage Notes: import and reloadUsing exec to Run Module FilesThe IDLE User InterfaceIDLE Startup DetailsIDLE Basic UsageIDLE Usability FeaturesAdvanced IDLE ToolsUsage Notes: IDLEOther IDEsOther Launch OptionsEmbedding CallsFrozen Binary ExecutablesText Editor Launch OptionsStill Other Launch OptionsFuture Possibilities?Which Option Should I Use?Chapter SummaryTest Your Knowledge: QuizTest Your Knowledge: AnswersTest Your Knowledge: Part I Exercises
- The Python Conceptual HierarchyWhy Use Built-in Types?Python’s Core Data TypesNumbersStringsSequence OperationsImmutabilityType-Specific MethodsGetting HelpOther Ways to Code StringsUnicode StringsPattern MatchingListsSequence OperationsType-Specific OperationsBounds CheckingNestingComprehensionsDictionariesMapping OperationsNesting RevisitedMissing Keys: if TestsSorting Keys: for LoopsIteration and OptimizationTuplesWhy Tuples?FilesBinary Bytes FilesUnicode Text FilesOther File-Like ToolsOther Core TypesHow to Break Your Code’s FlexibilityUser-Defined ClassesAnd Everything ElseChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Numeric Type BasicsNumeric LiteralsBuilt-in Numeric ToolsPython Expression OperatorsNumbers in ActionVariables and Basic ExpressionsNumeric Display FormatsComparisons: Normal and ChainedDivision: Classic, Floor, and TrueInteger PrecisionComplex NumbersHex, Octal, Binary: Literals and ConversionsBitwise OperationsOther Built-in Numeric ToolsOther Numeric TypesDecimal TypeFraction TypeSetsBooleansNumeric ExtensionsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- The Case of the Missing Declaration StatementsVariables, Objects, and ReferencesTypes Live with Objects, Not VariablesObjects Are Garbage-CollectedShared ReferencesShared References and In-Place ChangesShared References and EqualityDynamic Typing Is EverywhereChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- This Chapter’s ScopeUnicode: The Short StoryString BasicsString LiteralsSingle- and Double-Quoted Strings Are the SameEscape Sequences Represent Special CharactersRaw Strings Suppress EscapesTriple Quotes Code Multiline Block StringsStrings in ActionBasic OperationsIndexing and SlicingString Conversion ToolsChanging Strings IString MethodsMethod Call SyntaxMethods of StringsString Method Examples: Changing Strings IIString Method Examples: Parsing TextOther Common String Methods in ActionThe Original string Module’s Functions (Gone in 3.X)String Formatting ExpressionsFormatting Expression BasicsAdvanced Formatting Expression SyntaxAdvanced Formatting Expression ExamplesDictionary-Based Formatting ExpressionsString Formatting Method CallsFormatting Method BasicsAdding Keys, Attributes, and OffsetsAdvanced Formatting Method SyntaxAdvanced Formatting Method ExamplesComparison to the % Formatting ExpressionWhy the Format Method?General Type CategoriesTypes Share Operation Sets by CategoriesMutable Types Can Be Changed in PlaceChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- ListsLists in ActionBasic List OperationsList Iteration and ComprehensionsIndexing, Slicing, and MatrixesChanging Lists in PlaceDictionariesDictionaries in ActionBasic Dictionary OperationsChanging Dictionaries in PlaceMore Dictionary MethodsExample: Movie DatabaseDictionary Usage NotesOther Ways to Make DictionariesDictionary Changes in Python 3.X and 2.7Chapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- TuplesTuples in ActionWhy Lists and Tuples?Records Revisited: Named TuplesFilesOpening FilesUsing FilesFiles in ActionText and Binary Files: The Short StoryStoring Python Objects in Files: ConversionsStoring Native Python Objects: pickleStoring Python Objects in JSON FormatStoring Packed Binary Data: structFile Context ManagersOther File ToolsCore Types Review and SummaryObject FlexibilityReferences Versus CopiesComparisons, Equality, and TruthThe Meaning of True and False in PythonPython’s Type HierarchiesType ObjectsOther Types in PythonBuilt-in Type GotchasAssignment Creates References, Not CopiesRepetition Adds One Level DeepBeware of Cyclic Data StructuresImmutable Types Can’t Be Changed in PlaceChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: AnswersTest Your Knowledge: Part II Exercises
- The Python Conceptual Hierarchy RevisitedPython’s StatementsA Tale of Two ifsWhat Python AddsWhat Python RemovesWhy Indentation Syntax?A Few Special CasesA Quick Example: Interactive LoopsA Simple Interactive LoopDoing Math on User InputsHandling Errors by Testing InputsHandling Errors with try StatementsNesting Code Three Levels DeepChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Assignment StatementsAssignment Statement FormsSequence AssignmentsExtended Sequence Unpacking in Python 3.XMultiple-Target AssignmentsAugmented AssignmentsVariable Name RulesExpression StatementsExpression Statements and In-Place ChangesPrint OperationsThe Python 3.X print FunctionThe Python 2.X print StatementPrint Stream RedirectionVersion-Neutral PrintingChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- if StatementsGeneral FormatBasic ExamplesMultiway BranchingPython Syntax RevisitedBlock Delimiters: Indentation RulesStatement Delimiters: Lines and ContinuationsA Few Special CasesTruth Values and Boolean TestsThe if/else Ternary ExpressionChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- while LoopsGeneral FormatExamplesbreak, continue, pass, and the Loop elseGeneral Loop FormatpasscontinuebreakLoop elsefor LoopsGeneral FormatExamplesLoop Coding TechniquesCounter Loops: rangeSequence Scans: while and range Versus forSequence Shufflers: range and lenNonexhaustive Traversals: range Versus SlicesChanging Lists: range Versus ComprehensionsParallel Traversals: zip and mapGenerating Both Offsets and Items: enumerateChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Iterations: A First LookThe Iteration Protocol: File IteratorsManual Iteration: iter and nextOther Built-in Type IterablesList Comprehensions: A First Detailed LookList Comprehension BasicsUsing List Comprehensions on FilesExtended List Comprehension SyntaxOther Iteration ContextsNew Iterables in Python 3.XImpacts on 2.X Code: Pros and ConsThe range IterableThe map, zip, and filter IterablesMultiple Versus Single Pass IteratorsDictionary View IterablesOther Iteration TopicsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Python Documentation Sources# CommentsThe dir FunctionDocstrings: __doc__PyDoc: The help FunctionPyDoc: HTML ReportsBeyond docstrings: SphinxThe Standard Manual SetWeb ResourcesPublished BooksCommon Coding GotchasChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: AnswersTest Your Knowledge: Part III Exercises
- Why Use Functions?Coding Functionsdef Statementsdef Executes at RuntimeA First Example: Definitions and CallsDefinitionCallsPolymorphism in PythonA Second Example: Intersecting SequencesDefinitionCallsPolymorphism RevisitedLocal VariablesChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Python Scope BasicsScope DetailsName Resolution: The LEGB RuleScope ExampleThe Built-in ScopeThe global StatementProgram Design: Minimize Global VariablesProgram Design: Minimize Cross-File ChangesOther Ways to Access GlobalsScopes and Nested FunctionsNested Scope DetailsNested Scope ExamplesFactory Functions: ClosuresRetaining Enclosing Scope State with DefaultsThe nonlocal Statement in 3.Xnonlocal Basicsnonlocal in ActionWhy nonlocal? State Retention OptionsState with nonlocal: 3.X onlyState with Globals: A Single Copy OnlyState with Classes: Explicit Attributes (Preview)State with Function Attributes: 3.X and 2.XChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Argument-Passing BasicsArguments and Shared ReferencesAvoiding Mutable Argument ChangesSimulating Output Parameters and Multiple ResultsSpecial Argument-Matching ModesArgument Matching BasicsArgument Matching SyntaxThe Gritty DetailsKeyword and Default ExamplesArbitrary Arguments ExamplesPython 3.X Keyword-Only ArgumentsThe min Wakeup Call!Full CreditBonus PointsThe Punch Line...Generalized Set FunctionsEmulating the Python 3.X print FunctionUsing Keyword-Only ArgumentsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Function Design ConceptsRecursive FunctionsSummation with RecursionCoding AlternativesLoop Statements Versus RecursionHandling Arbitrary StructuresFunction Objects: Attributes and AnnotationsIndirect Function Calls: “First Class” ObjectsFunction IntrospectionFunction AttributesFunction Annotations in 3.XAnonymous Functions: lambdalambda BasicsWhy Use lambda?How (Not) to Obfuscate Your Python CodeScopes: lambdas Can Be Nested TooFunctional Programming ToolsMapping Functions over Iterables: mapSelecting Items in Iterables: filterCombining Items in Iterables: reduceChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- List Comprehensions and Functional ToolsList Comprehensions Versus mapAdding Tests and Nested Loops: filterExample: List Comprehensions and MatrixesDon’t Abuse List Comprehensions: KISSGenerator Functions and ExpressionsGenerator Functions: yield Versus returnGenerator Expressions: Iterables Meet ComprehensionsGenerator Functions Versus Generator ExpressionsGenerators Are Single-Iteration ObjectsGeneration in Built-in Types, Tools, and ClassesExample: Generating Scrambled SequencesDon’t Abuse Generators: EIBTIExample: Emulating zip and map with Iteration ToolsComprehension Syntax SummaryScopes and Comprehension VariablesComprehending Set and Dictionary ComprehensionsExtended Comprehension Syntax for Sets and DictionariesChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Timing Iteration AlternativesTiming Module: HomegrownTiming ScriptTiming ResultsTiming Module AlternativesOther SuggestionsTiming Iterations and Pythons with timeitBasic timeit UsageBenchmark Module and Script: timeitBenchmark Script ResultsMore Fun with BenchmarksOther Benchmarking Topics: pystonesFunction GotchasLocal Names Are Detected StaticallyDefaults and Mutable ObjectsFunctions Without returnsMiscellaneous Function GotchasChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: AnswersTest Your Knowledge: Part IV Exercises
- Why Use Modules?Python Program ArchitectureHow to Structure a ProgramImports and AttributesStandard Library ModulesHow Imports Work1. Find It2. Compile It (Maybe)3. Run ItByte Code Files: __pycache__ in Python 3.2+Byte Code File Models in ActionThe Module Search PathConfiguring the Search PathSearch Path VariationsThe sys.path ListModule File SelectionChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Module CreationModule FilenamesOther Kinds of ModulesModule UsageThe import StatementThe from StatementThe from * StatementImports Happen Only Onceimport and from Are Assignmentsimport and from EquivalencePotential Pitfalls of the from StatementModule NamespacesFiles Generate NamespacesNamespace Dictionaries: __dict__Attribute Name QualificationImports Versus ScopesNamespace NestingReloading Modulesreload Basicsreload ExampleChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Package Import BasicsPackages and Search Path SettingsPackage __init__.py FilesPackage Import Examplefrom Versus import with PackagesWhy Use Package Imports?A Tale of Three SystemsPackage Relative ImportsChanges in Python 3.XRelative Import BasicsWhy Relative Imports?The Scope of Relative ImportsModule Lookup Rules SummaryRelative Imports in ActionPitfalls of Package-Relative Imports: Mixed UsePython 3.3 Namespace PackagesNamespace Package SemanticsImpacts on Regular Packages: Optional __init__.pyNamespace Packages in ActionNamespace Package NestingFiles Still Have Precedence over DirectoriesChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Module Design ConceptsData Hiding in ModulesMinimizing from * Damage: _X and __all__Enabling Future Language Features: __future__Mixed Usage Modes: __name__ and __main__Unit Tests with __name__Example: Dual Mode CodeCurrency Symbols: Unicode in ActionDocstrings: Module Documentation at WorkChanging the Module Search PathThe as Extension for import and fromExample: Modules Are ObjectsImporting Modules by Name StringRunning Code StringsDirect Calls: Two OptionsExample: Transitive Module ReloadsA Recursive ReloaderAlternative CodingsModule GotchasModule Name Clashes: Package and Package-Relative ImportsStatement Order Matters in Top-Level Codefrom Copies Names but Doesn’t Linkfrom * Can Obscure the Meaning of Variablesreload May Not Impact from Importsreload, from, and Interactive TestingRecursive from Imports May Not WorkChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: AnswersTest Your Knowledge: Part V Exercises
- Why Use Classes?OOP from 30,000 FeetAttribute Inheritance SearchClasses and InstancesMethod CallsCoding Class TreesOperator OverloadingOOP Is About Code ReuseChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Classes Generate Multiple Instance ObjectsClass Objects Provide Default BehaviorInstance Objects Are Concrete ItemsA First ExampleClasses Are Customized by InheritanceA Second ExampleClasses Are Attributes in ModulesClasses Can Intercept Python OperatorsA Third ExampleWhy Use Operator Overloading?The World’s Simplest Python ClassRecords Revisited: Classes Versus DictionariesChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Step 1: Making InstancesCoding ConstructorsTesting As You GoUsing Code Two WaysStep 2: Adding Behavior MethodsCoding MethodsStep 3: Operator OverloadingProviding Print DisplaysStep 4: Customizing Behavior by SubclassingCoding SubclassesAugmenting Methods: The Bad WayAugmenting Methods: The Good WayPolymorphism in ActionInherit, Customize, and ExtendOOP: The Big IdeaStep 5: Customizing Constructors, TooOOP Is Simpler Than You May ThinkOther Ways to Combine ClassesStep 6: Using Introspection ToolsSpecial Class AttributesA Generic Display ToolInstance Versus Class AttributesName Considerations in Tool ClassesOur Classes’ Final FormStep 7 (Final): Storing Objects in a DatabasePickles and ShelvesStoring Objects on a Shelve DatabaseExploring Shelves InteractivelyUpdating Objects on a ShelveFuture DirectionsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- The class StatementGeneral FormExampleMethodsMethod ExampleCalling Superclass ConstructorsOther Method Call PossibilitiesInheritanceAttribute Tree ConstructionSpecializing Inherited MethodsClass Interface TechniquesAbstract SuperclassesNamespaces: The ConclusionSimple Names: Global Unless AssignedAttribute Names: Object NamespacesThe “Zen” of Namespaces: Assignments Classify NamesNested Classes: The LEGB Scopes Rule RevisitedNamespace Dictionaries: ReviewNamespace Links: A Tree ClimberDocumentation Strings RevisitedClasses Versus ModulesChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- The BasicsConstructors and Expressions: __init__ and __sub__Common Operator Overloading MethodsIndexing and Slicing: __getitem__ and __setitem__Intercepting SlicesSlicing and Indexing in Python 2.XBut 3.X’s __index__ Is Not Indexing!Index Iteration: __getitem__Iterable Objects: __iter__ and __next__User-Defined IterablesMultiple Iterators on One ObjectCoding Alternative: __iter__ plus yieldMembership: __contains__, __iter__, and __getitem__Attribute Access: __getattr__ and __setattr__Attribute ReferenceAttribute Assignment and DeletionOther Attribute Management ToolsEmulating Privacy for Instance Attributes: Part 1String Representation: __repr__ and __str__Why Two Display Methods?Display Usage NotesRight-Side and In-Place Uses: __radd__ and __iadd__Right-Side AdditionIn-Place AdditionCall Expressions: __call__Function Interfaces and Callback-Based CodeComparisons: __lt__, __gt__, and OthersThe __cmp__ Method in Python 2.XBoolean Tests: __bool__ and __len__Boolean Methods in Python 2.XObject Destruction: __del__Destructor Usage NotesChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Python and OOPPolymorphism Means Interfaces, Not Call SignaturesOOP and Inheritance: “Is-a” RelationshipsOOP and Composition: “Has-a” RelationshipsStream Processors RevisitedOOP and Delegation: “Wrapper” Proxy ObjectsPseudoprivate Class AttributesName Mangling OverviewWhy Use Pseudoprivate Attributes?Methods Are Objects: Bound or UnboundUnbound Methods Are Functions in 3.XBound Methods and Other Callable ObjectsClasses Are Objects: Generic Object FactoriesWhy Factories?Multiple Inheritance: “Mix-in” ClassesCoding Mix-in Display ClassesOther Design-Related TopicsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Extending Built-in TypesExtending Types by EmbeddingExtending Types by SubclassingThe “New Style” Class ModelJust How New Is New-Style?New-Style Class ChangesAttribute Fetch for Built-ins Skips InstancesType Model ChangesAll Classes Derive from “object”Diamond Inheritance ChangeMore on the MRO: Method Resolution OrderExample: Mapping Attributes to Inheritance SourcesNew-Style Class ExtensionsSlots: Attribute DeclarationsProperties: Attribute Accessors__getattribute__ and Descriptors: Attribute ToolsOther Class Changes and ExtensionsStatic and Class MethodsWhy the Special Methods?Static Methods in 2.X and 3.XStatic Method AlternativesUsing Static and Class MethodsCounting Instances with Static MethodsCounting Instances with Class MethodsDecorators and Metaclasses: Part 1Function Decorator BasicsA First Look at User-Defined Function DecoratorsA First Look at Class Decorators and MetaclassesFor More DetailsThe super Built-in Function: For Better or Worse?The Great super DebateTraditional Superclass Call Form: Portable, GeneralBasic super Usage and Its TradeoffsThe super Upsides: Tree Changes and DispatchRuntime Class Changes and superCooperative Multiple Inheritance Method DispatchThe super SummaryClass GotchasChanging Class Attributes Can Have Side EffectsChanging Mutable Class Attributes Can Have Side Effects, TooMultiple Inheritance: Order MattersScopes in Methods and ClassesMiscellaneous Class GotchasKISS Revisited: “Overwrapping-itis”Chapter SummaryTest Your Knowledge: QuizTest Your Knowledge: AnswersTest Your Knowledge: Part VI Exercises
- Why Use Exceptions?Exception RolesExceptions: The Short StoryDefault Exception HandlerCatching ExceptionsRaising ExceptionsUser-Defined ExceptionsTermination ActionsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- The try/except/else StatementHow try Statements Worktry Statement ClausesThe try else ClauseExample: Default BehaviorExample: Catching Built-in ExceptionsThe try/finally StatementExample: Coding Termination Actions with try/finallyUnified try/except/finallyUnified try Statement SyntaxCombining finally and except by NestingUnified try ExampleThe raise StatementRaising ExceptionsScopes and try except VariablesPropagating Exceptions with raisePython 3.X Exception Chaining: raise fromThe assert StatementExample: Trapping Constraints (but Not Errors!)with/as Context ManagersBasic UsageThe Context Management ProtocolMultiple Context Managers in 3.1, 2.7, and LaterChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Exceptions: Back to the FutureString Exceptions Are Right Out!Class-Based ExceptionsCoding Exceptions ClassesWhy Exception Hierarchies?Built-in Exception ClassesBuilt-in Exception CategoriesDefault Printing and StateCustom Print DisplaysCustom Data and BehaviorProviding Exception DetailsProviding Exception MethodsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Nesting Exception HandlersExample: Control-Flow NestingExample: Syntactic NestingException IdiomsBreaking Out of Multiple Nested Loops: “go to”Exceptions Aren’t Always ErrorsFunctions Can Signal Conditions with raiseClosing Files and Server ConnectionsDebugging with Outer try StatementsRunning In-Process TestsMore on sys.exc_infoDisplaying Errors and TracebacksException Design Tips and GotchasWhat Should Be WrappedCatching Too Much: Avoid Empty except and ExceptionCatching Too Little: Use Class-Based CategoriesCore Language SummaryThe Python ToolsetDevelopment Tools for Larger ProjectsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: AnswersTest Your Knowledge: Part VII Exercises
- String Changes in 3.XString BasicsCharacter Encoding SchemesHow Python Stores Strings in MemoryPython’s String TypesText and Binary FilesCoding Basic StringsPython 3.X String LiteralsPython 2.X String LiteralsString Type ConversionsCoding Unicode StringsCoding ASCII TextCoding Non-ASCII TextEncoding and Decoding Non-ASCII textOther Encoding SchemesByte String Literals: Encoded TextConverting EncodingsCoding Unicode Strings in Python 2.XSource File Character Set Encoding DeclarationsUsing 3.X bytes ObjectsMethod CallsSequence OperationsOther Ways to Make bytes ObjectsMixing String TypesUsing 3.X/2.6+ bytearray Objectsbytearrays in ActionPython 3.X String Types SummaryUsing Text and Binary FilesText File BasicsText and Binary Modes in 2.X and 3.XType and Content Mismatches in 3.XUsing Unicode FilesReading and Writing Unicode in 3.XHandling the BOM in 3.XUnicode Files in 2.XUnicode Filenames and StreamsOther String Tool Changes in 3.XThe re Pattern-Matching ModuleThe struct Binary Data ModuleThe pickle Object Serialization ModuleXML Parsing ToolsChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- Why Manage Attributes?Inserting Code to Run on Attribute AccessPropertiesThe BasicsA First ExampleComputed AttributesCoding Properties with DecoratorsDescriptorsThe BasicsA First ExampleComputed AttributesUsing State Information in DescriptorsHow Properties and Descriptors Relate__getattr__ and __getattribute__The BasicsA First ExampleComputed Attributes__getattr__ and __getattribute__ ComparedManagement Techniques ComparedIntercepting Built-in Operation AttributesExample: Attribute ValidationsUsing Properties to ValidateUsing Descriptors to ValidateUsing __getattr__ to ValidateUsing __getattribute__ to ValidateChapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- What’s a Decorator?Managing Calls and InstancesManaging Functions and ClassesUsing and Defining DecoratorsWhy Decorators?The BasicsFunction DecoratorsClass DecoratorsDecorator NestingDecorator ArgumentsDecorators Manage Functions and Classes, TooCoding Function DecoratorsTracing CallsDecorator State Retention OptionsClass Blunders I: Decorating MethodsTiming CallsAdding Decorator ArgumentsCoding Class DecoratorsSingleton ClassesTracing Object InterfacesClass Blunders II: Retaining Multiple InstancesDecorators Versus Manager FunctionsWhy Decorators? (Revisited)Managing Functions and Classes DirectlyExample: “Private” and “Public” AttributesImplementing Private AttributesImplementation Details IGeneralizing for Public Declarations, TooImplementation Details IIOpen IssuesPython Isn’t About ControlExample: Validating Function ArgumentsThe GoalA Basic Range-Testing Decorator for Positional ArgumentsGeneralizing for Keywords and Defaults, TooImplementation DetailsOpen IssuesDecorator Arguments Versus Function AnnotationsOther Applications: Type Testing (If You Insist!)Chapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- To Metaclass or Not to MetaclassIncreasing Levels of “Magic”A Language of HooksThe Downside of “Helper” FunctionsMetaclasses Versus Class Decorators: Round 1The Metaclass ModelClasses Are Instances of typeMetaclasses Are Subclasses of TypeClass Statement ProtocolDeclaring MetaclassesDeclaration in 3.XDeclaration in 2.XMetaclass Dispatch in Both 3.X and 2.XCoding MetaclassesA Basic MetaclassCustomizing Construction and InitializationOther Metaclass Coding TechniquesInheritance and InstanceMetaclass Versus SuperclassInheritance: The Full StoryMetaclass MethodsMetaclass Methods Versus Class MethodsOperator Overloading in Metaclass MethodsExample: Adding Methods to ClassesManual AugmentationMetaclass-Based AugmentationMetaclasses Versus Class Decorators: Round 2Example: Applying Decorators to MethodsTracing with Decoration ManuallyTracing with Metaclasses and DecoratorsApplying Any Decorator to MethodsMetaclasses Versus Class Decorators: Round 3 (and Last)Chapter SummaryTest Your Knowledge: QuizTest Your Knowledge: Answers
- The Python ParadoxOn “Optional” Language FeaturesAgainst Disquieting ImprovementsComplexity Versus PowerSimplicity Versus ElitismClosing ThoughtsWhere to Go From HereEncore: Print Your Own Completion Certificate!
- Installing the Python InterpreterIs Python Already Present?Where to Get PythonInstallation StepsConfiguring PythonPython Environment VariablesHow to Set Configuration OptionsPython Command-Line ArgumentsPython 3.3 Windows Launcher Command LinesFor More Help
- The Unix LegacyThe Windows LegacyIntroducing the New Windows LauncherA Windows Launcher TutorialStep 1: Using Version Directives in FilesStep 2: Using Command-Line Version SwitchesStep 3: Using and Changing DefaultsPitfalls of the New Windows LauncherPitfall 1: Unrecognized Unix #! Lines FailPitfall 2: The Launcher Defaults to 2.XPitfall 3: The New PATH Extension OptionConclusions: A Net Win for Windows
- Major 2.X/3.X Differences3.X Differences3.X-Only ExtensionsGeneral Remarks: 3.X ChangesChanges in Libraries and ToolsMigrating to 3.XFifth Edition Python Changes: 2.7, 3.2, 3.3Changes in Python 2.7Changes in Python 3.3Changes in Python 3.2Fourth Edition Python Changes: 2.6, 3.0, 3.1Changes in Python 3.1Changes in Python 3.0 and 2.6Specific Language Removals in 3.0Third Edition Python Changes: 2.3, 2.4, 2.5Earlier and Later Python Changes
- Part I, Getting StartedPart II, Types and OperationsPart III, Statements and SyntaxPart IV, Functions and GeneratorsPart V, Modules and PackagesPart VI, Classes and OOPPart VII, Exceptions and Tools
Overview
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.
Complete with quizzes, exercises, and helpful illustrations, this easy-to-follow, self-paced tutorial gets you started with both Python 2.7 and 3.3— the latest releases in the 3.X and 2.X lines—plus all other releases in common use today. You’ll also learn some advanced language features that recently have become more common in Python code.
- Explore Python’s major built-in object types such as numbers, lists, and dictionaries
- Create and process objects with Python statements, and learn Python’s general syntax model
- Use functions to avoid code redundancy and package code for reuse
- Organize statements, functions, and other tools into larger components with modules
- Dive into classes: Python’s object-oriented programming tool for structuring code
- Write large programs with Python’s exception-handling model and development tools
- Learn advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing