You are on page 1of 12

A PPENDIX D: P YTHON P ROGRAMMING C HEATSHEET

This appendix summarizes the most commonly-used Python language features in the textbook.

Hello, World.

import stdio

INTRO TO P ROGRAMMING

# Write 'Hello, World' to standard output.


stdio.writeln('Hello, World')

1. Elements of Programming
2. Functions
3. OOP
4. Data Structures

Editing, compiling, and interpreting.

R ELATED B OOKSITES

WEB R ESOURCES
FAQ
Code

Built-in data types.

Errata
Appendices

Assignment statements and traces.

Strings.

Integers.

Floating-point numbers.

converted by W eb2PDFConvert.com

Booleans.

Comparison operators.

Common functions.

Type conversion.

converted by W eb2PDFConvert.com

if and if-else statements.

if-elif-else statements.

if income
elif income
elif income
elif income
elif income
elif income
elif income
else:

<
<
<
<
<
<
<

0:
8925:
36250:
87850:
183250:
398350:
400000:

rate
rate
rate
rate
rate
rate
rate
rate

=
=
=
=
=
=
=
=

0.00
0.10
0.15
0.23
0.28
0.33
0.35
0.396

while and for statements.

break statements.

converted by W eb2PDFConvert.com

while True:
x = 1.0 + 2.0*random.random()
y = 1.0 + 2.0*random.random()
if x*x + y*y <= 1.0:
break

Arrays.

suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']

a = stdarray.create1D(n)
...
for i in range(n):
stdout.writeln(a[i])
...
for element in a:
stdout.writeln(element)

Array operations.

Array aliasing and copying.

converted by W eb2PDFConvert.com

Two-dimensional arrays.

a = stdarray.create2D(rowCount, colCount)
...
for i in range(rowCount):
for j in range(colCount)):
stdio.writeln(a[i][j])
...
for row in a:
for element in row:
stdio.writeln(element)

Our stdio module: writing functions.

converted by W eb2PDFConvert.com

Our stdio module: reading functions.

Our stddraw module.

converted by W eb2PDFConvert.com

Our stdaudio module.

Redirection and piping.

Functions.

converted by W eb2PDFConvert.com

Modules.

Our stdrandom module.

Our stdarray module.

converted by W eb2PDFConvert.com

Our stdstats module.

The str data type.

converted by W eb2PDFConvert.com

Our Color data type.

Our Picture data type.

Our InStream data type.

Our OutStream data type.

Defining a class.

converted by W eb2PDFConvert.com

Creating an object.

Using an object.
p = c1.potentialAt(.20, .50)

Special methods.

converted by W eb2PDFConvert.com

Copyright 20002015 by Robert Sedgewick, Kevin Wayne, and Robert Dondero. All rights reserved.

converted by W eb2PDFConvert.com

You might also like