Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
103 views

Python 2.7 Quick Reference Sheet: ver 2.01 ʹ 110105 (sjd)

This document provides a quick reference sheet for common Python syntax structures, built-in functions, data types, and modules. It includes summaries of assignment statements, selection statements, repetition statements, functions, classes, exceptions, strings, lists, files and more. Interactive help is available in Python through functions like help(), dir() and built-in documentation for modules and functions. Common data types in Python include integers, floats, booleans, strings, tuples, lists and dictionaries.

Uploaded by

Kannada Kuvara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Python 2.7 Quick Reference Sheet: ver 2.01 ʹ 110105 (sjd)

This document provides a quick reference sheet for common Python syntax structures, built-in functions, data types, and modules. It includes summaries of assignment statements, selection statements, repetition statements, functions, classes, exceptions, strings, lists, files and more. Interactive help is available in Python through functions like help(), dir() and built-in documentation for modules and functions. Common data types in Python include integers, floats, booleans, strings, tuples, lists and dictionaries.

Uploaded by

Kannada Kuvara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python  2.

7  Quick  Reference  Sheet   Common  Syntax  Structures   Common  Built-­‐in  Functions  


ver  2.01  ʹ  110105  (sjd)   Assignment  Statement   Function   Returns  
  var  =  exp   abs(x)   Absolute  value  of  x  
Console  Input/Output   dict()   Empty  dictionary,  eg:  d  =  dict()  
Interactive  Help  in  Python  Shell   var  =  input(  [prompt]  )   float(x)    int  or  string  x  as  float  
help()   Invoke  interactive  help   var  =  raw_input(  [prompt]  )   id(obj)   memory  addr  of  obj  
help(m)   Display  help  for  module  m   print  exp[,]  ͙   int  (x)   float  or  string  x  as  int  
help(f)   Display  help  for  function  f   Selection   len(s)   Number  of  items  in  sequence  s    
dir(m)   Display  names  in  module  m   if  (boolean_exp):   list()   Empty  list,  eg:  m  =  list()  
         stmt  ͙   max(s)   Maximum  value  of  items  in  s  
Small  Operator  Precedence  Table   [elif  (boolean_exp):   min(s)   Minimum  value  of  items  in  s  
func_name(args,  ͙)   Function  call          stmt  ͙]  ͙   open(f)   Open  filename  f  for  input  
x[index  :  index]   Slicing   [else:   ord(c)   ASCII  code  of  c  
x[index]   Indexing          stmt  ͙]  
pow(x,y)   x  **  y  
x.attribute   Attribute  reference   Repetition  
range(x)   A  list  of  x  ints  0  to  x  -­‐  1  
**   Exponentiation   while  (boolean_exp):  
round(x,n)   float  x  rounded  to  n  places  
*,    /,  %   Multiply,  divide,  mod          stmt  ͙  
str(obj)   str  representation  of  obj  
+,  -­‐   Add,  subtract   Traversal  
sum(s)   Sum  of  numeric  sequence  s  
>,  <,  <=,  >=,  !=,  ==   Comparison   for  var  in  traversable_object:  
tuple(items)   tuple  of  items  
in,  not  in   Membership  tests          stmt  ͙  
type(obj)   Data  type  of  obj  
not,  and,  or   Boolean  operators   Function  Definition  
def  function_name(  parmameters  ):    
NOT,  AND,  OR    
       stmt  ͙   Common  Math  Module  Functions  
 
Module  Import   Function  Call   Function   Returns  (all  float)  
import  module_name   function_name(  arguments  )   ceil(x)   Smallest  whole  nbr  >=    x  
from  module_name  import  name  ,  ͙   Class  Definition   cos(x)   Cosine  of  x  radians  
from  module_name  import  *   class  Class_name  [  (super_class)  ]:   degrees(x)   x  radians  in  degrees  
         [  class  variables  ]   radians(x)   x  degrees  in  radians  
Common  Data  Types          def  method_name(  self,    parameters  ):   exp(x)   e  **  x  
Type   Description   Literal  Ex                  stmt   floor(x)   Largest  whole  nbr  <=  x  
int   32-­‐bit  Integer   3,  -­‐4   Object  Instantiation   hypot(x,  y)   sqrt(x  *  x  +  y  *  y)  
long   Integer  >  32  bits   101L   obj_ref  =  Class_name(  arguments  )   log(x  [,  base])   Log  of  x  to  base  or  natural  log  if  
float   Floating  point  number   3.0,  -­‐6.55   Method  Invocation   base  not  given  
complex   Complex  number   1.2J   obj_ref.method_name(  arguments  )   pow(x,  y)   x  **  y  
bool   Boolean   True,  False   Exception  Handling   sin(x)   Sine  of  x  radians  
str   Character  sequence   ͞Python͟   try:   sqrt(x)   Positive  square  root  of  x  
tuple   Immutable  sequence   (2,  4,  7)          stmt  ͙   tan(x)   Tangent  of  x  radians  
list   Mutable  sequence   [2,  x,  3.1]   except  [exception_type]  [,  var]:   pi   Math  constant  pi  to  15  sig  figs  
dict   Mapping   {  x:2,  y:5  }          stmt  ͙   e   Math  constant  e  to  15  sig  figs  
Common  String  Methods   Common  List  Methods   Common  File  Methods  
S.method()   Returns  (str  unless  noted)   L.method()   Result/Returns   F.method()   Result/Returns  
capitalize   S  with  first  char  uppercase   append(obj)   Append  obj  to  end  of  L   read([n])   Return  str  of  next  n  chars  from  F,  
center(w)   S  centered  in  str  w  chars  wide   count(obj)   Returns  int  nbr  of  occurrences  of   or  up  to  EOF  if  n  not  given  
count(sub)   int  nbr  of  non-­‐overlapping   obj  in  L   readline([n])   Return  str  up  to  next  newline,  or  
occurrences  of  sub  in  S   index(obj)   Returns  index  of  first  occurrence   at  most  n  chars  if  specified  
find(sub)   int  index  of  first  occurrence  of   of  obj  in  L;  raises  ValueError  if   readlines()   Return  list  of  all  lines  in  F,  where  
sub  in  S  or  -­‐1  if  not  found     obj  not  in  L   each  item  is  a  line  
isdigit()   bool  True  if  S  is  all  digit  chars,   pop([index])   Returns  item  at  specified  index   write(s)   Write  str  s  to  F  
False  otherwise   or  item  at  end  of  L  if  index  not   writelines(L)   Write  all  str  in  seq  L  to  F  
islower()   bool  True  if  S  is  all  lower/upper   given;  raises  IndexError  if  L  is   close()   Closes  the  file  
isupper()   case  chars,  False  otherwise     empty  or  index  is  out  of  range    
join(seq)   All  items  in  seq  concatenated   remove(obj)   Removes  first  occurrence  of  obj   Other  Syntax  
into  a  str,  delimited  by  S   from  L;  raises  ValueError  if  obj  is   Hold  window  for  user  keystroke  to  close:  
lower()   Lower/upper  case  copy  of  S   not  in  L   raw_input(͞Press    <Enter>  to  quit.͟)  
upper()   reverse()   Reverses  L  in  place   Prevent  execution  on  import:  
lstrip()   Copy  of  S  with  leading/  trailing   sort()   Sorts  L  in  place   ŝĨͺͺŶĂŵĞͺͺсс͞ͺͺŵĂŝŶͺͺ͗͟  
rstrip()   whitespace  removed,  or  both              main()  
split([sep])   List  of  tokens  in  S,  delimited  by   Common  Tuple  Methods    
sep;  if  sep  not  given,  delimiter   T.method()   Returns   Displayable  ASCII  Characters  
is  any  whitespace   count(obj)   Returns  nbr  of  occurrences  of  
  32   SP   48   0   64   @   80   P   96   `   112   p  
obj  in  T  
Formatting    Numbers  as  Strings   33   !   49   1   65   A   81   Q   97   a   113   q  
index(obj)   Returns  index  of  first  occurrence  
Syntax:    ͞format_spec͟  %  numeric_exp   34   ͞   50   2   66   B   82   R   98   b   114   r  
of  obj  in  T;  raises  ValueError  if  
format_spec  syntax:    %  width.precision  type   obj  is  not  in  T   35   #   51   3   67   C   83   S   99   c   115   s  
  36   $   52   4   68   D   84   T   100   d   116   t  
x width  (optional):  align  in  number  of  colums  
specified;  negative  to  left-­‐align,  precede  with   Common  Dictionary  Methods   37   %   53   5   69   E   85   U   101   e   117   u  
0  to  zero-­‐fill   D.method()   Result/Returns   38   &   54   6   70   F   86   V   102   f   118   v  
x precision  (optional):  show  specified  digits  of   clear()   Remove  all  items  from  D   39   ͚   55   7   71   G   87   W   103   g   119   w  
precision  for  floats;  6  is  default   get(k  [,val])   Return  D[k]  if  k  in  D,  else  val   40   (   56   8   72   H   88   X   104   h   120   x  
x type  (required):  d  (decimal  int),  f  (float),  s   has_key(k)   Return  True  if  k  in  D,  else  False   41   )   57   9   73   I   89   Y   105   i   121   y  
(string),  e  (float  ʹ  exponential  notation)   items()   Return  list  of  key-­‐value  pairs  in   42   *   58   :   74   J   90   Z   105   j   122   z  
x Examples  for  x  =  123,  y  =  456.789   D;    each  list  item  is  2-­‐item  tuple   43   +   59   ;   75   K   91   [   107   k   123   {  
͞йϲĚ͟  %  x  -­‐>  .  .  .  123   keys()   Return  list  of  D͛ƐŬĞLJƐ   44   ,   60   <   76   L   92   \   108   l   124   |  
͞йϬϲĚ͟  %  x  -­‐>  000123     pop(k,  [val])   Remove  key  k,  return  mapped  
͞%8.2f  %  y  -­‐>  .  .  456.79   45   -­‐   61   =   77   M   93   ]   109   m   125   }  
value  or  val  if  k  not  in  D  
͞ϴ͘ϮĞ͟йLJ-­‐>  4.57e+02   46   .   62   >   78   N   94   ^   110   n   126   ~  
values()   Return  list  of  D͛s  values  
͞-­‐8s͟й͞,ĞůůŽ͟-­‐>  Hello  .  .  .   47   /   63   ?   79   O   95   _   111   o   127   DEL  
µ\¶ µ\W¶ µ\n¶  =  10  

You might also like