ebook img

Introduction to Python for Science and Engineering PDF

389 Pages·2019·5.22 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Introduction to Python for Science and Engineering

Introduction to Python for Science and Engineering Series in Computational Physics Parallel Science and Engineering Applications: The Charm++ Approach Laxmikant V. Kale, Abhinav Bhatele Introduction to Numerical Programming: A Practical Guide for Scientists and Engi- neers Using Python and C/C++ Titus A. Beu Computational Problems for Physics: With Guided Solutions Using Python Rubin H. Landau, Manual José Páez Introduction to Python for Science and Engineering David J. Pine For more information about this series, please visit: https://www.crcpress.com/ Series-in-Computational-Physics/book-series/CRCSERCOMPHY Introduction to Python for Science and Engineering David J. Pine MATLAB® is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of MATLAB® software or related products does not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical approach or particular use of the MATLAB® software. CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2019 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed on acid-free paper International Standard Book Number-13: 978-1-138-58389-4 (Paperback) International Standard Book Number-13: 978-1-138-58390-0 (Hardback) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, trans- mitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright. com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging‑in‑Publication Data Names: Pine, David J., author. Title: Introduction to Python for science and engineering / by David J. Pine. Description: Boca Raton, Florida : CRC Press, [2019] | Series: Series in computational physics | Includes bibliographical references and index. Identifiers: LCCN 2018027880 (print) | LCCN 2018051956 (ebook) | ISBN 9780429506413 (eBook General) | ISBN 9780429014253 (eBook Adobe Reader) | ISBN 9780429014246 (eBook ePub) | ISBN 9780429014239 (eBook Mobipocket) | ISBN 9781138583894 (paperback : acid-free paper) | ISBN 9781138583900 (hardback : acid-free paper). Subjects: LCSH: Python (Computer program language) | Computer programming. | Engineering--Data processing. | Science--Data processing. Classification: LCC QA76.73.P98 (ebook) | LCC QA76.73.P98 P58 2019 (print) | DDC 005.13/3--dc23 LC record available at https://lccn.loc.gov/2018027880 Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com To Alex Pine who introduced me to Python Contents Preface xv AbouttheAuthor xix 1 Introduction 1 1.1 IntroductiontoPythonforScienceandEngineering . 1 2 LaunchingPython 3 2.1 InteractingwithPython . . . . . . . . . . . . . . . . . 3 2.2 InstallingPythononYourComputer . . . . . . . . . . 4 2.3 TheSpyderWindow . . . . . . . . . . . . . . . . . . . 4 2.4 TheIPythonPane . . . . . . . . . . . . . . . . . . . . . 4 2.4.1 Magiccommands . . . . . . . . . . . . . . . . . 6 2.4.2 Systemshellcommands . . . . . . . . . . . . . 8 2.4.3 Tabcompletion . . . . . . . . . . . . . . . . . . 8 2.4.4 Recapofcommands . . . . . . . . . . . . . . . . 9 2.5 InteractivePythonasaCalculator . . . . . . . . . . . 9 2.5.1 BinaryarithmeticoperationsinPython . . . . . 10 2.5.2 Typesofnumbers . . . . . . . . . . . . . . . . . 10 2.5.3 ImportantnoteonintegerdivisioninPython . 12 2.6 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.6.1 Namesandtheassignmentoperator . . . . . . 13 2.6.2 Legalandrecommendedvariablenames . . . . 14 2.6.3 ReservedwordsinPython . . . . . . . . . . . . 15 2.7 ScriptFilesandPrograms . . . . . . . . . . . . . . . . 16 2.7.1 Firstscriptingexample:TheEditorpane . . . . 16 2.8 PythonModules . . . . . . . . . . . . . . . . . . . . . . 18 2.8.1 Pythonmodulesandfunctions:Afirstlook . . 20 2.8.2 SomeNumPyfunctions . . . . . . . . . . . . . . 22 2.8.3 ScriptingExample2 . . . . . . . . . . . . . . . 23 2.8.4 Differentwaysofimportingmodules . . . . . . 24 2.9 GettingHelp:DocumentationinIPython . . . . . . . 26 vii viii Contents 2.10 Stand-aloneIPython . . . . . . . . . . . . . . . . . . . 26 2.10.1 WritingPythonscriptsinatexteditor . . . . . 27 2.11 ProgrammingErrors . . . . . . . . . . . . . . . . . . . 28 2.11.1 Pyflakes . . . . . . . . . . . . . . . . . . . . . . 28 2.11.2 Errorchecking . . . . . . . . . . . . . . . . . . . 29 2.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3 Strings,Lists,Arrays,andDictionaries 33 3.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 3.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.2.1 Slicinglists . . . . . . . . . . . . . . . . . . . . . 37 3.2.2 Therangefunction:Sequencesofnumbers . . . 38 3.2.3 Tuples . . . . . . . . . . . . . . . . . . . . . . . 39 3.2.4 Multidimensionallistsandtuples . . . . . . . . 40 3.3 NumPyArrays . . . . . . . . . . . . . . . . . . . . . . . 41 3.3.1 Creatingarrays(1-d) . . . . . . . . . . . . . . . 41 3.3.2 Mathematicaloperationswitharrays . . . . . . 43 3.3.3 Slicingandaddressingarrays . . . . . . . . . . 46 3.3.4 Fancyindexing:Booleanmasks . . . . . . . . . 47 3.3.5 Multi-dimensionalarraysandmatrices . . . . . 49 3.3.6 Differencesbetweenlistsandarrays . . . . . . 52 3.4 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . 53 3.5 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4 InputandOutput 61 4.1 KeyboardInput . . . . . . . . . . . . . . . . . . . . . . 61 4.2 ScreenOutput . . . . . . . . . . . . . . . . . . . . . . . 64 4.2.1 Formattingoutputwithstr.format() . . . . . 64 4.2.2 Printingarrays . . . . . . . . . . . . . . . . . . . 68 4.3 FileInput . . . . . . . . . . . . . . . . . . . . . . . . . 69 4.3.1 Readingdatafromatextfile . . . . . . . . . . . 69 4.3.2 ReadingdatafromanExcelfile:CSVfiles . . . 71 4.4 FileOutput . . . . . . . . . . . . . . . . . . . . . . . . 73 4.4.1 Writingdatatoatextfile . . . . . . . . . . . . . 73 4.4.2 WritingdatatoaCSVfile . . . . . . . . . . . . 76 4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Contents ix 5 ConditionalsandLoops 81 5.1 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . 82 5.1.1 if,elif,andelsestatements . . . . . . . . . . 82 5.1.2 Logicaloperators . . . . . . . . . . . . . . . . . 86 5.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 5.2.1 forloops . . . . . . . . . . . . . . . . . . . . . . 87 5.2.2 whileloops . . . . . . . . . . . . . . . . . . . . . 91 5.2.3 Loopsandarrayoperations . . . . . . . . . . . 93 5.3 ListComprehensions . . . . . . . . . . . . . . . . . . . 94 5.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 96 6 Plotting 99 6.1 AnInteractiveSessionwithPyPlot . . . . . . . . . . . 100 6.2 BasicPlotting . . . . . . . . . . . . . . . . . . . . . . . 102 6.2.1 Specifyinglineandsymboltypesandcolors . . 106 6.2.2 Errorbars . . . . . . . . . . . . . . . . . . . . . 108 6.2.3 Settingplottinglimitsandexcludingdata . . . 110 6.2.4 Subplots . . . . . . . . . . . . . . . . . . . . . . 113 6.3 LogarithmicPlots . . . . . . . . . . . . . . . . . . . . . 116 6.3.1 Semi-logplots . . . . . . . . . . . . . . . . . . . 116 6.3.2 Log-logplots . . . . . . . . . . . . . . . . . . . . 118 6.4 MoreAdvancedGraphicalOutput . . . . . . . . . . . 118 6.4.1 Analternativesyntaxforagridofplots . . . . 122 6.5 Plotswithmultipleaxes . . . . . . . . . . . . . . . . . 125 6.6 MathematicsandGreeksymbols . . . . . . . . . . . . 126 6.7 TheStructureofmatplotlib:OOPandAllThat . . . . 131 6.7.1 Thebackendlayer . . . . . . . . . . . . . . . . . 132 6.7.2 Theartistlayer. . . . . . . . . . . . . . . . . . . 135 6.7.3 ThePyPlot(scripting)layer . . . . . . . . . . . 137 6.8 ContourandVectorFieldPlots . . . . . . . . . . . . . 139 6.8.1 Makinga2Dgridofpoints . . . . . . . . . . . . 139 6.8.2 Contourplots . . . . . . . . . . . . . . . . . . . 140 6.8.3 Streamlineplots . . . . . . . . . . . . . . . . . . 144 6.9 Three-DimensionalPlots . . . . . . . . . . . . . . . . . 149 6.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 152 7 Functions 155 7.1 User-DefinedFunctions . . . . . . . . . . . . . . . . . 156 7.1.1 Loopingoverarraysinuser-definedfunctions . 158

Description:
Series in Computational PhysicsSteven A. Gottlieb and Rubin H. Landau, Series EditorsIntroduction to Python for Science and EngineeringThis guide offers a quick and incisive introduction to Python programming for anyone. The author has carefully developed a concise approach to using Python in any di
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.