Catalog

Record Details

Catalog Search



Automate the boring stuff with Python : practical programming for total beginners / by Al Sweigart.

Sweigart, Al. (Author).

Record details

  • ISBN: 9781593275990
  • Publisher: San Francisco : No Starch Press, [2015].

Content descriptions

General Note:
Incluye índice.
Immediate Source of Acquisition Note:
IPICYT ; Compra/R.3357 ; 2016.
Language Note:
En inglés.
Subject: Phyton (Lenguaje de programación de computadores).
Lenguajes de programación (Computadores).

Available copies

  • 1 of 1 copy available at IPICYT.

Holds

  • 0 current holds with 1 total copy.
Show All Copies
Location Call Number / Copy Notes Barcode Shelving Location Status Due Date
Biblioteca Ipicyt QA76.73.P98S9 A8 2015 LCI00968 Coleccion General Available -

Acknowledgmentsxxiii
Introduction1
Whom Is This Book For?2
Conventions2
What Is Programming?3
What Is Python?4
Programmers Don't Need to Know Much Math4
Programming Is a Creative Activity5
About This Book5
Downloading and Installing Python6
Starting IDLE7
The Interactive Shell8
How to Find Help8
Asking Smart Programming Questions9
Summary10
Part I. Python Programming Basics11
1. Python Basics13
Entering Expressions into the Interactive Shell14
The Integer, Floating-Point, and String Data Types16
String Concatenation and Replication17
Storing Values in Variables18
Assignment Statements18
Variable Names20
Your First Program21
Dissecting Your Program22
Comments23
The print() Function23
The input() Function23
Printing the User's Name24
The len() Function24
The str(), int(), and float() Functions25
Summary28
Practice Questions28
2. Flow Control31
Boolean Values32
Comparison Operators33
Boolean Operators35
Binary Boolean Operators35
The not Operator36
Mixing Boolean and Comparison Operators36
Elements of Flow Control37
Conditions37
Blocks of Code37
Program Execution38
Flow Control Statements38
if Statements38
else Statements39
elif Statements40
while Loop Statements45
break Statements49
continue Statements50
for Loops and the range() Function53
Importing Modules57
from import Statements58
Ending a Program Early with sys.exit()58
Summary58
Practice Questions59
3. Functions61
def Statements with Parameters63
Return Values and return Statements63
The None Value65
Keyword Arguments and print()65
Local and Global Scope67
Local Variables Cannot Be Used in the Global Scope67
Local Scopes Cannot Use Variables in Other Local Scopes68
Global Variables Can Be Read from a Local Scope69
Local and Global Variables with the Same Name69
The global Statement70
Exception Handling72
A Short Program: Guess the Number74
Summary76
Practice Questions76
Practice Projects77
The Collatz Sequence77
Input Validation77
4. Lists79
The List Data Type80
Getting Individual Values in a List with Indexes80
Negative Indexes82
Getting Sublists with Slices82
Getting a List's Length with len()83
Changing Values in a List with Indexes83
List Concatenation and List Replication83
Removing Values from Lists with del Statements84
Working with Lists84
Using for Loops with Lists86
The in and not in Operators87
The Multiple Assignment Trick87
Augmented Assignment Operators88
Methods89
Finding a Value in a List with the index() Method89
Adding Values to Lists with the append() and insert() Methods89
Removing Values from Lists with remove()90
Sorting the Values in a List with the sort() Method91
Example Program: Magic 8 Ball with a List92
List-like Types: Strings and Tuples93
Mutable and Immutable Data Types94
The Tuple Data Type96
Converting Types with the list() and tuple() Functions97
References97
Passing References100
The copy Module's copy() and deepcopy() Functions100
Summary101
Practice Questions102
Practice Projects102
Comma Code102
Character Picture Grid103
5. Dictionaries and Structuring Data105
The Dictionary Data Type105
Dictionaries vs. Lists106
The keys(), values(), and items() Methods107
Checking Whether a Key or Value Exists in a Dictionary109
The get() Method109
The setdefault() Method110
Pretty Printing111
Using Data Structures to Model Real-World Things112
A Tic-Tac-Toe Board113
Nested Dictionaries and Lists117
Summary119
Practice Questions119
Practice Projects120
Fantasy Game Inventory120
List to Dictionary Function for Fantasy Game Inventory120
6. Manipulating Strings123
Working with Strings123
String Literals124
Indexing and Slicing Strings126
The in and not in Operators with Strings127
Useful String Methods127
The upper(), lower(), isupper(), and islower() String Methods128
The isX String Methods129
The startswith() and endswith() String Methods131
The join() and split() String Methods132
Justifying Text with rjust(), ljust(), and center()133
Removing Whitespace with strip(), rstrip(), and lstrip()134
Copying and Pasting Strings with the pyperclip Module135
Project. Password Locker136
Step 1: Program Design and Data Structures136
Step 2: Handle Command Line Arguments137
Step 3: Copy the Right Password137
Project. Adding Bullets to Wiki Markup139
Step 1: Copy and Paste from the Clipboard139
Step 2: Separate the Lines of Text and Add the Star140
Step 3: Join the Modified Lines141
Summary141
Practice Questions142
Practice Project142
Table Printer142
Part II. Automating Tasks145
7. Pattern Matching with Regular Expressions147
Finding Patterns of Text Without Regular Expressions148
Finding Patterns of Text with Regular Expressions150
Creating Regex Objects150
Matching Regex Objects151
Review of Regular Expression Matching152
More Pattern Matching with Regular Expressions152
Grouping with Parentheses152
Matching Multiple Groups with the Pipe153
Optional Matching with the Question Mark154
Matching Zero or More with the Star155
Matching One or More with the Plus155
Matching Specific Repetitions with Curly Brackets156
Greedy and Nongreedy Matching156
The findall() Method157
Character Classes158
Making Your Own Character Classes159
The Caret and Dollar Sign Characters159
The Wildcard Character160
Matching Everything with Dot-Star161
Matching Newlines with the Dot Character162
Review of Regex Symbols162
Case-Insensitive Matching163
Substituting Strings with the sub() Method163
Managing Complex Regexes164
Combining re.IGNORECASE, re.DOTALL, and re.VERBOSE164
Project: Phone Number and Email Address Extractor165
Step 1: Create a Regex for Phone Numbers166
Step 2: Create a Regex for Email Addresses166
Step 3: Find All Matches in the Clipboard Text167
Step 4: Join the Matches into a String for the Clipboard168
Running the Program169
Ideas for Similar Programs169
Summary169
Practice Questions170
Practice Projects171
Strong Password Detection171
Regex Version of strip()171
8. Reading and Writing Files173
Files and File Paths173
Backslash on Windows and Forward Slash on OS X and Linux174
The Current Working Directory175
Absolute vs. Relative Paths175
Creating New Folders with os.makedirs()176
The os.path Module177
Handling Absolute and Relative Paths177
Finding File Sizes and Folder Contents179
Checking Path Validity180
The File Reading/Writing Process180
Opening Files with the open() Function181
Reading the Contents of Files182
Writing to Files183
Saving Variables with the shelve Module184
Saving Variables with the pprint.pformat() Function185
Project: Generating Random Quiz Files186
Step 1: Store the Quiz Data in a Dictionary187
Step 2: Create the Quiz File and Shuffle the Question Order188
Step 3: Create the Answer Options189
Step 4: Write Content to the Quiz and Answer Key Files189
Project: Multiclipboard191
Step 1: Comments and Shelf Setup192
Step 2: Save Clipboard Content with a Keyword192
Step 3: List Keywords and Load a Keyword's Content193
Summary194
Practice Questions194
Practice Projects194
Extending the Multiclipboard194
Mad Libs195
Regex Search195
9. Organizing Files197
The shutil Module198
Copying Files and Folders198
Moving and Renaming Files and Folders199
Permanently Deleting Files and Folders200
Safe Deletes with the send2trash Module201
Walking a Directory Tree202
Compressing Files with the zipfile Module203
Reading ZIP Files204
Extracting from ZIP Files205
Creating and Adding to ZIP Files205
Project: Renaming Files with American-Style Dates to European-Style Dates206
Step 1: Create a Regex for American-Style Dates206
Step 2: Identify the Date Parts from the Filenames207
Step 3: Form the New Filename and Rename the Files209
Ideas for Similar Programs209
Project: Backing Up a Folder into a ZIP File209
Step 1: Figure Out the ZIP File's Name210
Step 2: Create the New ZIP File211
Step 3: Walk the Directory Tree and Add to the ZIP File211
Ideas for Similar Programs212
Summary212
Practice Questions213
Practice Projects213
Selective Copy213
Deleting Unneeded Files213
Filling in the Gaps214
10. Debugging215
Raising Exceptions216
Getting the Traceback as a String217
Assertions219
Using an Assertion in a Traffic Light Simulation219
Disabling Assertions221
Logging221
Using the logging Module221
Don't Debug with print()223
Logging Levels223
Disabling Logging224
Logging to a File225
IDLE's Debugger225
Go226
Step226
Over226
Out227
Quit227
Debugging a Number Adding Program227
Breakpoints229
Summary231
Practice Questions231
Practice Project232
Debugging Coin Toss232
11. Web Scraping233
Project: maplt.py with the webbrowser Module234
Step 1: Figure Out the URL234
Step 2: Handle the Command Line Arguments235
Step 3: Handle the Clipboard Content and Launch the Browser236
Ideas for Similar Programs236
Downloading Files from the Web with the requests Module237
Downloading a Web Page with the requests.get() Function237
Checking for Errors238
Saving Downloaded Files to the Hard Drive239
HTML240
Resources for Learning HTML240
A Quick Refresher240
Viewing the Source HTML of a Web Page241
Opening Your Browser's Developer Tools242
Using the Developer Tools to Find HTML Elements244
Parsing HTML with the BeautifulSoup Module245
Creating a BeautifulSoup Object from HTML245
Finding an Element with the select() Method246
Getting Data from an Element's Attributes248
Project: "I'm Feeling Lucky" Google Search248
Step 1: Get the Command Line Arguments and Request the Search Page249
Step 2: Find All the Results249
Step 3: Open Web Browsers for Each Result250
Ideas for Similar Programs251
Project: Downloading All XKCD Comics251
Step 1: Design the Program252
Step 2: Download the Web Page253
Step 3: Find and Download the Comic Image254
Step 4: Save the Image and Find the Previous Comic255
Ideas for Similar Programs256
Controlling the Browser with the selenium Module256
Starting a Selenium-Controlled Browser256
Finding Elements on the Page257
Clicking the Page259
Filling Out and Submitting Forms259
Sending Special Keys260
Clicking Browser Buttons261
More Information on Selenium261
Summary261
Practice Questions261
Practice Projects262
Command Line Emailer262
Image Site Downloader263
2048263
Link Verification263
12. Working with Excel Spreadsheets265
Excel Documents266
Installing the openpyxl Module266
Reading Excel Documents266
Opening Excel Documents with OpenPyXL267
Getting Sheets from the Workbook268
Getting Cells from the Sheets268
Converting Between Column Letters and Numbers270
Getting Rows and Columns from the Sheets270
Workbooks, Sheets, Cells272
Project: Reading Data from a Spreadsheet272
Step 1: Read the Spreadsheet Data273
Step 2: Populate the Data Structure274
Step 3: Write the Results to a File275
Ideas for Similar Programs276
Writing Excel Documents277
Creating and Saving Excel Documents277
Creating and Removing Sheets278
Writing Values to Cells278
Project: Updating a Spreadsheet279
Step 1: Set Up a Data Structure with the Update Information280
Step 2: Check All Rows and Update Incorrect Prices281
Ideas for Similar Programs281
Setting the Font Style of Cells282
Font Objects282
Formulas284
Adjusting Rows and Columns285
Setting Row Height and Column Width285
Merging and Unmerging Cells286
Freeze Panes287
Charts288
Summary290
Practice Questions291
Practice Projects291
Multiplication Table Maker291
Blank Row Inserter292
Spreadsheet Cell Inverter292
Text Files to Spreadsheet293
Spreadsheet to Text Files293
13. Working with PDF and Word Documents295
PDF Documents295
Extracting Text from PDFs296
Decrypting PDFs297
Creating PDFs298
Project: Combining Select Pages from Many PDFs303
Step 1: Find All PDF Files304
Step 2: Open Each PDF304
Step 3: Add Each Page305
Step 4: Save the Results305
Ideas for Similar Programs306
Word Documents306
Reading Word Documents307
Getting the Full Text from a .docx File308
Styling Paragraph and Run Objects309
Creating Word Documents with Nondefault Styles310
Run Attributes311
Writing Word Documents312
Adding Headings314
Adding Line and Page Breaks315
Adding Pictures315
Summary316
Practice Questions316
Practice Projects317
PDF Paranoia317
Custom Invitations as Word Documents317
Brute-Force PDF Password Breaker318
14. Working with CSV Files and JSON Data319
The csv Module320
Reader Objects321
Reading Data from Reader Objects in a for Loop322
Writer Objects322
The Delimiter and Lineterminator Keyword Arguments323
Project: Removing the Header from CSV Files324
Step 1: Loop Through Each CSV File325
Step 2: Read in the CSV File325
Step 3: Write Out the CSV File Without the First Row326
Ideas for Similar Programs327
JSON and APIs327
The json Module328
Reading JSON with the loads() Function328
Writing JSON with the dumps() Function329
Project: Fetching Current Weather Data329
Step 1: Get Location from the Command Line Argument330
Step 2: Download the JSON Data330
Step 3: Load JSON Data and Print Weather331
Ideas for Similar Programs332
Summary333
Practice Questions333
Practice Project333
Excel-to-CSV Converter333
15. Keeping Time, Scheduling Tasks, and Launching Programs335
The time Module336
The time.time() Function336
The time.sleep() Function337
Rounding Numbers338
Project: Super Stopwatch338
Step 1: Set Up the Program to Track Times339
Step 2: Track and Print Lap Times339
Ideas for Similar Programs340
The datetime Module341
The timedelta Data Type342
Pausing Until a Specific Date344
Converting Datetime Objects into Strings344
Converting Strings into Datetime Objects345
Review of Python's Time Functions346
Multithreading347
Passing Arguments to the Thread's Target Function348
Concurrency Issues349
Project: Multithreaded XKCD Downloader350
Step 1: Modify the Program to Use a Function350
Step 2: Create and Start Threads351
Step 3: Wait for All Threads to End352
Launching Other Programs from Python352
Passing Command Line Arguments to Popen()354
Task Scheduler, launchd, and cron354
Opening Websites with Python355
Running Other Python Scripts355
Opening Files with Default Applications355
Project: Simple Countdown Program357
Step 1: Count Down357
Step 2: Play the Sound File357
Ideas for Similar Programs358
Summary358
Practice Questions359
Practice Projects359
Prettified Stopwatch360
Scheduled Web Comic Downloader360
16. Sending Email and Text Messages361
SMTP362
Sending Email362
Connecting to an SMTP Server363
Sending the SMTP 'Hello' Message364
Starting TLS Encryption364
Logging in to the SMTP Server364
Sending an Email365
Disconnecting from the SMTP Server366
IMAP366
Retrieving and Deleting Emails with IMAP366
Connecting to an IMAP Server367
Logging in to the IMAP Server368
Searching for Email368
Fetching an Email and Marking It As Read372
Getting Email Addresses from a Raw Message373
Getting the Body from a Raw Message374
Deleting Emails375
Disconnecting from the IMAP Server375
Project: Sending Member Dues Reminder Emails376
Step 1: Open the Excel File376
Step 2: Find All Unpaid Members378
Step 3: Send Customized Email Reminders378
Sending Text Messages with Twilio380
Signing Up for a Twilio Account380
Sending Text Messages381
Project: "Just Text Me" Module383
Summary384
Practice Questions384
Practice Projects385
Random Chore Assignment Emailer385
Umbrella Reminder385
Auto Unsubscriber385
Controlling Your Computer Through Email386
17. Manipulating Images387
Computer Image Fundamentals388
Colors and RGBA Values388
Coordinates and Box Tuples389
Manipulating Images with Pillow390
Working with the Image Data Type392
Cropping Images393
Copying and Pasting Images onto Other Images394
Resizing an Image397
Rotating and Flipping Images398
Changing Individual Pixels400
Project: Adding a Logo401
Step 1: Open the Logo Image401
Step 2: Loop Over All Files and Open Images402
Step 3: Resize the Images403
Step 4: Add the Logo and Save the Changes404
Ideas for Similar Programs406
Drawing on Images406
Drawing Shapes406
Drawing Text408
Summary410
Practice Questions410
Practice Projects411
Extending and Fixing the Chapter Project Programs411
Identifying Photo Folders on the Hard Drive411
Custom Seating Cards412
18. Controlling the Keyboard and Mouse with GUI Automation413
Installing the PyAutoGUI Module414
Staying on Track414
Shutting Down Everything by Logging Out414
Pauses and Fail-Safes415
Controlling Mouse Movement415
Moving the Mouse416
Getting the Mouse Position417
Project: "Where Is the Mouse Right Now?"417
Step 1: Import the Module418
Step 2: Set Up the Quit Code and Infinite Loop418
Step 3: Get and Print the Mouse Coordinates418
Controlling Mouse Interaction419
Clicking the Mouse420
Dragging the Mouse420
Scrolling the Mouse422
Working with the Screen423
Getting a Screenshot423
Analyzing the Screenshot424
Project: Extending the mouseNow Program424
Image Recognition425
Controlling the Keyboard426
Sending a String from the Keyboard426
Key Names427
Pressing and Releasing the Keyboard428
Hotkey Combinations429
Review of the PyAutoGUI Functions430
Project: Automatic Form Filler430
Step 1: Figure Out the Steps432
Step 2: Set Up Coordinates432
Step 3: Start Typing Data434
Step 4: Handle Select Lists and Radio Buttons435
Step 5: Submit the Form and Wait436
Summary437
Practice Questions438
Practice Projects438
Looking Busy438
Instant Messenger Bot438
Game-Playing Bot Tutorial439
A. Installing Third-Party Modules441
The pip Tool441
Installing Third-Party Modules442
B. Running Programs443
Shebang Line443
Running Python Programs on Windows444
Running Python Programs on OS X and Linux445
Running Python Programs with Assertions Disabled445
C. Answers to the Practice Questions447
Chapter 1448
Chapter 2448
Chapter 3450
Chapter 4450
Chapter 5451
Chapter 6451
Chapter 7452
Chapter 8453
Chapter 9453
Chapter 10454
Chapter 11455
Chapter 12456
Chapter 13456
Chapter 14457
Chapter 15457
Chapter 16458
Chapter 17458
Chapter 18458
Index461

Additional Resources