ebook img

Begin to code with Python PDF

656 Pages·2018·6.073 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Begin to code with Python

Begin to Code with Python Rob Miles BEGIN TO CODE WITH PYTHON Published with the authorization of Microsoft Corporation by: Pearson Education, Inc. Copyright © 2018 by Pearson Education, Inc. ISBN-13: 978-1-5093-0452-3 ISBN-10: 1-5093-0452-5 Library of Congress Control Number: 2017958202 1 17 Contents at a glance Part 1: Programming fundamentals Chapter 1 Starting with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Chapter 2 Python and Programming .......................... 16 Chapter 3 Python program structure .......................... 44 Chapter 4 Working with variables ............................. 72 Chapter 5 Making decisions in programs ...................... 104 Chapter 6 Repeating actions with loops ....................... 140 Chapter 7 Using functions to simplify programs ............... 170 Chapter 8 Storing collections of data ......................... 210 Part 2: Advanced programming Chapter 9 Use classes to store data .......................... 264 Chapter 10 Use classes to create active objects ................. 308 Chapter 11 Object-based solution design ...................... 372 Chapter 12 Python applications ............................... 438 Part 3: Useful Python (Digital-only) The chapter PDF files for this Part are available at https://aka.ms/BeginCodePython/downloads. Chapter 13 Python and Graphical User Interfaces .............. 488 Chapter 14 Python programs as network clients ............... 548 Chapter 15 Python programs as network servers ................ 570 Chapter 16 Create games with Pygame ........................ 592 Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Part 1: Programming fundamentals 1 Starting with Python . . . . . . . . . . . . . . . . . . . . . . . . . . 2 What is Python? ............................................... 4 Python origins ......................................... 5 Python versions ....................................... 5 Build a place to work with Python .............................. 6 Get the tools .......................................... 6 Python for Windows PC ................................ 7 Start Python ................................................. 10 What you have learned ....................................... 14 2 Python and Programming . . . . . . . . . . . . . . . . . . . . . 16 What makes a programmer ................................... 18 Programming and party planning ..................... 18 Programming and problems .......................... 19 Programmers and people ............................. 21 Computers as data processors ................................. 22 Machines and computers and us ....................... 22 Programs as data processors .......................... 24 Python as a data processor ............................ 25 Data and information ......................................... 31 Work with Python functions ................................... 36 The ord function ..................................... 36 The chr function ..................................... 38 Investigate data storage using bin ..................... 39 What you have learned ....................................... 41 3 Python program structure . . . . . . . . . . . . . . . . . . . . 44 Write your first Python program ............................... 46 Run Python programs using IDLE ...................... 46 Get program output using the print function ......... 51 Use Python libraries .......................................... 57 The random library ................................... 57 The time library ...................................... 60 Python comments ............................................ 61 Code samples and comments ......................... 62 Run Python from the desktop ................................. 63 Delay the end of the program ......................... 64 Adding some snaps ........................................... 64 Adding the Pygame library ............................ 65 Snaps functions ....................................... 66 What you have learned ....................................... 70 4 Working with variables . . . . . . . . . . . . . . . . . . . . . . . 72 Variables in Python ........................................... 74 Python names ........................................ 76 Working with text ............................................ 79 Marking the start and end of strings ................... 81 Escape characters in text .............................. 82 Read in text using the input function .................. 84 Working with numbers ........................................ 87 Convert strings into integer values ..................... 87 Whole numbers and real numbers ..................... 89 Real numbers and floating-point numbers ............. 90 Convert strings into floating-point values .............. 95 Perform calculations .................................. 96 Convert between float and int ...................... 98 Weather snaps .............................................. 101 What you have learned ...................................... 102 5 Making decisions in programs . . . . . . . . . . . . . . . . 104 Boolean data ................................................ 106 Create Boolean variables ............................. 106 Boolean expressions ................................. 109 Comparing values .................................... 111 Boolean operations .................................. 114 The if construction ......................................... 118 Nesting if conditions ................................ 127 Working with logic .................................. 128 Use decisions to make an application ......................... 129 Design the user interface ............................. 129 Implement a user interface ........................... 130 Testing user input ................................... 132 Complete the program .............................. 133 Input snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 What you have learned ...................................... 138 6 Repeating actions with loops . . . . . . . . . . . . . . . . . 140 The while construction ...................................... 142 Repeat a sequence of statements using while ........ 142 Handling invalid user entry ........................... 147 Detect invalid number entry using exceptions ......... 152 Exceptions and number reading ...................... 154 Handling multiple exceptions ......................... 156 Break out of loops ................................... 157 Return to the top of a loop with continue ............ 158 Count a repeating loop .............................. 159 The for loop construction ................................... 162 Make a digital clock using snaps .............................. 167 What you have learned ...................................... 168 7 Using functions to simplify programs . . . . . . . . . . 170 What makes a function? ...................................... 172 Give information to functions using parameters ....... 176 Return values from function calls ..................... 185 Build reusable functions ..................................... 193 Create a text input function .......................... 193 Add help information to functions .................... 195 Create a number input function ...................... 197 Convert our functions into a Python module .......... 201 Use the IDLE debugger .............................. 202 What you have learned ...................................... 208 8 Storing collections of data . . . . . . . . . . . . . . . . . . . 210 Lists and tracking sales ....................................... 212 Limitations of individual variables ..................... 214 Lists in Python ....................................... 215 Read in a list ......................................... 218 Display a list using a for loop ........................ 219 Refactor programs into functions ............................. 221 Create placeholder functions ......................... 224 Create a user menu .................................. 225 Sort using bubble sort ....................................... 227 Initialize a list with test data .......................... 228 Sort a list from high to low ........................... 228 Sort a list from low to high ........................... 234 Find the highest and lowest sales values ............... 235 Evaluate total and average sales ...................... 236 Complete the program ............................... 237 Store data in a file ........................................... 238 Write into a file ...................................... 239 Write the sales figures ................................ 242 Read from a file ...................................... 244 Read the sales figures ................................ 246 Deal with file errors .................................. 247 Store tables of data .......................................... 251 Use loops to work with tables ......................... 253 Use lists as lookup tables ..................................... 255 Tuples ...................................................... 256 What you have learned ...................................... 259 Part 2: Advanced programming 9 Use classes to store data . . . . . . . . . . . . . . . . . . . . . 264 Make a tiny contacts app ..................................... 266 Make a prototype ................................... 267 Store contact details in separate lists .................. 269 Use a class to store contact details .................... 272 Use the Contact class in the Tiny Contacts program ................................... 275 Edit contacts ........................................ 278 Save contacts in a file using pickle .................... 289 Load contacts from a file using pickle ................. 292 Add save and load to Tiny Contacts ................. 293 Set up class instances ................................ 294 Dictionaries ................................................. 300 Create a dictionary .................................. 300 Dictionary management ............................. 302 Return a dictionary from a function ................... 303 Use a dictionary to store contacts ..................... 303 What you have learned ...................................... 305 10 Use classes to create active objects . . . . . . . . . . . 308 Create a Time Tracker ........................................ 310 Add a data attribute to a class ........................ 311 Create a cohesive object ............................. 312 Create method attributes for a class ................... 314 Add validation to methods ........................... 316 Protect a data attribute against damage .............. 328 Protected methods .................................. 331 Create class properties ....................................... 332 Evolve class design ........................................... 337 Manage class versions ............................... 340 The __str__ method in a class ............................... 346 Python string formatting ............................. 348 Session tracking in Time Tracker .............................. 350 The Python map function ............................. 355 The Python join method ............................ 361 Make music with Snaps ...................................... 363 What you have learned ...................................... 368 11 Object-based solution design . . . . . . . . . . . . . . . . 372 Fashion Shop application ..................................... 374 Application data design .............................. 376 Object-oriented design .............................. 376 Creating superclasses and subclasses .................. 379 Data design recap ................................... 396 Implement application behaviors ..................... 405 Objects as components .............................. 409 Create a FashionShop component ........................... 410 Create a user interface component ................... 417

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.