字符编码
编程规范(PEP8)
变量1.常量(用大写)
2.变量
常用类型:str 、int、float、 long、bool
字串格式化:
%d 整数
%2d占两位 %02d占两位用0填充
%f 浮点数
%.2f取两位小数
%s字符串
%x十六进制整数
%%表示 %
#单行注解
'''多行注解
>>>n = "abc"
>>>print('i can say %s' %n)
结果:i can say abc
字符串常用功能:
清除空格 strip
分割 split
获得长度 len(obj)
索引 obj[1]
切片 obj[1:10]
列表创建
>>> n = ["abc"]
>>> n
['abc']
>>> n = list('abc')
>>> n
['a', 'b', 'c']
列表常见方法
Eng是一个list
>>>Eng = ['a','b','c']
>>>Eng
>>>['a','b','c']
用索引来访问list
>>>Eng[0]
>>>['a']
用len来获得list元素个数
>>> len(Eng)
结果:3
追加一个元素
Eng.append('d')
删除最后一个元素
Eng.pop()
删除类表种的指定位置的一个元素
Eng.pop(1)
删除一个指定元素
Eng.remove('b')
获得列表的最后一个元素[-1]依此类推[-2]..可以获得倒是第2...个元素
>>> all_item = 95
>>> pager =10
>>> result = all_item.__divmod__(pager)
>>> result
(9, 5)
>>> age = 18
>>> result = age.__eq__(19)
>>> result
False
>>> age = 18
>>> result = age.__float__()
>>> result
18.0
>>> age.__floordiv__(9)
2
>>> age.__floordiv__(19)
0
>>> 18//19
0
>>> dir(name)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> print(type(name))
<class 'str'>
>>> name = 'eric'
>>> name.capitalize()
'Eric'
>>> name="Alen"
>>> result = name.center(20)
>>> print(result)
Alen
>>> result = name.center(20,'_')
>>> print(result)
________Alen________
>>> s = 'abcdabcddcfbgklmlllc'
>>> s.count('d')
3
>>> s.count('d',1,5)
1
>>> name = 'Alan'
>>> result = name.endswith('d')
>>> result
False
>>> result = name.endswith('n')
>>> result
True
>>> result = name.endswith('a',0,3)
>>> result
True
>>> name = 'a l an'
>>> result = name.expandtabs()
>>> result
'a l an'
>>> name = 'abcdefgabcdefggg'
>>> result = name.find('e')
>>> result
4
>>> result2 = name.index('e')
>>> result2
4
>>> result2 = name.index('p')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> w = 'i would like {0} {1}'
>>> result = w.format('a','coffe')
>>> result
'i would like a coffe'
>>>
>>> w = 'i would like {id} {name}'
>>> result = w.format(id='a',name='Alan')
>>> result
'i would like a Alan'
>>> name = list('Alan')
>>> name
['A', 'l', 'a', 'n']
>>> result ="".join(name)
>>> result
'Alan'
>>> w = 'youareright'
>>> result = w.partition("are")
>>> result
('you', 'are', 'right')
>>> w = 'you are right'
>>> result = w.replace('right','wrong')
>>> result
'you are wrong'
>>> a = 'one two three four'
>>> result.replace('o', 'i',1)
'y-u are right'
>>> result=a.replace('o', 'i',1)
>>> result
'ine two three four'
>>> result=a.replace('o', 'i',2)
>>> result
'ine twi three four'
>>> l1 = ['a','b','c']
>>> l1.extend(['d','e',])
>>> l1
['a', 'b', 'c', 'd', 'e']
>>> l1.append('A')
>>> li
>>> l1
['a', 'b', 'c', 'd', 'e', 'A']
['a', 'b', 'c', 'd', 'e', 'A']
>>> l1.insert(0,'B')
>>> l1
['B', 'a', 'b', 'c', 'd', 'e', 'A']
>>> l1
['B', 'a', 'b', 'c', 'd', 'e', 'A']
>>> ret = l1.pop(0)
>>> print(l1)
['a', 'b', 'c', 'd', 'e', 'A']
>>> print(ret)
B
>>> l1.remove('A')
>>> l1
['a', 'b', 'c', 'd', 'e']
>>> l1.reverse()
>>> l1
['e', 'd', 'c', 'b', 'a']
>> dic = dict(k1='v1',k2='v2')
>>> dic
{'k1': 'v1', 'k2': 'v2'}
>>> new_dic = dic.fromkeys(['k1'],'v1')
>>> new_dic
{'k1': 'v1'}
dic =['k1':'v1','k2':'v2']
dic =['k1']
>> dic = {'k1':'v1','k2':'v2'}
>>> dic['k1']
'v1'
>>> dic['k3']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'k3'
>>> dic.get('k3')
>>> print(dic.get('k3'))
None
>>> print(dic.get('k3','没有数据!'))
没有数据!
>>> print(dic.keys())
dict_keys(['k1', 'k2'])
>>> print(dic.values())
dict_values(['v1', 'v2'])
>>> print(dic.items())
dict_items([('k1', 'v1'), ('k2', 'v2')])
>> dic
{'k4': 'v4', 'k1': 123, 'k2': 'v2', 'k3': 'v3'}
>>> ret = dic.update({'k1':234})
>>> print(ret)
None
>>> ret
>>> dic
{'k4': 'v4', 'k1': 234, 'k2': 'v2', 'k3': 'v3'}
dic_list = [11,22,33,44,55,66,77,88,99,90]
dic = {}
for i in dic_list:
if i>66:
if "k1" in dic.keys():
dic['k1'].append(i)
else:
dic['k1'] =[i]
if i<=66:
if "k2" in dic.keys():
dic['k2'].append(i)
else:
dic['k2']=[i]
print(dic)
Eng[-1]
在列表的指定位置插入一个元素
Eng(1,'b')
Tuple元组基本操作
特性:1)元组的元素为只读(不修改)
>>> tl = ('a','b','c','c')
>>> tl[0]='1'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
2)元组的元素的元素可修改
>>> tl = ('a','b',{'k1':'v1'},'c')
>>> tl[2]['k1']
'v1'
>>> tl[2]['k1'] = 'p1'
>>> tl
('a', 'b', {'k1': 'p1'}, 'c')