Get ready to take on Python with a practical and job-focused guide
Job Ready Python offers readers a straightforward and elegant approach to learning Python that emphasizes hands-on and employable skills you can apply to real-world environments immediately.
Based on the renowned mthree Global Academy and Software Guild training program, this book will get you up to speed in the basics of Python, loops and data structures, object-oriented programming, and data processing. You’ll also get:
- Thorough discussions of Extract, Transform, and Load (ETL) scripting in Python
- Explorations of databases, including MySQL, and MongoDB - all commonly used database platforms in the field
- Simple, step-by-step approaches to dealing with dates and times, CSV files, and JSON files
Ideal for Python newbies looking to make a transition to an exciting new career, Job Ready Python also belongs on the bookshelves of Python developers hoping to brush up on the fundamentals with an authoritative and practical new handbook.
Table of Contents
About the Authors v
About the Technical Writer v
About the Technical Editor v
Acknowledgments vi
Introduction xvii
Part I: Getting Started with Python 1
Lesson 1: Setting Up a Python Programming Environment 3
Python Overview 4
Using Replit Online 4
Getting Started with Jupyter Notebook 14
A Quick Look at Visual Studio Code 21
Using Python from the Command Line 24
Summary 26
Exercises 26
Lesson 2: Understanding Programming Basics 29
The Future of Computer Programming 30
Programming Languages 32
Data Types and Variables 37
Variables 40
Constants 44
Summary 46
Exercises 46
Lesson 3: Exploring Basic Python Syntax 49
Using with Single- Line Commands 51
Using Semicolons 52
Continuing with Backslash 54
Working with Case Structure 55
Adding Comments 56
Using the Input Function 57
Storing Input 59
Understanding Variable Types 61
Displaying Variable Values 62
Naming Variables 64
Summary 65
Exercises 65
Lesson 4: Working with Basic Python Data Types 69
Review of Data Types 70
Number Data Types 70
Identifying Data Types 72
Mathematical Operations 74
Pemdas 77
Common Math Functions 81
Math Library Functions 83
Using Numbers with User Input 86
Boolean Types and Boolean Operations 89
Logic Operations 92
Comparative Operators 95
Summary 96
Exercises 97
Lesson 5: Using Python Control Statements 101
Control Structures Review 101
Understanding Sequence Control Structure 102
Understanding Selection Statements 103
Understanding Conditional Statements 106
If- Else Statements 108
Working with Nested Conditions 109
Embedding Conditions 112
Summary 114
Exercises 114
Lesson 6: Pulling It All Together: Income Tax Calculator 117
Getting Started 118
Step 1: Gather Requirements 118
Step 2: Design the Program 120
Step 3: Create the Inputs 120
Step 4: Calculate the Taxable Income 122
Step 5: Calculate the Tax Rate 124
Step 6: Update the Application 133
Step 7: Address the UI 136
On Your Own 139
Summary 139
Part II: Loops and Data Structures 141
Lesson 7: Controlling Program Flow with Loops 143
Iterations Overview 144
The Anatomy of a Loop 144
The for Loop 145
The while Loop 146
for vs. while Loops 149
Strings and String Operations 151
Iterating through Strings 164
Summary 167
Exercises 167
Lesson 8: Understanding Basic Data Structures: Lists 173
Data Structure Overview - Part 1 174
Creating Lists 175
Determining List Length 179
Working with List Indexes 179
Negative Indexing in Lists 182
Slicing Lists 184
Adding Items to a List 189
Inserting List Items 190
Removing List Items 192
Concatenating Lists 196
List Comprehension 197
Sorting Lists 199
Copying Lists 200
Summary 202
Exercises 202
Lesson 9: Understanding Basic Data Structures: Tuples 205
Tuples and Tuple Operations 206
Tuple Index Values 209
Negative Indexing in Tuples 210
Slicing Tuples 212
Immutability 213
Concatenating Tuples 216
Searching Tuples 217
Summary 218
Exercises 219
Lesson 10: Diving Deeper into Data Structures: Dictionaries 223
Data Structure Overview - Part 2 224
Getting Started with Dictionaries 224
Generating a Dictionary 227
Retrieving Items from a Dictionary 230
Using the keys() Method 233
Using the items() Method 234
Reviewing the keys(), values(), and items() Methods 236
Using the get() Method 239
Using the pop() Method 241
Working with the in Operator 245
Updating a Dictionary 246
Duplicating a Dictionary 249
Clearing a Dictionary 254
Summary 255
Exercises 255
Lesson 11: Diving Deeper into Data Structures: Sets 259
Sets 260
Retrieving Items from a Set 261
Adding Items to a Set 262
Creating an Empty Set 262
Understanding Set Uniqueness 263
Searching Items in a Set 265
Calculating the Length of a Set 267
Deleting Items from a Set 268
Clearing a Set 270
Popping Items in a Set 272
Deleting a Set 273
Determining the Difference Between Sets 274
Intersecting Sets 277
Combining Sets 278
Summary 279
Exercises 279
Lesson 12: Pulling It All Together: Prompting for an Address 283
Step 1: Getting Started 284
Step 2: Accept User Input 285
Step 3: Display the Input Value 286
Step 4: Modify the Output 287
Step 5: Split a Text Value 288
Step 6: Display Only the House Number 290
Step 7: Display the Street Name 291
Step 8: Add the Period 292
Summary 293
Lesson 13: Organizing with Functions 295
Functions Overview 295
Defining Functions in Python 296
Function Syntax 300
Default Input Values 301
Parameter Syntax 303
Arbitrary Arguments 304
Keyword Arguments 306
Arbitrary Keyword Arguments 306
Summary 308
Exercises 309
Part III: Object- Oriented Programming in Python 311
Lesson 14: Incorporating Object- Oriented Programming 313
Object- Oriented Programming Overview 314
Defining Classes 314
Creating Objects 316
Working with Methods 319
Class Attributes 324
Summary 330
Exercises 330
Lesson 15: Including
Inheritance 333
Understanding Inheritance 334
Creating a Parent Class 335
Creating a Child Class 335
Inheriting at Multiple Levels 338
Overriding Methods 340
Summary 343
Exercises 344
Lesson 16: Pulling It All Together: Building a Burger Shop 349
Requirements for Our Application 350
Plan the Code 350
Create the Classes 351
Create the Food Item Class 352
Create the Main File 357
Display the Output 364
Tie the Code Files Together 364
Summary 368
Part IV: Data Processing with Python 369
Lesson 17: Working with Dates and Times 371
Getting Started with Dates and Times 372
Getting the Current Date and Time 376
Splitting a Date String 377
Using datetime Attributes 379
Creating Custom datetime Objects 380
Compare datetime Values 381
Working with UTC Format 383
Applying Timestamps 384
Arithmetic and Dates 387
Calculating the Difference in Days 388
Using Date without Time 390
Using Time without Date 392
Summary 394
Exercises 394
Calculator 1: Time Duration 396
Calculator 2: Add or Subtract Time from a Date 397
Calculator 3: Age Calculator 397
Lesson 18: Processing Text Files 399
File Processing Overview 401
Introduction to File Input/Output 402
Processing Text Files 404
Opening a File 404
Reading Text from a File 406
Add Content to a File 412
Overwriting the Contents of a File 415
Creating a New File 417
Using the os Module 418
Deleting a File 419
Summary 421
Exercises 421
Lesson 19: Processing CSV Files 425
Reading CSV Files 426
Using the DictReader Class 430
Creating a Dataset List 432
Using writerow() 434
Appending Data 436
Writing Rows as Lists 439
Writing Rows from Dictionaries 440
Summary 444
Exercises 444
Lesson 20: Processing JSON Files 447
Processing JSON Files 448
Creating a JSON File with dump() 448
Converting to JSON with dumps() 449
Formatting JSON Data 450
Using json.loads() 452
Iterating through JSON Data 454
Reading and Writing JSON Data 457
Summary 460
Exercises 461
Part V: Data Analysis and Exception Handling 465
Lesson 21: Using Lambdas 467
Creating a Lambda Function 468
Working with Multiple Inputs 469
Placing Lambda Functions inside a Function 471
Using the map() Function 472
Combining Map and Lambda Functions 475
Using the filter() Function 477
Combining a Filter and a Lambda 479
Using the reduce() Function 480
Summary 486
Exercises 486
Lesson 22: Handling Exceptions 491
Built- In Exceptions 492
Working with try and except 493
Working with Multiple Excepts 495
Combining Exception Types 498
Using Multiple Operations in a try 500
Using the raise Keyword 501
Exploring the General Exception Classes 502
Adding finally 505
Summary 506
Exercises 506
Lesson 23: Pulling It All Together: Word Analysis in Python 511
Examine the Data 512
Read the Data 514
Tokenize the Dataset 517
Count the Words in Each Review 524
Summary 528
Lesson 24: Extracting, Transforming, and Loading with ETL Scripting 531
ETL Scripting in Python 532
Design and Implement Custom ETL Scripts 532
The extract Class 534
The transform Class 546
The load Class 569
Summary 582
Exercises 582
Lesson 25: Improving ETL Scripting 585
Converting to Static Methods for the extract Class 586
Converting to Static Methods for the transform Class 588
Summary 607
Exercises 608
Part VI: Appendices 611
Appendix A: Flowcharts 613
Flowchart Basics 613
Common Flowcharting Shapes 615
Appendix B: Creating Pseudocode 621
What Is Pseudocode? 621
Appendix C: Installing MySQL 623
MySQL Installation 623
Verify the Installation 628
The MySQL Notifier 630
Appendix D: Installing Vinyl DB 631
Database Structure 631
Create the Database 632
Appendix E: Installing MongoDB 637
Installing MongoDB Community Server 637
Running MongoDB 642
Appendix F: Importing to MongoDB 643
Index 645