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.

Editing, compiling, and interpreting Hello, World in Python

Built-in data types.

Built-in types of data

Assignment statements and traces.

Assignment statements trace Assignment statements formal trace

Strings.

Str data type

Integers.

Int data type

Floating-point numbers.

Float data type

Booleans.

Bool data type Boolean operators operators

Comparison operators.

Comparison operators

Common functions.

Common Python functions

Type conversion.

Type conversion API

if and if-else statements.

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.

While and for loops

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']
An array
a = stdarray.create1D(n)
...
for i in range(n):
    stdout.writeln(a[i])
... 
for element in a:
    stdout.writeln(element)

Array operations.

Array operations

Array aliasing and copying.

Array aliasing Array copying

Two-dimensional arrays.

2D array 2D array initialization
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.

Standard output API
Anatomy of printf
Formatting codes for writef

Our stdio module: reading functions.

Standard input API

Our stddraw module.

Stddraw drawing functions
Stddraw control functions
Stddraw shape functions
Stddraw text and color functions
Stddraw animation functions

Our stdaudio module.

Standard audio API

Redirection and piping.

Redirecting standard input Redirecting standard output
Piping

Functions.

Anatomy of a function
Example functions

Modules.

Module abstraction
Module control flow

Our stdrandom module.

Standard random module

Our stdarray module.

Standard array module

Our stdstats module.

Standard stats module

The str data type.

The str module

Our Color data type.

Color API

Our Picture data type.

Picture API

Our InStream data type.

InStream API

Our OutStream data type.

OutStream API

Defining a class.

Charge API
Charge class

Creating an object.

Creating an object

Using an object.

p = c1.potentialAt(.20, .50)

Special methods.

Special methods: arithmetic
Special methods: comparison
Special methods: functions