die function that prints out an error message, in case the else doesn't make sense. Shows errors.
Back
modules
Front
-aka libraries
-feature sets you can import into a program
Back
close()
Front
- method flushes unwritten information and closes file object
- Not necessary, but important best practice
Back
while
Front
controls flow of the program with truth statements. Statements inside the while loop are executed until the expression evaluates false.
Back
%r
Front
String format character; use for debugging
Back
\t
Front
tab character
Back
**
Front
exponent
Back
%d
Front
Integer format character
Back
exists()
Front
Returns TRUE if file in argument exists, FALSE if not
Back
truncate()
Front
Empties the file
Back
raw_input('prompt:')
Front
Reads a line of input from user and returns as string
Back
pop()
Front
method removes and returns the last object from a list
Back
boolean value
Front
True or False
Back
function
Front
1. Names code like variables name strings/numbers
2. Takes arguments the way scripts take argv
3. Using 1 and 2, allows for mini-commands
Back
""" ............."""
Front
String for multiple lines of text; can be multi-line comment
Back
floating point numbers
Front
-Scientific notation in computers
-Allows very large and small numbers using exponents
-Made up of:
Significand: 5, 1.5, -2.001
Exponent: 2, -2
-Put decimal after integers to make floating point
1 ~ 1.0
Back
%s
Front
String format character; use for user formatting
Back
Treat if statements like...
Front
..paragraphs.
Each if, elif, and else grouping is like a set of sentences. Put blank lines before and after.
Back
open()
Front
- function opens a file
- Required argument is filename
- Default access_mode is read(r)
- Does not return actual content; creates/reads fileObject
-
Back
append()
Front
Adds input to the end of a list
Back
Ordinal Numbers
Front
Start at 1; First, second, third
Back
Cardinal Numbers
Front
Start at 0
Back
len(input)
Front
Return the number of items from a sequence or characters in a string
Back
Never nest if-statements more than..
Front
..two deep. Try to do one deep (put inside another function).
Back
sorted()
Front
Sorts a list from smallest to highest or a string alphabetically
sorted(str, reverse=True) <- Sorts backwards
Back
argv
Front
- argument variable
- variable holds arguments passed to script when running it
script, first, second, third = argv (line 3)
- script = name of python script
- first, second, third = 3 variables arguments assigned to
Back
print
Front
print to console
Back
Boolean tests should be..
Front
...simple. If complex, move calculations to variables earlier in function and use a good name for the variable.
Back
input()
Front
Assumes input is valid python expression, returns evaluated result
Back
upper()
Front
converts a string to uppercase
Back
==
Front
equal
Back
write(stuff)
Front
Writes stuff to file
Back
read()
Front
- method reads a string from an open file
- fileObject.read([count])
- Count = # of bytes to read, reads as much as possible if not given
Back
lower()
Front
converts a string to lowercase
Back
\
Front
escape; tells python to ignore following character, or puts difficult characters into strings when used with specific escape sequences
Back
A while-loop is...
Front
an infinite loop.
"while True" ~ "While true is true, run this:"
Back
x += y
Front
ADD AND
x = x + y
Back
readline()
Front
Reads one line of text file
Back
split()
Front
Method splits a string into separate phrases
- Default is to split on whitespace
split(str, num)
str = separator (optional)
numb = number of separations (optional)
Back
def
Front
Defines a function
def function1():
print "this is function 1"
Back
int()
Front
Convert a string to an integer
int(raw_input(> ))
Back
!=
Front
not equal
Back
A for-loop is...
Front
Back
lower()/upper()
Front
convert input to upper or lowercase
Back
Every if statement must have a(n)...
Front
else
Back
5 % 3
Front
Modulo;
remainder of the division
5 % 3 = 2 (3 goes into 5 once, remainder 2)
Back
How many characters to a line?
Front
80 characters
Back
round()
Front
function rounds floating point numbers
round(1.773) = 2
Back
Front
line character; creates new line in string
Back
close()
Front
- method flushes unwritten information and closes file object
- Not necessary, but important best practice
Back
Section 2
(50 cards)
except
Front
catches the exception and executes codes
Back
%c
Front
Single character
-accepts integer or single char string
Back
%o
Front
unsigned octal
Back
\r
Front
ASCII Carriage Return (CR)
- Resets position to beginning of a line of text
Back
\f
Front
ASCII FormFeed (FF)
- ASCII Control character. Forces printer to eject current page and continue printing at top of another.
Back
%i
Front
signed integer decimal
Back
%u
Front
unsigned decimal
Back
\\
Front
backslash (\)
Back
%g
Front
Same as "e" if exponent is greater than -4 or less than precision
Back
Front
ASCII LineFeed (LF)
- Goes to next line
-newline escape
Back
else
Front
optional. Used after elif to catch other cases not provided for.
Back
%f
Front
Floating point decimal format (lowercase)
Back
is
Front
tests for object identity
Back
or
Front
at least one condition must be met
Back
continue
Front
used to interrupt the current cycle, without jumping out of the whole cycle. New cycle will begin.
Back
%E
Front
Floating point exponential format (uppercase)
Back
and
Front
all conditions in a boolean expression must be met
Back
global
Front
access variables defined outside functions
Back
not
Front
negates a boolean value
Back
try
Front
specifies exception handlers
Back
%G
Front
Same as "E" if exponent is greater than -4 or less than precision
Back
%x
Front
Unsigned hexadecimal (lowercase)
Back
del
Front
deletes objects
Back
\a
Front
ASCII Bell
-may cause receiving device to emit a bell or warning of some kind
Back
from
Front
for importing a specific variable, class or a function from a module
Back
\'
Front
Single Quote (')
Back
for
Front
iterate over items of a collection in order they appear
Back
yield
Front
is used with generators
Back
return
Front
exits the function and returns a value
Back
%d
Front
signed integer decimal
Back
exec
Front
executes Python code dynamically
Back
elif
Front
stands for else if. if the first test evaluates to False, continues with the next one
Back
%F
Front
Floating point decimal format (UPPERCASE)
Back
%X
Front
Unsigned Hexadecimal (uppercase)
Back
\"
Front
Double-quote (")
Back
pass
Front
does nothing
Back
class
Front
-way of producing objects with similar attributes and methods.
-used to create new user defined objects
-an object is an instance of a class
Back
as
Front
if we want to give a module a different alias
Back
finally
Front
is always executed in the end. Used to clean up resources.
Back
assert
Front
used for debugging purposes
Back
if
Front
Used to determine, which statements are going to be executed
Back
lambda
Front
creates a new anonymous function
Back
import
Front
import other modules into a python script
Back
break
Front
interrupt the (loop) cycle
Back
raise
Front
create a user defined exception
Back
\v
Front
ASCII Vertical Tab (VT)
- 6 vertical lines; 1 inch
ASCII Backspace (BS)
- Erases last character printed
Back
Section 3
(50 cards)
function argument
Front
passed in for function parameter
function(argument)
Back
type()
Front
returns argument value type; str, int, float, etc
Back
function parameter
Front
Variable name for passed in argment
def function(parameter):
Back
instance variable
Front
variable only available to one instance of a class
Back
sys
Front
module - contains important objects and functions
Back
os
Front
module - OS routines for NT or POSIX
Back
bit mask
Front
variable used to determine if bits are on or off in an input
-sort of works like a multiple choice test key
-can be used with | to turn bits on if off
-use with ^ and 11111111 to flip all bits
def check_bit4(input):
mask = 0b1000
desired = input & mask
if desired > 0:
return "on"
return "off"
Back
list slicing
Front
Way to access elements
list[start:end:stride]
-stride = count by __'s
-any term can be omitted, will be set to default
- a negative stride progresses through list backwards
Back
zip()
Front
Combines two or 3 lists to return all values in for loops
Back
items()
Front
Returns a list of a dict's tuple pairs (key, value)
Back
filter()
Front
-filters a list for terms that make the function true
filter(function, list)
filter(lambda x: x%3 ==0, my_list)
-for anonymous (throwaway) functions
Back
~88
Front
Bitwise NOT
flips all bits in a number
for integers, effectively adds 1 and makes negative
Back
// (operator)
Front
Floor Division. Numbers after the decimal in the quotient are removed
- 9//2 = 4
Back
member variables
Front
variables only available to members of certain class
Back
sort()
Front
sorts a list from smallest to greatest
Back
=
Front
assigns values from right side operands to left side operand
Back
<>
Front
Value of two operands not equal?
-Similar to !=
Back
8 & 5
Front
bitwise AND
Turns on bits turned on in BOTH inputs
0b100 & 0b101 = 0b100
Back
universal import
Front
Imports all functions and variables from a module
- Can cause conflicts with user defined functions and vars
- Better to import only necessary functions
from module import *
Back
%r
Front
String
-converts any python object using repr()
Back
%s
Front
String
-Converts any python object using str()
Back
5 << 1
Front
bitwise left shift
-shifts turned on bits to the left
0b001 << 1 = 0b010
Back
12 ^ 42
Front
Bitwise XOR, EXCLUSIVE OR
Turns bits on if EITHER but not BOTH bits of inputs are turned on
0b1010 ^ 0b1101 = 0b0111
Back
Syntax to index 2 nested lists?
Front
list[x][y]
Back
abs()
Front
returns absolute value
Back
range()
Front
Returns a list of numbers from start up to (but not including) stop
start defaults to 0 and step defaults to 1
range(stop)
range(start, stop)
range(start, stop, step)
Back
del keyword
Front
deletes key/value pairs from dict
Back
%=
Front
Modulus AND. Takes modulus using two operands and assigns the result to left operand
A%=B ~ A = A%B
Back
-=
Front
Subtract AND. Subtracts right operand from left and assigns the result to the left.
A-=B ~ A = A - b
Back
max()
Front
returns maximum
Back
values()
Front
Returns an array of dict's values
Back
9 | 4
Front
Bitwise OR
Turns on bits if turned on in either input
0b001 | 0b100 = 0b101
Back
"mutable"
Front
can be changed after created
Back
method
Front
function of an object
Back
keys()
Front
Returns an array of dict's keys
Back
*=
Front
Multiply AND. Multiplies left operand by right and assigns product to left operand
A=B ~ A = AB
Back
tuple
Front
An immutable sequence of Python objects
-Immutable; can't be changed
-Similar to list, but can't be modified
- Uses (), ends in ;
tuple1 = ('word', 1, False);
Back
list comprehension
Front
Python rules for creating lists intelligently
s = [x for x in range(1:51) if x%2 == 0]
[2, 4, 6, 8, 10, 12, 14, 16, etc]
Back
+=
Front
Add AND. Adds right operand to the left and assigns the result to the left.
A += B ~ A = A + B
Back
min()
Front
returns minimum
Back
dictionary
Front
A list whose objects can be accessed with a key instead of an index. Key can be any string or number.
d = {'key1' : 1, 'key2' : 2}
Back
items()
Front
Returns an array of dict key/value pairs
Back
**=
Front
Exponent AND. Performs exponential calculation on operators and assigns value to left operand.
A*=B ~ A = A*B
Back
for loop
Front
for variable in list_name:
-variable is assigned each list item in turn
-indented code below executed on each list item
Back
%
Front
no argument converted, results in "%" in the result
Back
global variable
Front
available everywhere
Back
5 >> 4
Front
bitwise right shift
-shifts turned on bits to the right
0b010 >> 1 = 0b001
Back
get()
Front
Returns a value for the given key. If key is not available, returns default of 'none'.
Back
enumerate()
Front
gives an index number to each element in a list
Back
.remove()
Front
removes items from list
Back
Section 4
(4 cards)
seek()
Front
move to a new position in file, reads bytes
Back
module
Front
a file containing Python definitions, statements or scripts, can be user defined or from a built-in library