About the Authors vAbout the Technical Writer vAbout the Technical Editor vAcknowledgments viIntroduction xviiPart I: Getting Started with Python 1Lesson 1: Setting Up a Python Programming Environment 3Python Overview 4Using Replit Online 4Creating a Replit Account 5Creating a Python Program in Replit 7Running a Python Program in Replit 9Other Replit Tasks 11Renaming Your Code File 11Saving Your Coding File Locally 12Creating a New File for Your Python Project 12Adding Files to Your Python Project 13Returning to Replit 13Getting More Help for Replit 14Getting Started with Jupyter Notebook 14Installing Anaconda Jupyter Notebook 15Creating a New Jupyter Notebook File 16Renaming a Jupyter Notebook Project File 18Saving a Python File Locally 19Opening an Existing Jupyter Notebook File 20A Quick Look at Visual Studio Code 21Obtaining Visual Studio Code 21Adding the Python Extension to Visual Studio Code 22Using Python from the Command Line 24Summary 26Exercises 26Exercise 1: Say Hello 27Exercise 2: What's It Do? 27Exercise 3: Counting 27Exercise 4: Fruity Code 28Lesson 2: Understanding Programming Basics 29The Future of Computer Programming 30What Is Programming? 30What Is a Program? 30Computational Thinking 31Programming Languages 32Common Components 33Statements 33Syntax 33Reserved Words 34Operators 35Hello, World! 36Data Types and Variables 37Data Types 37Text 38Numbers 38True/False 39Date/Time 39Data Collections 39Variables 40Reserving Memory 42Variables and Data Types 43Constants 44Example 1: Area of a Circle 44Example 2: Tax Rate 45Example 3: Output Messages 45Summary 46Exercises 46Exercise 1: Daily Tasks 47Exercise 2: Python Programming 47Lesson 3: Exploring Basic Python Syntax 49Using with Single-Line Commands 51Using Semicolons 52Continuing with Backslash 54Working with Case Structure 55Adding Comments 56Using the Input Function 57Storing Input 59Understanding Variable Types 61Displaying Variable Values 62Naming Variables 64Summary 65Exercises 65Exercise 1: Displaying Text 66Exercise 2: Follow the Comments 66Exercise 3: Fixing the Code 66Exercise 4: Broken Variables 67Exercise 5: Broken Names 67Exercise 6: Where Are You? 67Lesson 4: Working with Basic Python Data Types 69Review of Data Types 70Number Data Types 70Identifying Data Types 72Mathematical Operations 74PEMDAS 77Common Math Functions 81Math Library Functions 83Using Numbers with User Input 86Boolean Types and Boolean Operations 89Convert to Boolean 90Logic Operations 92Comparative Operators 95Summary 96Exercises 97Exercise 1: Prompting the User 97Exercise 2: Manipulated Math 97Exercise 3: Integers Only 98Exercise 4: Current Value 98Exercise 5: Simple Interest 98Exercise 6: True or False 99Exercise 7: Playing with Numbers 99Exercise 8: Do the Math 99Exercise 9: Street Addresses 100Lesson 5: Using Python Control Statements 101Control Structures Review 101Understanding Sequence Control Structure 102Understanding Selection Statements 103Understanding Conditional Statements 106If-Else Statements 108Working with Nested Conditions 109Embedding Conditions 112Summary 114Exercises 114Exercise 1: Are You Rich? 115Exercise 2: Cats or Dogs 115Exercise 3: True or False Quiz 115Exercise 4: For Every Season... 115Exercise 5: Company Picnic 116Lesson 6: Pulling It All Together: Income Tax Calculator 117Getting Started 118Step 1: Gather Requirements 118Values in Use 119User Interface 119Other Standards 120Step 2: Design the Program 120Step 3: Create the Inputs 120Step 4: Calculate the Taxable Income 122Step 5: Calculate the Tax Rate 124Add a Conditional Statement 125Create Nested Conditionals 127Step 6: Update the Application 133What About Negative Taxable Incomes? 134Does Code Compare to Standards? 136Step 7: Address the UI 136On Your Own 139Summary 139Part II: Loops and Data Structures 141Lesson 7: Controlling Program Flow with Loops 143Iterations Overview 144The Anatomy of a Loop 144The for Loop 145The while Loop 146Unexecutable while Loop 148for vs. while Loops 149Strings and String Operations 151Determining the Length of a String 152Splitting a String 153Storing Characters 154Comparison Operators in Strings 155Concatenating Strings 158Slicing Strings 159Searching Strings 163Iterating through Strings 164Summary 167Exercises 167Exercise 1: Separating Your Fruits 168Exercise 2: Keeping It Short 168Exercise 3: Fruit Finder 168Exercise 4: It's Divisible 169Exercise 5: Identify the Numbers 169Exercise 6: And the Total Is... 169Exercise 7: Multiplication Tables 169Exercise 8: Sum of Prime Numbers 169Exercise 9: One Letter at a Time 170Exercise 10: Length without len() 170Exercise 11: Count the Numbers 170Exercise 12: Fizz Buzz 170Lesson 8: Understanding Basic Data Structures: Lists 173Data Structure Overview--Part 1 174Creating Lists 175Determining List Length 179Working with List Indexes 179Negative Indexing in Lists 182Slicing Lists 184Using the Slice Object 187Adding Items to a List 189Inserting List Items 190Removing List Items 192Deleting versus Removing Items 194Popping Instead of Removing 195Concatenating Lists 196List Comprehension 197Sorting Lists 199Copying Lists 200Summary 202Exercises 202Exercise 1: All About You 203Exercise 2: Shopping List 203Exercise 3: List Deletion 203Exercise 4: List Modification 203Exercise 5: A CompleteList Program 203Lesson 9: Understanding Basic Data Structures: Tuples 205Tuples and Tuple Operations 206Syntax of Tuples vs. Syntax of Lists 206Tuple Length 208Tuple Index Values 209Negative Indexing in Tuples 210Slicing Tuples 212Immutability 213Concatenating Tuples 216Searching Tuples 217Summary 218Exercises 219Exercise 1: Creating Tuples 219Exercise 2: Modifying Tuples 219Exercise 3: Where's Waldo? 220Exercise 4: A Complete Tuple Program 220Lesson 10: Diving Deeper into Data Structures: Dictionaries 223Data Structure Overview--Part 2 224Getting Started with Dictionaries 224Generating a Dictionary 227Retrieving Items from a Dictionary 230Using the keys() Method 233Using the items() Method 234Reviewing the keys(), values(), and items() Methods 236Using the get() Method 239Using the pop() Method 241Working with the in Operator 245Updating a Dictionary 246Duplicating a Dictionary 249Clearing a Dictionary 254Summary 255Exercises 255Exercise 1: Working with Text 256Exercise 2: Separating the High from the Low 256Exercise 3: High and Low All in One 257Exercise 4: Self-Assessment 257Lesson 11: Diving Deeper into Data Structures: Sets 259Sets 260Retrieving Items from a Set 261Adding Items to a Set 262Creating an Empty Set 262Understanding Set Uniqueness 263Searching Items in a Set 265Calculating the Length of a Set 267Deleting Items from a Set 268Clearing a Set 270Popping Items in a Set 272Deleting a Set 273Determining the DifferenceBetween Sets 274Intersecting Sets 277Combining Sets 278Summary 279Exercises 279Exercise 1: Line by Line 280Exercise 2: Adding New Names 280Exercise 3: Popping Accounts 280Exercise 4: Everywhere ThatMary Went... 280Exercise 5: Self-Assessment 281Lesson 12: Pulling It All Together: Prompting for an Address 283Step 1: Getting Started 284Step 2: Accept User Input 285Step 3: Display the Input Value 286Step 4: Modify the Output 287Step 5: Split a Text Value 288Step 6: Display Only the House Number 290Step 7: Display the Street Name 291Step 8: Add the Period 292Summary 293Lesson 13: Organizing with Functions 295Functions Overview 295Defining Functions in Python 296Function Syntax 300Default Input Values 301Parameter Syntax 303Arbitrary Arguments 304Keyword Arguments 306Arbitrary Keyword Arguments 306Summary 308Exercises 309Exercise 1: Lower Numbers 309Exercise 2: This Will Be 309Exercise 3: Finding the Largest 310Exercise 4: Simple Calculator 310Exercise 5: Which Is Greater? 310Part III: Object-Oriented Programming in Python 311Lesson 14: Incorporating Object-Oriented Programming 313Object-Oriented Programming Overview 314Defining Classes 314Attributes 315Methods 316Creating Objects 316Working with Methods 319Class Attributes 324Working with Static Methods 326Working with Class Methods 328Summary 330Exercises 330Exercise 1: Create Your Own Class 331Exercise 2: Classy Vehicles 331Exercise 3: Streamlined Banking 331Exercise 4: Using a Calculator inClass 331Lesson 15: Including Inheritance 333Understanding Inheritance 334Creating a Parent Class 335Creating a Child Class 335Inheriting at Multiple Levels 338Overriding Methods 340Summary 343Exercises 344Exercise 1: Basic Inheritance 344Exercise 2: Adding Attributes 344Exercise 3: Creating More Children 345Exercise 4: Dogs and Cats 345Exercise 5: Hourly Employees 345Exercise 6: File System 347Lesson 16: Pulling It All Together: Building a Burger Shop 349Requirements for Our Application 350Plan the Code 350Create the Classes 351Create the Food Item Class 352Create a Burger Class 353Create a Drink Class 354Create a Side Class 354Create a Combo 355Create the Order Class 356Create the Main File 357Create order once 358Order a Burger 359Add a Drink 360Add Sides 362Order a Combo 363Display the Output 364Tie the Code Files Together 364Summary 368Part IV: Data Processing with Python 369Lesson 17: Working with Dates and Times 371Getting Started with Dates and Times 372Creating a Variable for a Date 372Creating a Variable for Time 375Creating a Variable for Both Date and Time 375Getting the Current Date and Time 376Splitting a Date String 377Using datetime Attributes 379Creating Custom datetime Objects 380Compare datetime Values 381Working with UTC Format 383Applying Timestamps 384Arithmetic and Dates 387Calculating the Difference in Days 388Using Date without Time 390Using Time without Date 392Summary 394Exercises 394Exercise 1: Displaying Dates 394Exercise 2: Leap Years 395Exercise 3: The Past 395Exercise 4: Unix Dates 395Exercise 5: Yesterday, Today, and Tomorrow 395Exercise 6: Setting Future Days 395Exercise 7: Five Seconds in the Future 395Exercise 8: Date Calculators 396Calculator 1: Time Duration 396Calculator 2: Add or SubtractTime from a Date 397Calculator 3: Age Calculator 397Lesson 18: Processing Text Files 399File Processing Overview 401Introduction to File Input/Output 402The input() Function 402The open() Function 402The read() Method 403The write() Method 403The close() Method 403The print() Function 403Processing Text Files 404Opening a File 404Reading Text from a File 406Use the read() Method to Limit the Content 406Reading Lines 408Iterating through a File 410Add Content to a File 412Overwriting the Contents of a File 415Creating a New File 417Using the os Module 418Deleting a File 419Summary 421Exercises 421Exercise 1: Reading Lines 422Exercise 2: Combination of the Two 422Exercise 3: Combination of Them All 422Exercise 4: Listing Lines 422Exercise 5: Longest Word 422Exercise 6: Listing Text 423Exercise 7: Text in Reverse 423Lesson 19: Processing CSV Files 425Reading CSV Files 426Using the DictReader Class 430Creating a Dataset List 432Using writerow() 434Appending Data 436Writing Rows as Lists 439Writing Rows from Dictionaries 440Summary 444Exercises 444Exercise 1: Reading Lines 444Exercise 2: Company Stocks 444Exercise 3: Rearranging Files 445Exercise 4: Pop Music Evolution 445Exercise 5: All About Cars 446Lesson 20: Processing JSON Files 447Processing JSON Files 448Creating a JSON File with dump() 448Converting to JSON with dumps() 449Formatting JSON Data 450Using json.loads() 452Iterating through JSON Data 454Reading and Writing JSON Data 457Summary 460Exercises 461Exercise 1: Company Bank Account 461Exercise 2: Formatted Account Information 461Exercise 3: Nobel Prizes 461Exercise 4: New York Restaurants 462Exercise 5: Movies 463Part V: Data Analysis and Exception Handling 465Lesson 21: Using Lambdas 467Creating a Lambda Function 468Working with Multiple Inputs 469Placing Lambda Functions inside a Function 471Using the map() Function 472Combining Map and Lambda Functions 475Using the filter() Function 477Combining a Filter and a Lambda 479Using the reduce() Function 480Specify an Initial Value 482Using reduce() with Comparison Operations 484Summary 486Exercises 486Exercise 1: Computing the Square Root 487Exercise 2: Converting a Text File to Uppercase 487Exercise 3: Determining Prime 487Exercise 4: Identifying Absolute Value 487Exercise 5: Highest Number 487Exercise 6: Lowest Number 487Exercise 7: Last Key 487Exercise 8: Highest Value 488Exercise 9: Sum of Even 488Exercise 10: Sum of Positive Numbers 488Exercise 11: Highest Stock Market Volume 488Exercise 12: Bad Stock Market Day 488Exercise 13: Highest Opening Price 488Exercise 14: Highest Price at Closing 489Exercise 15: Self-Assessment 489Lesson 22: Handling Exceptions 491Built-In Exceptions 492Working with try and except 493Working with Multiple Excepts 495Combining Exception Types 498Using Multiple Operations in a try 500Using the raise Keyword 501Exploring the General Exception Classes 502Adding finally 505Summary 506Exercises 506Exercise 1: Typing Numbers 507Exercise 2: Current Value 507Exercise 3: Reading Lines 508Exercise 4: Concatenating Files 508Exercise 5: Creating a List from a File 508Exercise 6: Self-Assessment 508Lesson 23: Pulling It All Together: Word Analysis in Python 511Examine the Data 512Read the Data 514Tokenize the Dataset 517Tokenize an Input String 518Tokenize an Input Review 521Tokenize the Entire Dataset 522Using the Tokenize Functions 523Count the Words in Each Review 524Word Count for an Input List of Words 524Word Count an Input Review 525Word Count for the Dataset 526Summary 528Lesson 24: Extracting, Transforming, and Loading with ETL Scripting 531ETL Scripting in Python 532Design and Implement Custom ETL Scripts 532The extract Class 534Adding the extract.fromCSV Method 534Creating the extract.fromJSON Method 536Creating the extract.fromMySQL Method 538Creating the extract.fromMongoDB Method 542Verify the extract.py Module 544Using Our Script as an External Module 545The transform Class 546Defining the transform Class 546Creating the head and tail Methods 547Renaming a Column 551Removing a Column from the Data Source 552Renaming Multiple Columns 556Removing Multiple Columns 558Transforming the Data 563The load Class 569Creating the load.toCSV Method 570Creating the load.toJSON Method 572Creating the load.toMYSQLMethod 574Creating the Load.toMONGODB Method 578Summary 582Exercises 582Exercise 1: Transforming CSV to CSV 583Exercise 2: Transforming CSV to JSON 583Exercise 3: Transforming JSON to CSV 583Exercise 4: Transforming JSON to JSON 583Exercise 5: Removing an Attribute 583Exercise 6: Renaming an Attribute 583Exercise 7: Confirming an Attribute 584Lesson 25: Improving ETL Scripting 585Converting to Static Methods for the extract Class 586Converting to Static Methods for the transform Class 588Converting to Static Methods for the load Class 592Adding Exception Handling in the extract Class 594Creating a Custom Extractor for the extract Class 601Summary 607Exercises 608Exercise 1: Revisiting Lessons Learned 608Exercise 2: Day of Week 608Exercise 3: Date Validity 609Exercise 4: Listing Duplicates 609Exercise 5: Removing Duplicates 609Exercise 6: Transforming Names 609Part VI: Appendices 611Appendix A: Flowcharts 613Flowchart Basics 613Sequences 613Branches 614Loops 615Common Flowcharting Shapes 615Flowcharting Example 616Additional Flowchart Elements 618Appendix B: Creating Pseudocode 621What Is Pseudocode? 621Appendix C: Installing MySQL 623MySQL Installation 623Download and Install MySQL 623Configure MySQL 624Configure MySQL Router Options 626Configure Samples and Examples 627Verify the Installation 628The MySQL Notifier 630Appendix D: Installing Vinyl DB 631Database Structure 631Create the Database 632Appendix E: Installing MongoDB 637Installing MongoDB Community Server 637Running MongoDB 642Appendix F: Importing to MongoDB 643Index 645
HAYTHEM BALTI, PhD, is the associate dean at Wiley's mthree academy. He has created courses used by thousands of Software Guild and mthree alumni to learn Go, Java, Python, and other development and data science skills.KIMBERLY A. WEISS is a veteran course developer, specializing in Computer Science courses since 2002. She was an assistant professor in Computer Science for over ten years before deciding to focus exclusively on course design. She has worked with multiple universities as well as corporate training settings to develop interactive instructional content appropriate for the target learners and course goals.
1997-2025 DolnySlask.com Agencja Internetowa