#!/usr/bin/env python {options}
| Type | Description |
| dict | Dictionary:
>>> 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 }
|
| int | x = 5 |
| list | List:
>>> 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']
|
| str | Strings:
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"
|
This is basically pythons null value.
| Usage | Effect |
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 |
import pdbpdb.set_trace()h then return to get help| Thing | Description |
| 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 Commands | Use ;; to seperate multiple commands |
| Repeating Multiple Commands | Uses 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