Quick+Python+3+(David+L.+Matuszek)+(Z-Library)

Author: Unknown Author

技术

No Description

📄 File Format: PDF
💾 File Size: 7.3 MB
343
Views
65
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
Quick Python 3 Are you a programmer who wants to get started quickly in a new language? This book is for you. Are you a novice who wants to learn to program? This book is not for you. Are you a Python programmer who needs encyclopaedic information? This book is not for you. Like any mainstream language, Python has loops, if statements, assignment statements, functions, etc. I’ll show you what these look like in Python. I won’t waste your time telling you what they’re good for. Python has features you may not be familiar with—iterators, list comprehensions, maybe even dictionaries. I’ll spend more time on these. I’ll cover some of the library functions I found most immediately useful and tell you where to find more. In short, this book will help you hit the ground running. Next week, you’ll be ready to buy that Python encyclopedia.
📄 Page 3
(This page has no text content)
📄 Page 4
Quick Python 3 David Matuszek
📄 Page 5
First edition published 2023 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN CRC Press is an imprint of Taylor & Francis Group, LLC © 2023 David Matuszek 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, transmitted, 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, access www. copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermissions@tandf.co.uk 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: Matuszek, David L., author. Title: Quick Python 3 / David Matuszek. Other titles: Quick Python three Description: First edition. | Boca Raton : CRC Press, 2023. | Series: Quick programming series | Includes bibliographical references and index. Identifiers: LCCN 2022036020 (print) | LCCN 2022036021 (ebook) | ISBN 9781032410920 (hbk) | ISBN 9781032410913 (pbk) | ISBN 9781003356219 (ebk) Subjects: LCSH: Python (Computer program language) | Computer programming. Classification: LCC QA76.73.P98 M385 2023 (print) | LCC QA76.73.P98 (ebook) | DDC 005.13/3‐‐dc23/eng/20220930 LC record available at https://lccn.loc.gov/2022036020 LC ebook record available at https://lccn.loc.gov/2022036021 ISBN: 978-1-032-41092-0 (hbk) ISBN: 978-1-032-41091-3 (pbk) ISBN: 978-1-003-35621-9 (ebk) DOI: 10.1201/9781003356219 Typeset in Minion by MPS Limited, Dehradun
📄 Page 6
To all my students, past, present, and future
📄 Page 7
(This page has no text content)
📄 Page 8
Contents Author, xi Preface, xiii Chapter 1 ■ The Absolute Bare Minimum 1 1.1 Python and IDEs 1 1.2 IDLE 2 1.3 Variables 3 1.4 Important Data Types 3 1.5 Basic Arithmetic 4 1.6 Comparisons 6 1.7 Booleans 6 1.8 Lists 7 1.9 Strings 8 1.10 Comments 9 1.11 Statement Types 9 1.11.1 Assignment Statements 10 1.11.2 Print “Statements” 10 1.11.3 If Statements 11 1.11.4 While Loops 11 vii
📄 Page 9
1.11.5 For Loops 12 1.11.6 Import Statements 13 1.12 Input from the User 14 1.13 Functions 14 1.14 Syntax 15 1.15 Order of Execution 16 1.16 Summary 16 1.17 Wait, That’s It? 17 Chapter 2 ■ Better Tools 19 2.1 Object Notation 19 2.2 Lists 20 2.3 Tuples 22 2.4 Sets 23 2.5 Dictionaries 25 2.6 String Methods 27 2.7 Loops for Objects 28 2.7.1 Looping over Lists 28 2.7.2 Looping over Sets 29 2.7.3 Looping over Dictionaries 29 2.8 Handing Exceptions 30 2.9 Type Conversions 33 2.10 Scope 34 2.11 File I/O 36 2.12 Pickling 38 Chapter 3 ■ Classes 39 3.1 Classes and Inheritance 39 3.2 Constructors and Self 41 3.3 Subclasses 44 viii ▪ Contents
📄 Page 10
3.4 Printing Objects 45 3.5 Comparing Objects 47 3.6 Bindings 48 3.7 Shallow and Deep Copies 50 Chapter 4 ■ Getting Fancy 53 4.1 Statements 53 4.2 Identifiers 56 4.3 Type Hints 57 4.4 Numbers 58 4.5 Strings 59 4.6 F-strings 60 4.7 Bit Operations 61 4.8 List Comprehensions 62 4.9 Iterators 63 4.10 Generators 66 4.11 Parameters and Arguments 68 4.12 Functional Programming 69 Chapter 5 ■ Testing 73 5.1 Philosophy 74 5.2 Doctest 75 5.3 Unit Testing 77 5.4 Unit Test Example 80 5.5 Test Suites 81 Chapter 6 ■ Graphical User Interfaces 83 6.1 Dialogs 83 6.2 Tkinter 84 6.3 Creating Widgets 85 Contents ▪ ix
📄 Page 11
6.4 Putting Widgets Into the GUI 87 6.5 Tkinter Example 89 ■ Afterword 91 Appendix A: string methods, 93 Appendix B: Numerical Functions, 97 Appendix C: Statistics, 101 Appendix D: Functions on Iterables, 103 Appendix E: Operating System Commands, 105 Appendix F: Unit Test Methods, 107 Index, 109 x ▪ Contents
📄 Page 12
Author I ’M DAVID MATUSZEK, known to most of my students as “Dr. Dave.” I wrote my first program on punched cards in 1963, and immediately got hooked. I taught my first computer classes in 1970 as a graduate student in computer science at The University of Texas in Austin. I eventually received my PhD from there, and I’ve been teaching ever since. Admittedly, I spent over a dozen years in industry, but even then I taught as an adjunct for Villanova University. I finally escaped from industry and joined the Villanova faculty full time for a few years, and then moved to the University of Pennsylvania, where I directed a master’s program (MCIT, master’s in computer and information technology) for students coming into computer science from another discipline. Throughout my career, my main interests have been in artificial intelligence (AI) and programming languages. I’ve used a lot of programming languages. I retired in 2017, but I can’t stop teaching, so I’m writing a series of “quick start” books on programming and programming languages. I’ve also written two science fiction novels, Ice xi
📄 Page 13
Jockey and All True Value, and I expect to write more. Check them out! And hey, if you’re a former student of mine, drop me a note. I’d love to hear from you! david.matuszek@gmail.com xii ▪ Author
📄 Page 14
Preface T HE PURPOSE OF THIS BOOK IS TO GET YOU, a programmer, up and productive in Python as quickly as possible. Don’t buy this book if: • You aren’t already a programmer (in some language), or • You want a comprehensive guide to Python. With that out of the way, let’s get started. xiii
📄 Page 15
(This page has no text content)
📄 Page 16
C H A P T E R 1 The Absolute Bare Minimum 1.1 PYTHON AND IDEs Go to python.org and download the most recent version of Python (it’s free). Install it on your computer as you would any other program. While you’re there, you might want to explore the online doc- umentation. A couple of clicks will bring you to docs.python. org/3/, and from there the Library Reference link will bring you to a long list of modules, each containing numerous useful methods. Included in your Python download you will find a simple IDE (Integrated Development Environment) called IDLE. You have four choices: • You can spend five minutes learning how to use IDLE. This is what I recommend if you’re in a hurry. DOI: 10.1201/9781003356219-1 1
📄 Page 17
• If you are already familiar with a professional IDE such as Eclipse, IntelliJ IDEA, NetBeans, or Jupyter Notebook, there will be a choice of Python plugins you can use. You can spend an indefinite amount of time choosing, downloading, installing, and configuring your new plugin. This is what I recommend after you have gained some familiarity with Python. • You can run Python from the command line. • There are several online Python compiler/editors that you can use (search for “Python online”). These are great for just trying a few things out. 1.2 IDLE When you run IDLE, you get a “Shell” window, into which you can type expressions or statements at the >>> prompt. • If you type an expression such as 2+2, IDLE will respond with 4 and a new prompt. • If you enter a statement such as x=2+2, IDLE responds with just a new prompt. (You can see the value assigned by entering the expression x.) This is a great way to test small amounts of code. To write complete programs, choose File > New File and enter your code there. You have to save the file before you can run it. The extension .py will be added automatically to the file name, or you can add it yourself. Run your program by choosing Run > Run Module or by hitting F5. Pro tip: Every time you make a change to your program, you are prompted to save it again before you can run it. You can skip this step by going to Preferences > General and, where it says At Start of Run (F5), choose No Prompt. 2 ▪ Quick Python 3
📄 Page 18
Spend a few minutes looking over the menus; there are things in there that can save you time. Notice that the menus differ ac- cording to whether you are in a shell window or a file window. 1.3 VARIABLES Variables do not have a type, and do not have to be declared. Variables come into existence when you assign a value to them. Example: best_value = 36 Convention: Variable names begin with a lowercase letter or (in special cases) an underscore. For multi-word variable names, most programmers use underscores between words (as above), but some use “camelCase” (sometimes called “studlyCaps”) such as bestValue. Caution: Python’s built-in type names and function names are not reserved words, and it is easy to override them accidentally. For example, if you use the name list or abs for a variable, you can no longer use those names to create lists or find absolute values. 1.4 IMPORTANT DATA TYPES The basic data types are similar to what you’re already used to: • int (integers): 73, -15, 12345678901234567890. Integers may be arbitrarily large. • float (real numbers): 3.1416, 6.022e+23. • bool (logicals, or booleans): True and False. Note that these are capitalized. • str (strings): "Hello World", 'Goodbye'. You can use either single or double quotes. You can insert single quotes The Absolute Bare Minimum ▪ 3
📄 Page 19
inside double-quoted strings, and vice versa. There is no separate “character” type. • list (array-like lists): [1, 2, "Buckle my shoe"]. Lists aren’t exactly arrays, but you can treat them as such. Lists are zero indexed, so if my_list holds the above list, my_list[0] is 1. There is also a simple but unusual type: • NoneType, with the single value None. None is an actual value that can be assigned to variables; it is not a default value for variables that have not yet been assigned a value. In Python, every function returns a value, and None is most often seen as the result of a function that does not explicitly return a value. Other data types (tuples, sets, and dictionaries) will be discussed later. 1.5 BASIC ARITHMETIC • Add (+), subtract (-), multiply (∗), and exponentiation (∗∗). If both operands are integers, the result is an integer, otherwise the result is a float. • Divide (/). The result of a division using/is always a float. • Integer divide (//). The result of dividing two integers is the integer part of the result, rounded down. If either or both op- erants of // are floats, the result is a float, rounded down to the nearest integer value. Thus, 10 // 4 is 2, while 10.0 // 4 is 2.0. • Modulus (%). The result of “modding” two integers is the remainder of the division. Thus, 20 % 7 is 6, because 7 goes into 20 twice, with 6 left over. If one or both operands of % are floats, the result is a float. The % operator has nothing to do with percentages. 4 ▪ Quick Python 3
📄 Page 20
• Order of precedence: In a complex expression, • Exponentiations are done first, then multiplications and divisions (including modulus), then additions and subtractions. • Multiple exponentiations are done right to left; that is, 2∗∗3∗∗4 means 2∗∗(3∗∗4). • Other operations of the same precedence are done left to right, so 10–5–2 means (10–5)–2. • If in doubt, use parentheses to control the order of operations. Except in the case of / (which always yields a float), the result of an operation will be an integer if both operands are integer, otherwise it will be a float. Caution: Languages do not all agree what the results should be when using the // or % operators with nega- tive numbers! Avoid this situation or, if you cannot avoid it, experiment until you are sure of the rules in Python. Then add an appropriate comment to your code. Style: Put spaces around all arithmetic and comparison operators, including the assignment operator (=). This makes your code easier to read. Conversions between types can be made with the float, int, round, and str functions. For example: • float(123), float("123"), and float("123.0") all return the floating-point number 123.0. The Absolute Bare Minimum ▪ 5
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List