Automate the Boring Stuff with Python Practical Programming for Total Beginners 2nd Edition Al Sweigart download
Automate the Boring Stuff with Python Practical Programming for Total Beginners 2nd Edition Al Sweigart download
https://ebookmeta.com/product/automate-the-boring-stuff-with-
python-practical-programming-for-total-beginners-2nd-edition-al-
sweigart/
https://ebookmeta.com/product/automate-the-boring-stuff-with-
python-practical-programming-for-total-beginners-1st-edition-al-
sweigart/
https://ebookmeta.com/product/automate-the-boring-stuff-with-
python-practical-programming-for-total-beginners-1st-edition-al-
sweigart-3/
https://ebookmeta.com/product/automate-the-boring-stuff-with-
python-practical-programming-for-total-beginners-1st-edition-al-
sweigart-2/
https://ebookmeta.com/product/cultural-revolution-manuscripts-
unofficial-entertainment-fiction-from-1970s-china-1st-edition-
lena-henningsen/
A TO Z INDIA OCTOBER 2022 3rd Edition A To Z India
https://ebookmeta.com/product/a-to-z-india-october-2022-3rd-
edition-a-to-z-india/
https://ebookmeta.com/product/tracks-vanessa-acton/
https://ebookmeta.com/product/managing-customer-experiences-in-
an-omnichannel-world-melody-of-online-and-offline-environments-
in-the-customer-journey-1st-edition-taskin-dirsehan/
https://ebookmeta.com/product/hardening-your-macs-keeping-apple-
computers-safe-at-the-hardware-and-software-level-1st-edition-
jesus-vigo-2/
https://ebookmeta.com/product/starting-over-blossom-ridge-
book-1-1st-edition-becca-jameson/
5 Day German Language Challenge Learn German In 5 Days
1st Edition Self
https://ebookmeta.com/product/5-day-german-language-challenge-
learn-german-in-5-days-1st-edition-self/
AUTOMATE THE BORING STUFF WITH
PYTHON
2ND EDITION
by Al Sweigart
San Francisco
AUTOMATE THE BORING STUFF WITH PYTHON, 2ND EDITION.
Copyright © 2020 by Al Sweigart.
All rights reserved. No part of this work may be reproduced or transmitted in any
form or by any means, electronic or mechanical, including photocopying, recording, or
by any information storage or retrieval system, without the prior written permission of
the copyright owner and the publisher.
Fifth printing
25 24 23 22 21 5 6 7 8 9
ISBN-10: 1-59327-992-2
ISBN-13: 978-1-59327-992-9
The Library of Congress Control Number for the first edition is: 2014953114
No Starch Press and the No Starch Press logo are registered trademarks of No Starch
Press, Inc. Other product and company names mentioned herein may be the
trademarks of their respective owners. Rather than use a trademark symbol with every
occurrence of a trademarked name, we are using the names only in an editorial fashion
and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The information in this book is distributed on an “As Is” basis, without warranty.
While every precaution has been taken in the preparation of this work, neither the
author nor No Starch Press, Inc. shall have any liability to any person or entity with
respect to any loss or damage caused or alleged to be caused directly or indirectly by
the information contained in it.
INTRODUCTION
Whom Is This Book For?
Conventions
What Is Programming?
What Is Python?
Programmers Don’t Need to Know Much Math
You Are Not Too Old to Learn Programming
Programming Is a Creative Activity
About This Book
Downloading and Installing Python
Downloading and Installing Mu
Starting Mu
Starting IDLE
The Interactive Shell
Installing Third-Party Modules
How to Find Help
Asking Smart Programming Questions
Summary
1
PYTHON BASICS
Entering Expressions into the Interactive Shell
The Integer, Floating-Point, and String Data Types
String Concatenation and Replication
Storing Values in Variables
Assignment Statements
Variable Names
Your First Program
Dissecting Your Program
Comments
The print() Function
The input() Function
Printing the User’s Name
The len() Function
The str(), int(), and float() Functions
Summary
Practice Questions
2
FLOW CONTROL
Boolean Values
Comparison Operators
Boolean Operators
Binary Boolean Operators
The not Operator
Mixing Boolean and Comparison Operators
Elements of Flow Control
Conditions
Blocks of Code
Program Execution
Flow Control Statements
if Statements
else Statements
elif Statements
while Loop Statements
break Statements
continue Statements
for Loops and the range() Function
Importing Modules
from import Statements
Ending a Program Early with the sys.exit() Function
A Short Program: Guess the Number
A Short Program: Rock, Paper, Scissors
Summary
Practice Questions
3
FUNCTIONS
def Statements with Parameters
Define, Call, Pass, Argument, Parameter
Return Values and return Statements
The None Value
Keyword Arguments and the print() Function
The Call Stack
Local and Global Scope
Local Variables Cannot Be Used in the Global Scope
Local Scopes Cannot Use Variables in Other Local Scopes
Global Variables Can Be Read from a Local Scope
Local and Global Variables with the Same Name
The global Statement
Exception Handling
A Short Program: Zigzag
Summary
Practice Questions
Practice Projects
The Collatz Sequence
Input Validation
4
LISTS
The List Data Type
Getting Individual Values in a List with Indexes
Negative Indexes
Getting a List from Another List with Slices
Getting a List’s Length with the len() Function
Changing Values in a List with Indexes
List Concatenation and List Replication
Removing Values from Lists with del Statements
Working with Lists
Using for Loops with Lists
The in and not in Operators
The Multiple Assignment Trick
Using the enumerate() Function with Lists
Using the random.choice() and random.shuffle() Functions
with Lists
Augmented Assignment Operators
Methods
Finding a Value in a List with the index() Method
Adding Values to Lists with the append() and insert() Methods
Removing Values from Lists with the remove() Method
Sorting the Values in a List with the sort() Method
Reversing the Values in a List with the reverse() Method
Example Program: Magic 8 Ball with a List
Sequence Data Types
Mutable and Immutable Data Types
The Tuple Data Type
Converting Types with the list() and tuple() Functions
References
Identity and the id() Function
Passing References
The copy Module’s copy() and deepcopy() Functions
A Short Program: Conway’s Game of Life
Summary
Practice Questions
Practice Projects
Comma Code
Coin Flip Streaks
Character Picture Grid
5
DICTIONARIES AND STRUCTURING DATA
The Dictionary Data Type
Dictionaries vs. Lists
The keys(), values(), and items() Methods
Checking Whether a Key or Value Exists in a Dictionary
The get() Method
The setdefault() Method
Pretty Printing
Using Data Structures to Model Real-World Things
A Tic-Tac-Toe Board
Nested Dictionaries and Lists
Summary
Practice Questions
Practice Projects
Chess Dictionary Validator
Fantasy Game Inventory
List to Dictionary Function for Fantasy Game Inventory
6
MANIPULATING STRINGS
Working with Strings
String Literals
Indexing and Slicing Strings
The in and not in Operators with Strings
Putting Strings Inside Other Strings
Useful String Methods
The upper(), lower(), isupper(), and islower() Methods
The isX() Methods
The startswith() and endswith() Methods
The join() and split() Methods
Splitting Strings with the partition() Method
Justifying Text with the rjust(), ljust(), and center() Methods
Removing Whitespace with the strip(), rstrip(), and lstrip()
Methods
Numeric Values of Characters with the ord() and chr() Functions
Copying and Pasting Strings with the pyperclip Module
Project: Multi-Clipboard Automatic Messages
Step 1: Program Design and Data Structures
Step 2: Handle Command Line Arguments
Step 3: Copy the Right Phrase
Project: Adding Bullets to Wiki Markup
Step 1: Copy and Paste from the Clipboard
Step 2: Separate the Lines of Text and Add the Star
Step 3: Join the Modified Lines
A Short Progam: Pig Latin
Summary
Practice Questions
Practice Projects
Table Printer
Zombie Dice Bots