1. 运行一个文件没有main函数,所以一般要运行一个.py文件,一般在文件中加入:
if '__name__' == '__main__':
##the code you want to run
2. String 的用法
1> len(str)是取字符串的长度
2> str[0] 渠道文件的第几个字符
3> str[1:3] 取字符串从第一个到第三个(不包括)的子字符串
4> str[-2:-1] 取倒数第一个(不包括)到倒数第二个的子字符串
5> str[1:] 第一个到最后一个
6> str[:-2] 第一个到倒数第二个
3. dir(函数名:sys): 列出所有的子功能,函数或者属性
4. help(函数名)。 显示函数的介绍
5. sorted 函数,是用来输出对list排序的结果,对list的值不会改变,很方便。sorted(list, cmp=None, key = None, reverse=False|True).
key 是用来设置调用函数的返回值进行排序,如:
a= ['aaa', 'bb', 'c']
sorted(a, key =len)
6. string的join函数 如:a=['aaaz', 'ddd', 'cc', 'd']
":".join(a) = 'aaaz:ddd:cc:d'
7. string 的split函数: b='aaaz:ddd:cc:d' b.split(":") = ['aaaz', 'ddd', 'cc', 'd'] b.split(":",2) = ['aaaz', 'ddd', 'cc:d']
8. range 函数, 用来对整数进行规则输出成list的
>>> range(1,20,2)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
>>> help(range)
Help on built-in function range in module __builtin__:
range(...)
range([start,] stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
9. del 删除一个变量 del a
10. list 定义: a = ['df', 'dfdg','dge', 2,4]
11. tuple不可改变的list 定义 : b = (2,4,'df')
12. key ---- value
>>> a ={}
>>> a['a'] = 'aaaa'
>>> a['b'] = 'bbbb'
>>> a['c'] = 'cccc'
>>> a
{'a': 'aaaa', 'c': 'cccc', 'b': 'bbbb'}
>>> a.keys
<built-in method keys of dict object at 0x01FB15D0>
>>> a.kyes()
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
a.kyes()
AttributeError: 'dict' object has no attribute 'kyes'
>>> a.keys()
['a', 'c', 'b']
>>> a.values()
['aaaa', 'cccc', 'bbbb']
>>> a.items()
[('a', 'aaaa'), ('c', 'cccc'), ('b', 'bbbb')]
>>> 'a' in a
True
>>> 'd' in a
False
>>>