Appendix D: Python Programming Cheatsheet
Appendix D: Python Programming Cheatsheet
This appendix summarizes the most commonly-used Python language
features in the textbook.
Hello, World.
import stdio
# Write 'Hello, World' to standard output.
stdio.writeln('Hello, World')
|
Editing, compiling, and interpreting.
Built-in data types.
Assignment statements and traces.
Strings.
Integers.
Floating-point numbers.
Booleans.
Comparison operators.
Common functions.
Type conversion.
if and if-else statements.
if-elif-else statements.
if income < 0: rate = 0.00
elif income < 8925: rate = 0.10
elif income < 36250: rate = 0.15
elif income < 87850: rate = 0.23
elif income < 183250: rate = 0.28
elif income < 398350: rate = 0.33
elif income < 400000: rate = 0.35
else: rate = 0.396
|
while and for statements.
break statements.
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.
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.
Our stdio module: reading functions.
Our stddraw module.
Our stdaudio module.
Redirection and piping.
Functions.
Modules.
Our stdrandom module.
Our stdarray module.
Our stdstats module.
The str data type.
Our Color data type.
Our Picture data type.
Our InStream data type.
Our OutStream data type.
Defining a class.
Creating an object.
Using an object.
p = c1.potentialAt(.20, .50)
Special methods.