Python quick reference

11 minute read

Resources

Contents

  1. Special Characters
  2. Variables and Operators
  3. Functions
  4. Input/Output
  5. Control Flow
  6. Data Structures & Classes
  7. Modules
  8. Python for data analysis
  9. Selected topics
  10. Other

Special characters

  • #: add comments after #
  • common escape sequences:
    \t, \n, \', \" ,\\ ...
  • \: line continuation character
  • ' ' and " " have the same effects
  • use ; between statements to allow multiple statements in a single line
  • True, False, None

Variables and operators

  • basics:

      +,-,*,/ ...  # straight forward
    	
      %  # modulo operator
      // # floor division: take floor() after division
      a**b # exponential: a^b
    	
      # bitwise operator same as C
      A | B #Set union 
      A & B #Set intersection
      A & ~B	#Set subtraction 
      ALL_BITS ^ A or ~A 	#Set negation 
      A |= 1 << bit 	# Set 1 to bit 
      A &= ~(1 << bit) 	# Clear bit 
      (A & 1 << bit) != 0 	# Test bit 
      A&-A or A&~(A-1) or x^(x&(x-1)) 	#Extract last bit 
      A&(A-1) 	#Remove last bit 
      ~0 	#Get all 1-bits
    

    for a review of bitwise operations: bitwise operations review

  • Logical variables: True, False
  • Logical operators:
    not(),A or B, C and D
  • check type of a variable:
    print type(variable) or print type(variable).__name__
  • multiple assignment of variables:
    a,b,c = 1,2,'power'
  • standard data types:
    • numbers: var1 = 1; var2 = 2.0 # var1 int, var2 double
      del var1, var2 # remove variables
    • string
    • list
    • tuple
    • dictionary
  • global/local variables:

      global var # assign `var` to its global value in a local environment (eg. in a function)
      globals()  # gives a dict of global variables
      locals()   # gives a dict of local variables
    
  • the dir() function:

      print dir() # all variables in the current space
      print dir(module_name) # all variable names in a module
    

Functions

  • define a function:

      # a simple function example
      def my_fun(x,y):
          print "sum of x and y is ", x+y
          return (x,y,x+y) # or {'x':x,'z':x+y}
    	
      # use the function
      my_fun(2,3) 
      my_fun(x=2,y=3)	
    

    Note that you can return multiple things separated by , as a ordered list of items. Let out = my_fun(1,3). We can access values by out[0] (or out['x'] in the commented case)

  • Pass by reference vs value
    All python parameters are passed by reference, i.e. when a parameter is used in a function, a new reference to the same object is created and used in the statements in the function body.

      # example function
      def change(mylist):
          mylist.append('2'); # change the object
          mylist += ['a','b']; # change the object
          mylist = mylist + ['c','d']; # does not change the object 
          # because at this line, a new object is created and mylist become a reference to that, while the old object remains
          return;
    
  • variable length arguments in functions

      # example function
      def eg(arg1, *vartuple):
          print arg1
          for var in vartuple
              print var
          return
      # the argument followed by * holds the remaining parameters
    
  • lambda functions (small anonymous functions)

      # lambda function example
      g = lambda x: x**2
      f = lambda x,y: x**2 + y**2
      print g(3)
      print g(1,2)
    

Input/Output

command line input/output

  • print examples:

      # basics
      print "hello world"
      print "hello", "world"
      print "Power" + "God" # concatenating, no space in between
      word1 = "hello"; word2 = "world"
      print "say: %s %s" % (word1,word2)
      print "say: %s" % word1
      print word1, word2
    	
      # more examples
      print "God " *10 # repeatedly printing
      print """ 
      type in as long as you want
      you can use multiple lines
      """  # the use of tripple quotes
    	
    
    • use %s for strings, %d for integers, %f for floating point. Use %r when debugging for raw version of variables
    • if add a , at the end of a print sentence, then it doesn’t end the line.
  • taking inputs interactively:

      prompt = '>' # optional
      print "tell me your age: "
      age = raw_input(prompt)
      #eg
      age = raw_input("How old are you?")
      #or
      import sys
      data = sys.stdin.read() # read from input
    
    • you need to use print( ) in python3 or later versions
  • passing command line input:

      from sys import argv
      # first argument is always the script
      script, first, second  = argv 	
      # first argument in arg1, the remaining in arg
      script, arg1, *arg = argv 
    

file input/output

  • file objects:
    use txt = open(filename) where the open function returns a file object. There are also three modes, 'r'(read), 'w'(write), and 'a'(append), for opening the file. With the file object, you can do:

      txt.read()     
      txt.write("stuff")
      txt.readline() # read only one line   
      txt.truncate() # erase the file    
      txt.close() # close the file
    	
      # others
      txt.name  # name of the file
      txt.closed # bool: whether the file is closed
      txt.mode # open mode
    	
    

Control Flow

Indentation matters in the following codes.

# if sentence:
if cond1:
	statement1
elif cond2:
	statement2
else:
	statement3
	
# conditional expression	
x = val1 if condition else val2 # same as
if condition: x = val1
else: x = val2

	
# for loop:
for iterator in range:
	actions_in_each_iteration
else:
	after_condition_violation
	
# while loop:
while (condition):
	actions_in_each_iteration
else:
	after_condition_violation
	
# loop control statements
break
continue
pass # null operation

Note that an else: statement can be added after each looping structure.

Data Structures & Classes

Numbers

Tutorialspoint: Numbers overview

  • type convertion functions:
    int(x); long(x); float(x); complex(x)

Strings

# initialization
mystring = "a test sentence"

# member functions
mystring.split(' ') # split by ' ' into a vector of strings
mystring.strip() # remove the return at the end (if any)
mystring.split(' ') # split strings 
mystring.join(seq) # concatenate values in seq using mystring as separater
mystring.endswith(str) # test the end of mystring
mystring.replace(str1, str2) # replace str1 with str2, return new string
mystring.isupper(); mystring.islower() # test upper/lower case
mystring.index('a')
mystring.count('c') # get index and counts of a substring

# others
str[1:3] 
str[2:] # subsetting
str*2 # string repitition
str +  "another string"  # string concatenation
str() # convert to string
list(s) # turn string into list of characters

str[1]='a' #DOES NOT WORK! strings do not support modification
ord('x') # letter to ASCII code
chr(5) # code to letter

List

# initialization with []
mylist = [1,2,'a',(1,2,3),[1,2]]
mylist = ['a'] * 10  # initialize with length

# member functions
mylist.append() # append to the end of list
mylist.sort() # sort the list (in place)
mylist.pop()	# pop the last item
mylist.pop(i) # pop the ith
mylist.reverse()
mylist.insert(index, obj) # insertion 
mylist.remove(object) # deletion
mylist.count(item)  # count the frequency of item
# ... etc

# others
mylist[2:]  # subsetting
mylist[-3:]
mylist[:2]
mylist[1:2]
mylist[::-1] # reversing
mylist*2 # repetition
mylist + list2 # list concatenation
del mylist[2] # item deletion
3 in [1,2,3]  # membership test
list(var) # convert to list

Tuples

The main differences between lists and tuples are: Lists’ size can be changed, while tuples cannot be updated. Tuples can be thought of as read-only lists.

# initialization by round parenthesis
mytuple = ('abc','def',1,2,3)

# member functions
mytuple.index('abc')   # get index
mytuple.count(1)   # get frequency of element

# others (similar to lists)

Dictionary

Dictionary is a hash table like data structure in Python.

# initialization with curly braces
mydict  = {}
mydict = {'one': "power", 'two': "god"}

mydict['one'] = "item 1"
mydict[2] = "item 2" # keys can be almost anything that is immutable

# member functions
mydict.keys()  # get keys
mydict.values() # get values
mydict.get(key,default=None) # get value for key; if no key found, return default
mydict.clear()   # empty the mydict
mydict.items()   # returns a list of tuple (key, value) pairs
mydict.update(mydict2)  # add mydict2 elements to mydict; repeated keys in mydict2 will be discarded
mydict.pop(key) # remove an item
mydict.setdefault(key,default_value) # use default_value if key not found

Sets

# set initialization
s = set([...]) 

# operations
s.add('element') # add elements

CLASSES

Python is an object-oriented language, thus it’s very easy to define classes and objects.

## create a class
class myclass:
    "this is the first class I create"
    name = ""
    height = 0
    weight = 0
    bmi = 0
    __secret = "hidden variable"

    # class initiation method
    def __init__(self,name, height, weight):
        self.name = name
        self.height = height
        self.weight = weight
        self.bmi = (self.weight)/(self.height)**2

    # printing method
    def printmyclass(self):
        print("Name:",self.name)
		print("Height:",self.height,"\nWeight:",self.weight)
        print("Your bmi is calculated as: %.3f" % self.bmi)

	# destroy this object:
	# Python deletes built-in types or class instances automatically to free the memory space
	# just to pop out a message
	def __del__(self):
        print(self.__class__.__name__, "destroyed")
	
## use a class
me = myclass("PowerGod",1.72,68)
me.printmyclass() 
print me.bmi

## methods on a class object
print (hasattr(me,"height")) # test existence of an attribute
print (getattr(me,"height")) # get an attribute
setattr(me,"height",1.78)	# ...
delattr(me,"weight")
me.printmyclass()

## selected built-in attributes
me.__doc__   # documentation string
me.__dict__ # dictionary of class's namespace

# private variables
# name the variables you want to hide with double underscore prefix
print (me.__secret) # will give error

Class inheritance: assume that myclass is defined, then we can define a child subclass that inherit all its attributes:

class child(myclass):
    """
    this is a subclass of myclass
    all myclass's attributes can be inherited
    """
    major=""
    year=1993

    def printchild(self):
        print(self.name," is going to graduate in ",self.year)

me2 = child("PowerGod",1.00,45.00) # using parent class method
me2.printchild()  # using subclass method
me2.printmyclass() # using parent class method

Packages and Modules

Modules

# a python source file can be used as a module
# import a python source file support.py
import support  # similar to source(support.R)
support.myfun() # use myfun() from support.py

# import certain items from module
from module_name import item1, item2
from support import myfun # example
myfun()  # this time no need to use module name in the front

# import all names from the module
from moduel_name import * 
myfun()  # no need to use module name in the front

Packages
A package is a hierachical directory with multiple modules. For instance, I have a folder temp, with source files file1 and file2.

# to make it into a package, add a __init__.py file in the temp folder, with commands
__all__ = ["file1","file2"]
from temp.file1 import item1, item2 ...
from temp.file2 import item1, item2 ...

# now we can import this temp package 
import temp 
from temp import file1 # or part of the package
file1.item1()

# to skip temp.
from temp import *
file1.item1()
file2.item1()
  • the __all__ is used for from temp import *. Without it, import * would result in name collision (item1 exists in both files). __all__ resolves this by requiring to add submodule name in the front.
  • read more on organizing packages:
    python software fundation : modules

Useful Modules/Packages

  • os: provide functions to take actions related to operating system

  • sys: system-specific parameters and functions
    • sys.path ...: modify system searching path
    • sys.argv: command line inputs
    • sys.exit(code): exit program with code
  • subprocess
    • subprocess.call(cmd, shell=True): run cmd as shell command
  • re module for regular expressions:
    Python regular expression

Python for data analysis

The built-in data structures of Python are not easy to use when performing data analysis. A couple of popular packages provide Python many R-like functionalities, and are perfect for data scientists.

  • NumPy for
    • math function evaluations (on arrays, list etc)
    • sampling from distribution (numpy.random.***)
    • array (matrix) manipulation
    • tutorial: NumPy quick start
  • SciPy for scientific computing
    • linear algebra
    • numerical integration
    • optimization
  • Pandas for
    • Series: fixed-size dict
    • DataFrame: most commonly used object
    • Pandas objects are suitable for most Numpy functions
    • import (or write) files from different data structures:
      • Series.to_csv, Series.from_csv
      • DataFrame.to_csv, DataFrame.from_csv
    • tutorial:
      pandas data structure
      pandas tutorial
  • matplotlib for visualization
  • sklearn for
    • machine learning tools
  • yaml for
    • read parameters from files:
      """
      The following is my parameter file:
      par1: a
      par2: [b,c]
      par3: (d,e,f)
      """
      import yaml
      txt = open(file_path)
      pars = yaml.load(txt)
      txt.close()
    

    Then the parameters are saved as elements in pars which is a dict type variable.

  • pickle for saving and restoring python work space:

      import pickle
      # Save objects:
      f = open("results.pckl","wb")
      pickle.dump([obj1,obj2,obj3],f)
      f.close()
    	
      # restore the objects:
      f=open("results.pckl","rb")
      obj1,obj2,obj3 = pickle.load(f)
      f.close()
    
  • data structure modules:
    • heapq for implementations of heaps

Selected topics

Sorting

# sort list using member function
# note: tuple, dict do not have .sort() member
ls =[a,b,c]
ls.sort() # default sorting
ls.sort(key = myfun) # customized sorting

# sort using sorted():
# note this can be used to sort tuple, dict and other objects
sorted(obj, key = myfun) 

Others

  • get help:
    type pydoc python_function in terminal to get document for a python function

  • IDE recommendation
  • object identification:
    id(var), unique identification number for each object

  • assertion:
    assert expression, "error message": if expression result in False, an error is incurred, and the error message is printed

  • try-except exception handling, and raise exceptions

      # simple exception handling
      try:
          code may have error
      except:
          what to do when error exists
    	
      # more exception handling
      try:
         your operations ;
      except ExceptionI, ExceptionII:
         If there is ExceptionI, ExceptionI then execute this block.
      except ExceptionII:
         If there is ExceptionIII, then execute this block.
      else:
         If there is no exception then execute this block.
      finally:
          always execute this block
    
      # raise exceptions, eg:
      def raise_exception(s):
          if s<10:
              raise Errortype("error message")
          return
      try:
          raise_exception(20)
      except Errortype:
          actions on this exception
          raise  
      ........
    	
    

    for a list of Exception types, see exception handling

  • reload module in interactive mode:
    when developing the package, you need to constantly modify and update the modules that have been imported. To re-import after modification, you need to use importlib.reload(module.file).

Updated: