Transcription of Python Programming for the Absolute Beginner Second …
1 MICHAEL DAWSONTHCDIVISCHNICOURSE TECHNOLOGYP rofessional n Technical n ReferencePythonProgramming forthe AbsoluteBeginnerSecond EditionCONTENTSCHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on Windows 5 Installing Python on Windows 5 Setting Up Python on Other Operating Systems 6 Introducing the Python IDLE 7 Programming in Interactive Mode 7 Programming in Script Mode 10 Back to the Game Over Program 12 Using
2 Comments 13 Using Blank Lines 14 Printing the String 14 Waiting for the User 14 Summary 15 CHAPTER 2 TYPES, VARIABLES, AND SIMPLE 1/0: THE USELESSTRIVIA PROGRAM 17 Introducing the Useless Trivia Program 18 Using Quotes with Strings 18 Introducing the Game Over Program 18 Using Quotes inside Strings 20 Continuing a Statement on the Next Line 20 Creating Triple-Quoted Strings 21 Using Escape Sequences with Strings 22 Python Programming for the Absolute BeginnerIntroducing the Fancy Credits Program 22 Sounding the System Bell 23 Moving Forward a Tab Stop 23 Printing a Backslash 23 Inserting a Newline 24 Inserting a Quote 24 Concatenating and
3 Repeating Strings 25 Introducing the Silly Strings Program 25 Concatenating Strings 26 Suppressing a Newline 27 Repeating Strings 27 Working with Numbers 28 Introducing the Word Problems Program 28 Understanding Numeric Types 30 Using Mathematical Operators 30 Understanding Variables 32 Introducing the Greeter Program 32 Creating Variables 33 Using Variables 34 Naming Variables 34 Getting User Input 35 Introducing the Personal Greeter Program 35 Using the raw_i nput( ) Function 36 Using String Methods 37 Introducing the Quotation Manipulation Program 37 Creating New Strings with String Methods 38 Using the Right Types 40 Introducing the Trust Fund Buddy-Bad Program 41 Tracking Down Logical Brrors 42 Converting Values 43 Introducing the Trust Fund Buddy-Good Program 43 Converting Strings to Integers 44 Using Augmented Assignment Operators 45 Printing Strings and Numbers Together 46 Back to the Useless Trivia Program 47 Creating the Initial Comments 47 Getting the User Input 47
4 Printing Lowercase and Uppercase Versions of name 48 Calculating dog_yea rs 48 Calculating seconds 48 Printing name Five Times 49 Calculating moon_wel ght and sun_wei ght 49 Waiting for the User 49 Summary 49Co ntentsCHAPTER 3 BRANCHING, whi 1 e LOOPS, AND PROGRAMPLANNING: THE GUESS MY NUMBER GAME 51 Introducing the Guess My Number Game 52 Generating Random Numbers 52 Introducing the Craps Roller Program 52 Using the import Statement 53 Accessing randrange( ) 54 Using randrange( ) 54 Using the i f Structure 55 Introducing the Password Program 55 Examining the i f Structure 56 Creating Conditions 57 Understanding Compaison Operators 57 Using Indentation to Create Blocks 58 Buildin' g Your Own i f Structure 59 Using the i f- el se Structure 59 Introducing the Granted or Denied Program 59 Examining the el se Statement 60 Using the i f - eli f - el se Structure 61 Introducing the Mood Computer Program 61 Examining the i f - el i f - el se Structure 63 Creating whi 1 e Loops 65 Introducing the
5 Three-Year-Old Simulator Program 65 Examining the whi 1 e Structure 66 Initializing the Sentry Variable 67 Checking the Sentry Variable 67 Updating the Sentry Variable 68 Avoiding Infinite Loops 68 Introducing the Losing Battle Program 68 Tracing the Program 70 Creating Conditions That Can Become False 71 Treating Values as Conditions 72 Introducing the Maitre D' Program 72 Interpreting Any Value as True or Fal se 73 Creating Intentional Infinite Loops 74 Introducing the Finicky Counter Program 74 Using the break Statement to Exit a Loop 75 Using the conti nue Statement to Jump Back to the Top of a Loop 75 Understanding When to Use break and conti nue 76 Using Compound Conditions 76 Introducing the Exdusive Network Program 76 Understanding the not Logical Operator 79 Understanding the and Logical Operator 80 Understanding the or Logical Operator 81 Python Programming for the Absotute BeginnerCHAPTER 4 Planning Your Programs 82 Creating Algorithms with Pseudocode 82 Applying Stepwise Reanerneut to Your Algorithms 83
6 Returning to the Guess My Nuraber Game 83planning the Program 84 Creating the Initial Comment Block 85 Importing the r andom Module 85 Explaining the Garne 85 Setting the Initial Values 85 Creating a Guessing Loop 86 Congratulating the Player 86 Waiting for the Player to Quit 86for LOOPS, STR1 NGS, AND TUPLES: THE WORDJUMBLE GAME 89 Introducing the Word Jumble Game 90 Using for Loops 90 Introducing the Loopy String Program 90 Understanding for Loops 91 Creating a for Loop 92 Counting with a for Loop 92 Introducing the Counter Program 92 Counting Forwards 94 Counting by Fives 94 Counting Backwards 95 Using Sequence Operators and Functions with.
7 Strings 95 Introducing the Message Analyzer Program 95 Using the 1 en ( ) Function 96 Using the i n Operator 97 Indexing Strings 97 Introducing the Random Access Program 98 Working with Positive Position Numbers 99 Working with Negative Position Numbers 100 Accessing a Random String Element 101 Understanding String Immutability 101 Building a New String 103 Introducing the No Vowels Program 103 Creating Constants 104 Creating New Strings from Existing Ones 105 Slicing Strings 106 Introducing the Pizza Slicer Program 106 Introducing None 108 ContentsUnderstanding Slicing 108 Creating
8 Slices 109 Using Slicing Shorthand 110 Creating Tuples 110 Introducing the Hero's Inventory Program 111 Creating an Empty Tuple 112 Treating a Tuple as a Condition 112 Creating a Tuple with Elements 113 Printing a Tuple 113 Looping Through a Tuple's Elements 113 Using Tuples 114 Introducing the Hero's Inventory 114 Setting Up the Program 115 Using the 1 en ( ) Function with Tuples 115 Using the i n Operator with Tuples 116 Indexing Tuples 116 Slicing Tuples 116 Understanding Tuple Immutability 117 Concatenating Tuples 117 Back to the Word Jumble Game 118 Setting Up the Program 118 Planning the Jumble Creation Section 119 Creating an Empty Jumble String 119 Setting Up the Loop 119 Generating a Random Position in wo r d 120 Creating a New Version of j umbl e 120 Creating a New Version of wo rd 120 Welcorning the Player 120 Getting the Player's Guess 121 Congratulating
9 The Player 121 Ending the Garne 121 Summary 121 CHAPTER 5 LISTS AND DICTIONARIES: THE HANGMAN GAME 123 Introducing the Hangman Garne 124 Using Lists 125 Introduring the Hero's Inventory Program 125 Creating a List 126 Using the 1 en ( ) Function with Lists 127 Using the i n Operator with Lists 127 Indexing Lists 127 Slicing Lists 127 Concatenating Lists 128 Python Programming for the Absolute BeginnerUnderstanding List Mutability 128 Assigning a New List Element by Index 128 Assigning a New List Slice 129 Deleting a List Element 129 Deleting a List Slice 130 Using List Methods 130 Introducing the High Scores Program 130 Setting Up the Program 131 Displaying the Menu 131 Exiting the Program 132 Displaying the Scores 132 Adding a Score 132
10 Removing a Score 133 Sorting the Scores 133 Reversing the Scores 134 Dealing with an Invalid Choice 134 Waiting for the User 134 Understanding When to Use Tuples Instead of Lists 135 Using Nested Sequences 135 Introducing the High Scores Program 135 Creating Nested Sequences 136 Accessing Nested Elements 137 Unpacking a Sequence 138 Setting Up the Program 138 Displaying the Scores by Accessing Nested Tuples 139 Adding a Score by Appending a Nested Tuple 139 Dealing with an Invalid Choice 139 Waiting for the User 140 Understanding Shared Refer