Python Projects for Beginners Connor

Chapter 1: Getting Started␈ 1

Monday: Introduction␈ 2 What Is Python?␈ 2 Why Python?␈ 3 Why This Book?␈ 4 Who This Book Is For?␈ 4 What You’ll Learn␈ 5 Tuesday: Setting Up Anaconda and Python␈ 6 Cross-Platform Development␈ 6 Installing Anaconda and Python for Windows␈ 6 What Is Anaconda?␈ 8 What Is Jupyter Notebook?␈ 8 Wednesday: How to Use the Terminal␈ 9 Changing Directories␈ 9 Checking the Directory␈ 10 Making Directories␈ 10 Creating Files␈ 10 Checking a Version Number␈ 11 Clearing the Terminal Output␈ 11 Using the Python Shell␈ 12 Writing Your First Line of Python␈ 12 Exiting the Python Shell␈ 13 vTable of Contents Thursday: Using Jupyter Notebook␈ 13 Opening Jupyter Notebook␈ 14 Creating a Python File␈ 14 Jupyter Notebook Cells␈ 15 Friday: Creating Your First Program␈ 17 Line Numbers Introduced␈ 17 Creating the Program␈ 18 Final Output␈ 19 Weekly Summary␈ 20 Weekly Challenges␈ 20

Chapter 2: Python Basics␈ 21

Monday: Comments and Basic Data Types␈ 22 What Are Comments and Why Use Them?␈ 22 Writing Comments␈ 23 What Are Data Types?␈ 24 The Print Statement␈ 24 Integers␈ 25 Floats␈ 25 Booleans␈ 25 Strings␈ 26 Tuesday: Variables␈ 27 How They Work␈ 27 Handling Naming Errors␈ 28 Integer and Float Variables␈ 28 Boolean Variables␈ 29 String Variables␈ 29 Using Multiple Variables␈ 29 Using Operators on Numerical Variables␈ 30 Overwriting Previously Created Variables␈ 30 Whitespace␈ 31 viTable of Contents Wednesday: Working with Strings␈ 31 String Concatenation␈ 32 Formatting Strings␈ 32 String Index␈ 34 String Slicing␈ 36 Thursday: String Manipulation␈ 37 .␇title( )␈ 37 .␇replace( )␈ 37 .␇find( )␈ 38 .␇strip( )␈ 38 .␇split( )␈ 39 Friday: Creating a Receipt Printing Program␈ 39 Final Design␈ 40 Initial Process␈ 40 Defining Our Variables␈ 41 Creating the Top Border␈ 42 Displaying the Company Info␈ 42 Displaying the Product Info␈ 43 Displaying the Total␈ 44 Displaying the Ending Message␈ 44 Displaying the Bottom Border␈ 45 Weekly Summary␈ 45 Challenge Question Solution␈ 45 Weekly Challenges␈ 46

Chapter 3: User Input and Conditionals␈ 47

Monday: User Input and Type Converting␈ 48 Accepting User Input␈ 48 Storing User Input␈ 48 What Is Type Converting?␈ 49 Checking the Type␈ 49 viiTable of Contents Converting Data Types␈ 49 Converting User Input␈ 50 Handling Errors␈ 51 Code Blocks and Indentation␈ 52 Tuesday: If Statements␈ 52 How They Work␈ 53 Writing Your First If Statement␈ 53 Comparison Operators␈ 54 Checking User Input␈ 54 Logical Operators␈ 55 Membership Operators␈ 56 Wednesday: Elif Statements␈ 58 How They Work␈ 58 Writing Your First Elif Statement␈ 59 Checking Multiple Elif Conditions␈ 59 Conditionals Within Conditionals␈ 60 If Statements vs. Elif Statements␈ 60 Thursday: Else Statements␈ 62 How They Work␈ 62 Writing Your First Else Statement␈ 62 Complete Conditional Statement␈ 63 Friday: Creating a Calculator␈ 64 Final Design␈ 65 Step #1: Ask User for Calculation to Be Performed␈ 65 Step #2: Ask for Numbers, Alert Order Matters␈ 66 Step #3: Set Up Try/Except for Mathematical Operation␈ 66 Final Output␈ 67 Weekly Summary␈ 69 Challenge Question Solution␈ 69 Weekly Challenges␈ 69 viiiTable of Contents

Chapter 4: Lists and Loops␈ 71

Monday: Lists␈ 72 What Are Lists?␈ 72 Declaring a List of Numbers␈ 72 Accessing Elements Within a List␈ 73 Declaring a List of Mixed Data Types␈ 73 Lists Within Lists␈ 74 Accessing Lists Within Lists␈ 74 Changing Values in a List␈ 75 Variable Storage␈ 76 Copying a List␈ 77 Tuesday: For Loops␈ 78 How Loops Work␈ 78 Writing a For Loop␈ 78 Range()␈ 80 Looping by Element␈ 80 Continue Statement␈ 81 Break Statement␈ 82 Pass Statement␈ 82 Wednesday: While Loops␈ 83 Writing a While Loop␈ 84 While vs. For␈ 84 Infinite Loops␈ 84 Nested Loops␈ 85 Thursday: Working with Lists␈ 86 Checking Length␈ 87 Slicing Lists␈ 87 Adding Items␈ 88 Removing Items␈ 88 Working with Numerical List Data␈ 90 Sorting a List␈ 90 ixTable of Contents Conditionals and Lists␈ 91 Loops and Lists␈ 92 Friday: Creating Hangman␈ 93 Final Design␈ 94 Previous Line Symbols Introduced␈ 94 Adding Imports␈ 95 Declaring Game Variables␈ 96 Generating the Hidden Word␈ 96 Creating the Game Loop␈ 97 Outputting Game Information␈ 97 Checking a Guess␈ 98 Clearing Output␈ 98 Creating the Losing Condition␈ 99 Handling Correct Guesses␈ 99 Creating a Winning Condition␈ 100 Outputting Guessed Letters␈ 101 Adding Guessed Letters␈ 101 Handling Previous Guesses␈ 102 Final Output␈ 102 Weekly Summary␈ 103 Challenge Question Solution␈ 103 Weekly Challenges␈ 104

Chapter 5: Functions␈ 105

Monday: Creating and Calling Functions␈ 106 What Are Functions?␈ 106 Function Syntax␈ 107 Writing Your First Function␈ 107 Function Stages␈ 108 UDF vs. Built-in␈ 109 Performing a Calculation␈ 109 xTable of Contents Tuesday: Parameters␈ 110 What Are Parameters?␈ 110 Passing a Single Parameter␈ 111 Multiple Parameters␈ 111 Passing a List␈ 112 Default Parameters␈ 113 Making Parameters Optional␈ 113 Named Parameter Assignment␈ 114 *args␈ 114 **kwargs␈ 115 Wednesday: Return Statement␈ 116 How It Works␈ 116 Using Return␈ 117 Ternary Operator␈ 118 Thursday: Scope␈ 119 Types of Scope␈ 119 Global Scope Access␈ 119 Handling Function Scope␈ 120 In-Place Algorithms␈ 120 Friday: Creating a Shopping Cart␈ 121 Final Design␈ 122 Initial Setup␈ 122 Adding Items␈ 123 Removing Items␈ 123 Showing the Cart␈ 124 Clearing the Cart␈ 124 Creating the Main Loop␈ 124 Handling User Input␈ 125 Final Output␈ 126 xiTable of Contents Weekly Summary␈ 126 Challenge Question Solution␈ 127 Weekly Challenges␈ 127

Chapter 6: Data Collections and Files␈ 129

Monday: Dictionaries␈ 129 What Are Dictionaries?␈ 130 Declaring a Dictionary␈ 130 Accessing Dictionary Information␈ 131 Using the Get Method␈ 131 Dictionaries with Lists␈ 132 Lists with Dictionaries␈ 132 Dictionaries with Dictionaries␈ 133 Tuesday: Working with Dictionaries␈ 134 Adding New Information␈ 134 Changing Information␈ 135 Deleting Information␈ 135 Looping a Dictionary␈ 135 Wednesday: Tuples, Sets, Frozensets␈ 137 What Are Tuples?␈ 137 Declaring a Tuple␈ 138 What Are Sets?␈ 138 Declaring a Set␈ 138 What Are Frozensets?␈ 139 Declaring a Frozenset␈ 139 Data Collection Differences␈ 140 Thursday: Reading and Writing Files␈ 140 Working with Text Files␈ 141 Writing to CSV Files␈ 142 Reading from CSV Files␈ 142 File Modes in Python␈ 143 xiiTable of Contents Friday: Creating a User Database with CSV Files␈ 144 Final Design␈ 144 Setting Up Necessary Imports␈ 145 Handling User Registration␈ 145 Handling User Login␈ 146 Creating the Main Loop␈ 147 Weekly Summary␈ 148 Challenge Question Solution␈ 149 Weekly Challenges␈ 149

Chapter 7: Object-Oriented Programming␈ 151

Monday: Creating and Instantiating a Class␈ 152 What Is an Object?␈ 152 OOP Stages␈ 153 Creating a Class␈ 153 Creating an Instance␈ 154 Creating Multiple Instances␈ 154 Tuesday: Attributes␈ 156 Declaring and Accessing Attributes␈ 156 Changing an Instance Attributes␈ 157 Using the __init__( ) Method␈ 157 The “self” Keyword␈ 158 Instantiating Multiple Objects with __init__( )␈ 159 Global Attributes vs. Instance Attributes␈ 159 Wednesday: Methods␈ 161 Defining and Calling a Method␈ 161 Accessing Class Attributes in Methods␈ 162 Method Scope␈ 162 Passing Arguments into Methods␈ 163 Using Setters and Getters␈ 164 Incrementing Attributes with Methods␈ 165 xiiiTable of Contents Methods Calling Methods␈ 166 Magic Methods␈ 166 Thursday: Inheritance␈ 168 What Is Inheritance?␈ 168 Inheriting a Class␈ 168 Using the super( ) Method␈ 169 Method Overriding␈ 170 Inheriting Multiple Classes␈ 171 Friday: Creating Blackjack␈ 172 Final Design␈ 173 Setting Up Imports␈ 174 Creating the Game Class␈ 174 Generating the Deck␈ 175 Pulling a Card from the Deck␈ 175 Creating a Player Class␈ 176 Adding Cards to the Player’s Hand␈ 177 Showing a Player’s Hand␈ 178 Calculating the Hand Total␈ 179 Handling the Player’s Turn␈ 181 Handling the Dealer’s Turn␈ 182 Calculating a Winner␈ 183 Final Output␈ 184 Weekly Summary␈ 184 Challenge Question Solution␈ 185 Weekly Challenges␈ 185

Chapter 8: Advanced Topics I: Efficiency␈ 187

Monday: List Comprehension␈ 188 List Comprehension Syntax␈ 188 Generating a List of Numbers␈ 189 If Statements␈ 190 If-Else Statements␈ 190 xivTable of Contents List Comprehension with Variables␈ 191 Dictionary Comprehension␈ 192 Tuesday: Lambda Functions␈ 193 Lambda Function Syntax␈ 193 Using a Lambda␈ 193 Passing Multiple Arguments␈ 194 Saving Lambda Functions␈ 195 Conditional Statements␈ 195 Returning a Lambda␈ 196 Wednesday: Map, Filter, and Reduce␈ 197 Map Without Lambdas␈ 197 Map with Lambdas␈ 198 Filter Without Lambdas␈ 199 Filter with Lambdas␈ 200 The Problem with Reduce␈ 201 Using Reduce␈ 201 Thursday: Recursive Functions and Memoization␈ 203 Understanding Recursive Functions␈ 203 Writing a Factorial Function␈ 204 The Fibonacci Sequence␈ 205 Understanding Memoization␈ 206 Using Memoization␈ 207 Using @lru_cache␈ 208 Friday: Writing a Binary Search␈ 209 Final Design␈ 209 Program Setup␈ 211 Step 1: Sort the List␈ 211 Step 2: Find the Middle Index␈ 212 Step 3: Check the Value at the Middle Index␈ 213 Step 4: Check if Value Is Greater␈ 213 Step 5: Check if Value Is Less␈ 214 xvTable of Contents Step 6: Set Up a Loop to Repeat Steps␈ 214 Step 7: Return False Otherwise␈ 215 Final Output␈ 216 Weekly Summary␈ 217 Challenge Question Solution␈ 217 Weekly Challenges␈ 218

Chapter 9: Advanced Topics II: Complexity␈ 219

Monday: Generators and Iterators␈ 220 Iterators vs. Iterables␈ 220 Creating a Basic Iterator␈ 220 Creating Our Own Iterator␈ 221 What Are Generators?␈ 222 Creating a Range Generator␈ 222 Tuesday: Decorators␈ 224 What Are Decorators?␈ 224 Higher-Order Functions␈ 225 Creating and Applying a Decorator␈ 225 Decorators with Parameters␈ 226 Functions with Decorators and Parameters␈ 226 Restricting Function Access␈ 227 Wednesday: Modules␈ 229 Importing a Module␈ 229 Importing Only Variables and Functions␈ 230 Using an Alias␈ 231 Creating Our Own Module␈ 231 Using Our Module in Jupyter Notebook␈ 232 Thursday: Understanding Algorithmic Complexity␈ 234 What Is Big O Notation?␈ 234 Hash Tables␈ 236 Dictionaries vs. Lists␈ 238 Battle of the Algorithms␈ 239 xviTable of Contents Friday: Interview Prep␈ 241 Developer Interview Process␈ 241 What to Do Before the Interview␈ 243 General Questions␈ 245 Whiteboarding and Technical Questions␈ 248 End of Interview Questions␈ 249 What to Do After the Interview␈ 250 Weekly Summary␈ 251 Challenge Question Solution␈ 252 Weekly Challenges␈ 252

Chapter 10: Introduction to Data Analysis␈ 253

Monday: Virtual Environments and Requests Module␈ 254 What Are Virtual Environments?␈ 254 What Is Pip?␈ 256 Creating a Virtual Environment␈ 256 Activating the Virtual Environment␈ 257 Installing Packages␈ 258 APIs and the Requests Module␈ 259 Using the Requests Module␈ 259 Tuesday: Pandas␈ 263 What Is Pandas?␈ 263 Key Terms␈ 264 Installing Pandas␈ 265 Importing Pandas␈ 265 Creating a DataFrame␈ 265 Accessing Data␈ 267 Built-in Methods␈ 268 Filtration␈ 271 Column Transformations␈ 272 Aggregations␈ 274 xviiTable of Contents Pandas Joins␈ 277 Dataset Pipeline␈ 280 Wednesday: Data Visualization␈ 281 Types of Charts␈ 282 Installing Matplotlib␈ 282 Importing Matplotlib␈ 283 Line Plot␈ 283 Bar Plot␈ 285 Box Plot␈ 286 Scatter Plot␈ 288 Histogram␈ 289 Saving the Chart␈ 292 Flattening Multidimensional Data␈ 293 Thursday: Web Scraping␈ 295 Installing Beautiful Soup␈ 295 Importing Beautiful Soup␈ 295 Requesting Page Content␈ 296 Parsing the Response with Beautiful Soup␈ 297 Scraping Data␈ 297 DOM Traversal␈ 299 Friday: Web Site Analysis␈ 304 Final Design␈ 304 Importing Libraries␈ 306 Creating the Main Loop␈ 307 Scraping the Web Site␈ 307 Scrape All Text␈ 308 Filtering Elements␈ 309 Filtering Waste␈ 310 Count Word Frequency␈ 312 Sort Dictionary by Word Frequency␈ 313 Displaying the Top Word␈ 313 xviiiTable of Contents Graphing the Results␈ 314 Final Output␈ 315 Weekly Summary␈ 315 Challenge Question Solution␈ 316 Weekly Challenges␈ 316 Afterword: Post-Course: What to Do Now?␈ 319 Back-End Development with Python␈ 319 Full-Stack Development with Python␈ 320 Data Analysis with Python␈ 320 Data Science with Python␈ 320 Resources␈ 320 Final Message␈ 323 Index␈ 325