1. The methods of Variable:
String Methods:
(1) name.endswith('a') # if the variable ends with sring 'a'
(2) name.startswith('a')
(3) name.lower() # Change the variable to lower string
(4) name.upper()
(5) name.isspace() # if the variable is space
(6) name.strip() # Delete start and end space of the string
(7) list = '1+2+3+4'.split('+') # Split the string with specified symbol
Dict Methods:
(1) d.values
(2) d.keys # Return the key list
(3) d.items # Return the dict as tuple
for key, value in d.items():
(4) d.setdefault('name',[]) # Set [] to key 'name'
d.setdefault('Lie',[]).append(my_sister)
(5) d.get('key') # Get the value of key
(6) Zip Function # P:79
1) names = ['Guo','Hong','Li','Qing',]
ages = ['5','10','15','20',]
dict_v = zip(names,ages)
(7) enumerate Function: P80
1) for index,string in enumerate(strings):
if 'xxx' in string:
strings[index]='Replaced'
List Methods:
(1)
Assert:
(1) assert 0 < age <100 'Error return info' # P:76 like:if not condition: crash program
2. Two function like methods
(1) input() and raw_input()
(2) range(1,10) and xrange(1,10)
3. If sentence and while and for:
(1) [x*x for x in range(10)] #82
(2) [x*x for x in range(10) if x % 3 == 0]
(3) [(x,y) for x in range(3) for y in range(4)]
4. Function
(1) parameter in function #P98
1) def hello(greeting='Hello',name='Hong'): # Parameter with default value
hello(name='Qing',greeting='Go') # Parameter don't care the location
2) def print_params(title,*params): #P99, all other values save to params as tuple
3) def print_params_2(x,y,z=3,*pospar,**keypar): #100, parse the parameter with key words.
print_params_2(1,2,3,5,6,7,foo=1,bar=2)
(2) Function lib
1) map #P109
2) filter(fund,seq) #P109
3) reduce(). #P110
4) from random import choice; x=choice('string',[1,2,3]) #随机选择函数
5)repr(x). #P115,返回数据类型的原生态
6) fun.__doc__ # display the help message which in the first line with ''' docInfo''''
5. module
import hello
hello.__name__
hello.__all__ #p173 only import the wanna function
hello.__doc__ # display the doc which define at the
hello.__file__ #Got the full path of this module
10. Useful Module and it's functions
(1)import sys
sys.path.append('/home/hguo1/scripts/python') # must follow with full path
sys.path.appendusr('~/scripts/python')
(2)import sys, pprint
pprint.pprint(sys.path) # Get all the path which python used.
dir(sys) # display all the function in sys module
sys.__all__ # all the use fun import through: import sys
help(copy.copy) # get the help info
copy.__file__ # get the full path of this module
(3)import webbrowser
webbrowser.open('http://www.baidu.com')
(4)fileinput