Python Programming : An Introduction to Computer Science - GitHub

ers are so commonplace in the business world today that the ability to understand and program computers might just give you the edge over your competition, regardless of your occupation. 1.3 What is Computer Science? You might be surprised to learn that computer science is not the study of computers. A famous.
2MB Sizes 384 Downloads 1613 Views
Python Programming: An Introduction to Computer Science John M. Zelle, Ph.D. Preliminary Second Edition Fall 2009

Copyright © 2009 John M Zelle.

All rights reserved. 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, or otherwise, without prior written permission.

This document was typeset by the author with LATEX 2ǫ .

Contents 1 Computers and Programs 1.1 The Universal Machine . . . 1.2 Program Power . . . . . . . 1.3 What is Computer Science? 1.4 Hardware Basics . . . . . . 1.5 Programming Languages . . 1.6 The Magic of Python . . . . 1.7 Inside a Python Program . . 1.8 Chaos and Computers . . . 1.9 Chapter Summary . . . . . 1.10 Exercises . . . . . . . . . . .

. . . . . . . . . .

1 1 2 3 4 5 7 12 14 16 17

. . . . . . . . . . . . . .

21 21 22 24 24 25 27 28 28 30 32 34 37 39 40

3 Computing with Numbers 3.1 Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Using the Math Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 Accumulating Results: Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

45 45 49 51

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

2 Writing Simple Programs 2.1 The Software Development Process . . . . 2.2 Example Program: Temperature Converter 2.3 Elements of Programs . . . . . . . . . . . 2.3.1 Names . . . . . . . . . . . . . . . . 2.3.2 Expressions . . . . . . . . . . . . . 2.4 Output Statements . . . . . . . . . . . . . 2.5 Assignment Statements . . . . . . . . . . . 2.5.1 Simple Assignment . . . . . . . . . 2.5.2 Assigning Input . . . . . . . . . . . 2.5.3 Simultaneous Assignment . . . . . 2.6 Definite Loops . . . . . . . . . . . . . . . . 2.7 Example Program: Future Value . . . . . . 2.8 Chapter Summary . . . . . . . . . . . . . 2.9 Exercises . . . . . . . . . . . . . . . . . . .

i

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . . . . .

Contents

ii 3.4 3.5 3.6 3.7

Li