Skip to Content
A Concise Introduction to Programming in Python
book

A Concise Introduction to Programming in Python

by Mark J. Johnson
December 2011
Beginner
217 pages
8h
English
Chapman and Hall/CRC
Content preview from A Concise Introduction to Programming in Python
Chapter 6
Selection: If Statements
At both high levels and at the machine level, programs execute statements
one after the other. Selection statements allow a program to execute differ-
ent code depending on what happens as the program runs. This flexibility is
another key to the power of computation.
Listing 6.1: Centipede
1 # centipede.py
2
3 from turtle import
*
4
5 def centipede(length, step, life):
6 penup()
7 theta = 0
8 dtheta = 1
9 for i in range(life):
10 forward(step)
11 left(theta)
12 theta += dtheta
13 stamp()
14 if i > length:
15 clearstamps(1)
16 if theta > 10 or theta < -10:
17 dtheta = -dtheta
18 if ycor() > 350:
19 left(30)
20
21 def main():
22 setworldcoordinates(-400, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Standard Library

Python Standard Library

Fredrik Lundh
Numerical Computing with Python

Numerical Computing with Python

Pratap Dangeti, Allen Yu, Claire Chung, Aldrin Yim

Publisher Resources

ISBN: 9781439896952