Home   Notes   Contact Me

Python

Local

External


String Literals

# Normal 'this is a string' # With escaped char: 'line 1\nline 2' # Raw string where \ is not used as an escape r'c:\delme\log.txt' # unicode u'I am UNICODE' # Raw UNICODE ur'c:\delme\log.txt'

Duplicating

#duplicating a list: orig = [ 'a', 'b', 3, 8 ] dupe = orig[:]

Scripting, what to put on first line.

#!/usr/bin/env python {options}

Types

TypeDescription
dictDictionary:
>>> dictVar = { 'one' : 1, 'two' : 2, 'three' : 'some value for 3' } >>> p dictVar { 'three': 'some value for 3', 'two': 2, 'one': 1 } >>> p dictVar['one'] 1 >>> dictVar['four'] = '4th' >>> p dictVar { 'four': '4th', 'three': 'some value for 3', 'two': 2, 'one': 1 } >>> p dictVar.keys() [ 'four', 'three', 'two', 'one' ] >>> p dictVar.update( {'five': 5} ) { 'four': '4th', 'three': 'some value for 3', 'two': 2, 'one': 1, 'five': 5 } >>> p dictVar.update( {'five': 55} ) { 'four': '4th', 'three': 'some value for 3', 'two': 2, 'one': 1, 'five': 55 }
intx = 5
listList:
>>> myList ['a', 'b', 'c'] >>> p myList ['a', 'b', 'c'] >>> p myList[0] 'a' >> p myList[-1] 'c' >>> len(myList) 3 >>> myList.append(5) ['a', 'b', 'c', 5] >>> myList.extend([6, 7]) ['a', 'b', 'c', 5, 6, 7] >>> myList.remove(6) ['a', 'b', 'c', 5, 7] >>> myList.pop() ;; myList 7 ['a', 'b', 'c', 5] >>> myList.pop(1) ;; myList 'b' ['a', 'c', 5] >>> myList.insert(2, 'a') ['a', 'c', 'a', 5] >>> myList.count('a') 2 >>> myList.reverse() ;; myList [5, 'a', 'c', 'a'] [5, 'a', 'c', 'a'] >>> ['it is ' + str(item) for item in myList] ['it is 5', 'it is a', 'it is c', 'it is a']
strStrings:
s = 'string' s = "string" s = "john's string" s = 'john\'s string' #-> john's string s = 'john"s string" s = "john\"s string" #-> john"s string s = "this is one line long\ but defined on 2 lines" s = "line 1 \n line 2" s = "line 1 \n\ line2" s = r"line1 \n still line 1 but with '\n' as not converted to a new line" s = r"line 1 \n still line 1 \n\ still line 1" s = '''line 1 line 2''' s = """line 1 line 2""" su = u'unicode' su = u"unicode"

Type detection

foo = 'a string' # is it a string? if type(foo) == string: print 'it is a string' # is it a dictionary if type(foo) == dict: print 'it is a dictionary'

None

This is basically pythons null value.


Debug: Show Elements

The built in command dir([x])

UsageEffect
dir()Shows all names you have currently defined
dir(module)Shows all names a module defines
dir(object)Shows names of all elements of object
import __builtin__
dir(__builtin__)
Shows built in functions and variables, they are normally not shown by dir()
vars()Shows all the local variables

Enabling Debugging

  1. Include the debug module, put this near the top of your .py file import pdb
  2. Turn on tracing, put this just before the code you want to start debugging pdb.set_trace()
  3. Type h then return to get help

Debugging

Quick Guide

ThingDescription
List, performing an operation on each member Say you have a list of things that have a name property and you want to see all the names Do this to show them:
[onething.name for onething in listOfThings]
Mulitple CommandsUse ;; to seperate multiple commands
Repeating Multiple CommandsUses copy and paste, and just keep pasting the line with multiple commands

A good beginning reference: http://www.ferg.org/papers/debugging_in_python.html