PythonTheHardWay Flashcards
print “something”
print statement (of something in quotes)
#
pound character used to comment or disable parts of code
+
plus symbol. used to add numbers or concatenate strings
-
minus symbol. used in subtraction
/
slash symbol. used in division
*
asterisk symbol. used in multiplication
%
modulus symbol. used to get remainder in division
less-than symbol.
>
greater-than symbol
less-than-or-equal symbol
> =
greater-than-or-equal symbol
=
single-equal. assigns the value on the right to a variable on the left
==
double-equal. tests if two things have the same value
%s
string formatter
%d
number formatter
%r
raw data formatter. displays the raw data of a variable.
\n
newline symbol
\t
tab symbol
\
escape character
raw_input()
reads a line from input and converts to string
argv
the argument variable. holds the arguments passed into a script from the command line.
import
imports any Python source file as a module
open()
open a file, e.g. txt = open(filename).
read()
read the contents of a file, e.g. txt.read()
close()
close a previously opened file
write()
write to an open file, e.g. f.write(‘some text’)
truncate()
truncate the file size
*args
allows you to pass a non-keyworded variable argument list to a function
seek()
set the position of the read/write pointer within a file
readline()
reads one entire line from the file
not False
True
not True
False
True or False
True
True or True
True
False or True
True
False or False
False
True and False
False
True and True
True
False and True
False
False and False
False
not (True or False)
False
not (True or True)
False
not (False or True)
False
not (False or False)
True
not (True and False)
True
not (True and True)
False
not (False and True)
True
not (False and False)
True
1 != 0
True
1 != 1
False
0 != 1
True
0 != 0
False
1 == 0
False
1 == 1
True
0 == 1
False
0 == 0
True
as
part of the with-as statement
assert
ensure something is true
break
stop this loop right now (while True: break)
class
define a class, e.g. class Person(obj)
continue
don’t process more of a loop, do it again (while True: continue)
def
define/declare a function
del
delete from a dictionary, e.g. del X[Y]
elif
else if condition
else
else condition
except
if an exception occurs, do this (except ValueError, e: print e)
exec
run a string as Python
finally
exceptions or not, finally do this no matter what
for
loop over a collection of things
from
import specific parts of a module
global
declare a global variable
if
if condition
in
part of a for-loop
is
like == to test equality
lambda
create a short anonymous function
pass
this block is empty
raise
raise an exception when things go wrong
return
exit the function with a return value
try
test this block and if exception, go to except
while
while loop
with
with an expression as a variable do
yield
pause here and return to caller
None
Represents “nothing” or “no value”
string
stores textual information
int
stores numbers
float
stores decimals
list
stores a collection of things
dict
stores a key=value mapping of things
%i
same as %d
%o
octal number
%u
unsigned decimal
%x
hexadecimal lowercase
%X
hexadecimal uppercase
%e
exponential notation, lowercase ‘e’
%E
exponential notation, uppercase “E”
%f
floating point real number
%F
same as %f
%g
Either %f or %e, whichever is shorter
%G
same as %g but uppercase
%c
character format
%%
a percent sign
**
power of
//
floor division (2 // 4.0 == 0.5)
not equal
[ ]
list brackets
{ }
dict brackets
@
decorator
+=
add and assign
-=
subtract and assign
*=
multiply and assign
/=
divide and assign
//=
floor divide and assign
%=
modulus assign
**=
power assign (x **= 2)