Python Programming Cheatsheet

Here is the quick reference for Python Programming I found on the web. It summarizes common features and usage in shortest and easiest way possible.

There are also some Princeton University developed modules which are very useful. I have also included them with direct links to it’s corresponding webpage on University’s website.

This is just a blog post taking useful content from Princeton University’s site. For the good intend to share the knowledge with other users and programmers. No intention of modifying or removing credits. Just thought to share this awesome resource with everyone. For more in depth reference, above link to university’s website is advised. It is well documented with excellent documentation and programming examples.

So, here it goes –

 

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)

 

Redirection and piping.

Redirecting standard input Redirecting standard output

Piping

Functions.

Anatomy of a function

Example functions

Modules.

Module abstraction

Module control flow

The str data type.

The str module

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

 

stdio module: writing functions.

Standard output API

Anatomy of printf

Formatting codes for writef

 

stdio module: reading functions.

Standard input API

stddraw module.

Stddraw drawing functions

Stddraw control functions

Stddraw shape functions

Stddraw text and color functions

Stddraw animation functions

stdaudio module.

Standard audio API

stdrandom module.

Standard random module

stdarray module.

Standard array module

stdstats module.

Standard stats module

Color data type.

Color API

Picture data type.

Picture API

InStream data type.

InStream API

OutStream data type.

OutStream API

 

 

Pass it on and help others learn if you have too.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s