Hello World! Download at Boykma.Com Licensed to Deborah Christiansen Download at Boykma.Com Licensed to Deborah Christiansen Hello World! Computer Programming for Kids and Other Beginners WARREN SANDE CARTER SANDE MANNING Greenwich (74° w. long.) Download at Boykma.Com Licensed to Deborah Christiansen For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B fax: (609) 877-8256 Greenwich, CT 06830 email:
[email protected] ©2009 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without elemental chlorine. Manning Publications Co. Sound View Court 3B Greenwich, CT 06830 Development editor: Copyeditors: Technical proofreader: Typesetter: Illustrator: Cover designer: Cynthia Kane Andy Carroll, Anna Welles Ignacio Beltran-Torres Marija Tudor Martin Murtonen Leslie Haimes Fourth, corrected printing August 2009 ISBN 978-1-933988-49-8 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 15 14 13 12 11 10 09 Download at Boykma.Com Licensed to Deborah Christiansen To our family, who inspire, encourage, and support us in school, work, and life Download at Boykma.Com Licensed to Deborah Christiansen Download at Boykma.Com Licensed to Deborah Christiansen Contents Preface xiii Acknowledgments xix About this book xxi 1 Getting Started 1 Installing Python 1 Starting Python with IDLE 2 Instructions, please 3 Interacting with Python 5 Time to program 7 Running your first program 8 If something goes wrong 9 Your second program 11 ■ ■ ■ ■ ■ 2 Remember This—Memory and Variables 14 Input, processing, output 14 Names 16 What’s in a name? 20 Numbers and strings 21 How “variable” are they? 22 The new me 23 ■ ■ ■ 3 Basic Math ■ 26 The four basic operations 27 Operators 28 Order of operations 29 Two more operators 30 Really big and really small 33 ■ ■ ■ ■ vii Download at Boykma.Com Licensed to Deborah Christiansen viii CONTENTS 4 Types of Data 38 Changing types 38 Getting more information: type() 41 Type-conversion errors 42 Using type conversions 42 ■ ■ 5 Input 44 45 The print command and the comma Inputting numbers 47 Input from the Web 49 raw_input() ■ 45 ■ 6 GUIs—Graphical User Interfaces 52 What’s a GUI? 52 Our first GUI 53 GUI input 54 Pick your flavo