Python For Kids For Dummies. Scott Brendan
Читать онлайн книгу.dan Scott
Python® For Kids For Dummies®
Python® For Kids For Dummies®
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2015 by John Wiley & Sons, Inc., Hoboken, New Jersey
Media and software compilation copyright © 2015 by John Wiley & Sons, Inc. All rights reserved.
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. Python is a registered trademark of Python Software Foundation. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2015944529
ISBN 978-1-119-09310-7 (pbk); ISBN 978-1-119-11216-7 (ebk); ISBN 978-1-119-11085-9 (ebk)
Introduction
Hi! Welcome to the book. You’re going on a tour of all things Python. If you join me and code along with the projects, you’ll have your basic Python programming wings by the end of the book.
Everything in this book you need to know by doing – typing in the code or, better yet, thinking up the code before reading what I’ve done.
This book walks you through all the parts you have to know about Python programming. You get examples. I talk about planning programs. And I help link you with the broader Python community so that you can head out there after mastering the projects in this book.
Keep these things in mind while you read:
✓ Sometimes words are in italics and then I explain the words. Here is an example: “The objects in the list are called elements.” When you see this sentence, you know to keep your eyes peeled for a definition. (Elements are the objects in a list.)
✓ Python code is written in a different font from the other text. Sometimes it’s inline with the text and looks like this: print('Hello World!').
✓ Sometimes it’s a separate block of text, like this: print('Hello World!')
✓ Some code blocks have a >>> at the front of some lines. I’m showing you what happens when you’re using an interactive Python prompt. You need to type the code that follows the >>> in this book into the Python console that’s running on your computer to see what happens:
>>> my_message = "Hello World!"
>>> print(my_message)
✓ The number of spaces at the front of each line of code is important. The length of your lines isn’t (technically) significant, but Python style guidelines suggest lines with no more than 79 characters (letters, numbers, spaces, or punctuation marks). This book isn’t as wide as your screen; it only lets me show 69 characters in a line. I’ve broken (split) some lines of code in the book. I split them to make sure that the code works and prints the right way. Be careful when you type them in! It’s not always clear how many spaces are at the front of the broken line.
✓ I split lines two ways.
• The first is implicitly. Basically, you can split the code in between any pair of parentheses at a comma. Python still sees it as a single line. The second and later parts of a split line should be indented to where the parentheses opened. Here’s an example from the code in Project 9:
values = (e.first_name, e.family_name,
e. date_of_birth, e.email_address)
Even though you type this as two separate lines, Python sees it as a single line. (Think of it as one long line.) Type this code as you see it, pressing Enter at the end of each line and typing spaces at the start of each line so that the first character in the line is in the right place.
• The second way to split a line is explicitly with the backslash character: \ (not /). Here’s an example from Project 9:
raw_input_prompt = "Press: 1 for training,"+\
" 2 for testing, 3 to quit.\n"
You type these as two separate lines, with the \ at the end of the first line. However, Python sees it as a single line.
✓ When using the Python interpreter in Projects 2 and 3 only, each new line starts with either … or >>>. If you don’t see these in the code in the book, then the previous line is meant to be typed in as one long line. For example, the following code is from Project 2:
>>> my_second_message = 'This name is a little long. Ideally, try to keep the name short, but not too short.'
This code doesn’t have … or >>> at the start of the second or third line. This means you’re supposed to type it all in before pressing Enter. Only press Enter after typing too short.' at the end, not after typing little long.