la la la la l as l as
sh sh sh sh h h
ne ne ne ne ne ne
tz tz tz tz tz t
2nd Edition By John M.
An Introduction to
Computer Science
Sp Sp Sp Sp Sp
An Introduction To Compter Science
la la la la l as
sh sh sh sh h
n e ne ne ne ne
,Sp Sp Sp Sp Sp Sp
la l
a la la la la
sh sh sh sh sh sh
ne ne ne ne ne ne
tz tz tz tz tz
lOMoAR cPSD| 24448587
Sp Sp Sp SpSp
la la la la la
sh sh sh sh sh
ne ne ne ne ne
, lOMoAR cPSD| 24448587
ne
sh
ne
la
tz
sh
Sp
ne
la
Python Programming:
sh
Sp
An Introduction to Computer Science
ne
la
tz
John M. Zelle, Ph.D.
sh
Sp
ne
Preliminary Second Edition
Fall 2009
la
Copyright © 2009 John M Zelle.
sh
Sp
ne
la
tz
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
sh
transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise,
Sp
without prior written permission.
ne
la
sh
Sp
ne
This document was typeset by the author with LATEX 2ë.
la
Contents
tz
sh
Sp
ne
1 Computers and Programs 1
la
1.1 The Universal Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
sh
1.2 Program Power ....................................... 2
Sp
ne
1.3 What is Computer Science? ................................ 3
a
1.4 Hardware Basics ...................................... 4
tz
l
sh
Sp
1.5 Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
ne
1.6 The Magic of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
la
sh
Sp
la
Sp
, lOMoAR cPSD| 24448587
ne
sh
ne
la
tz
sh
Sp
ne
la
1.7 Inside a Python Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
sh
Sp
1.8 Chaos and Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
ne
1.9 Chapter Summary ..................................... 16
la
tz
1.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
sh
Sp
2 Writing Simple Programs 21
ne
2.1 The Software Development Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
la
2.2 Example Program: Temperature Converter . . . . . . . . . . . . . . . . . . . . . . . . 22
sh
Sp
2.3 Elements of Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
ne
2.3.1 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
la
tz
2.3.2 Expressions ..................................... 25
sh
Sp
2.4 Output Statements ..................................... 27
ne
2.5 Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
la
2.5.1 Simple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
sh
Sp
2.5.2 Assigning Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
ne
2.5.3 Simultaneous Assignment ............................. 32
la
tz
2.6 Definite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 sh
Sp
2.7 Example Program: Future Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
ne
2.8 Chapter Summary ..................................... 39
la
2.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
sh
Sp
3 Computing with Numbers 45
ne
3.1 Numeric Data Types .................................... 45
a
tz
l
3.2 Using the Math Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
sh
Sp
3.3 Accumulating Results: Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
ne
la
i
sh
Sp
la
Sp