编程爱好者联盟 2017-01-12
1.字符串处理


1 name = "my name is jiachen"
2 #首字母大写
3 print (name.capitalize())
4 #统计字母出现次数
5 print (name.count('a'))
6 #居中打印
7 print (name.center(50,'-'))
8 #字符串装换成bytes类型
9 print (name.encode('utf-8'))
10 #判断以什么结尾
11 print (name.endswith('en'))
12 #将tab转多少个空格
13 print (name.expandtabs())
14 #查找字符开头索引
15 print (name.find('y'))
16 #格式化字符串
17 #name = "my name is {name} and i am {year} old"
18 #print (name.format(name='jiachen',year=27))
19 #print (name.format_map({'name':'jiachen','year':27}))
20 #是否为数字或字母
21 print ('abc123'.isalnum())
22 #是否为字母
23 print ('Abc'.isalpha())
24 #是否为十进制
25 print ('1'.isdecimal())
26 #是否为整数
27 print ('11'.isdigit())
28 #判断是不是一个合法的表示符(变量名)
29 print ('a1A'.isidentifier())
30 #是否为小写
31 print ('Abc'.islower())
32 #是否只有数字
33 print ('213'.isnumeric())
34 #是否为空格
35 print (' '.isspace())
36 #是否每个首字母大写
37 print ('My Name Is'.istitle())
38 #是否能打印,tty file drive file
39 print ('My Name Is'.isprintable())
40 #是否都为大写
41 print ('My'.isupper())
42 #拼接字符串
43 print ('+'.join('abc'))
44 #长50不够用*号后面补上
45 print (name.ljust(50,'*'))
46 #长50不够用*号前面补上
47 print (name.rjust(50,'*'))
48 #变小写
49 print ('Alex'.lower())
50 #变大写
51 print ('alex'.upper())
52 #从左边去掉空格回车
53 print (' jiachen '.lstrip())
54 #从右边去掉空格回车
55 print (' jiachen '.rstrip())
56 #去掉头尾空格
57 print (' jiachen '.strip())
58 #
59 p = str.maketrans('abcdef','123456')
60 print ('jiachen'.translate(p))
61 #字符串替换
62 print ('jaaaiachen'.replace('a','x',1))
63 #从右侧查找
64 print ('jiachen'.rfind('e'))
65 #分割成列表
66 print ('jiachen'.split('a'))
67 #匹配换行符,分割成列表
68 print ('1+2\n+3+4'.splitlines())
69 #反转大小写
70 print ('Jiachen'.swapcase())
71 #变成一个title
72 print ('jiachen'.title())
73 #不够50就前面补零
74 print ('jiachen'.zfill(50)) View Code2.字典操作


info = {
'stu01':'Tom',
'stu02':'Jack',
'stu03':'Ben'
}
#创建
#info['stu04'] = 'petter'
#修改
#info['stu01'] = '汤姆'
#删除
#del info['stu01']
#info.pop('stu02')
#随机删
#info.popitem()
#查询
#print (info['stu01'])
#print ('stu01' in info)
#取字典的value
#print (info.values())
#清空字典
#info.clear()
#复制字典
#info2 = info.copy()
#初始化一个字典
#print (info.fromkeys(['stu04','stu05'],'look'))
#取某个key的值
#print (info.get('stu01'))
#key-value元祖所有添加到列表中
#print (info.items())
#取字典的key
#print (info.keys())
#查找key如果不存在使用默认值
#print (info.setdefault('stu04','Jane'))
#将一个字典添加到另一个字典中
#info2 = {'name':'jiachen'}
#info.update(info2)
#print (info) View Code3.元祖操作
集合、去重、关系测试
集合是一个无序的,不重复的数据组合


list_1 = [1,4,5,7,3,6,7,9] list_1 = set(list_1) list_2 = set([2,6,0,66,22,8,4]) print (list_1,list_2) ''' #求交集 print (list_1.intersection(list_2)) #求并集 print (list_1.union(list_2)) #求差集,1里面有2里面没有 print (list_1.difference(list_2)) #求子集,1是2的子集 print (list_1.issubset(list_2)) #求父集,1是2的父集 print (list_1.issuperset(list_2)) #求对称差集,两个互相没有的,去掉重复的 print (list_1.symmetric_difference(list_2)) #判断是否有交集,有为false,无为true print (list_1.isdisjoint(list_2)) ''' #交集 print (list_1 & list_2) #并集 print (list_1 | list_2) #差集 print (list_1 - list_2) #对称差集 print (list_1 ^ list_2) #添加 list_1.add(999) list_1.update([222,223,224]) #删除 list_1.remove(999) #不存在报错 list_1.discard(888) #不存在不报错 #长度 len(list_1) #测试x是否是a的成员 999 in list_1 #测试x是否不是a的成员 999 not in list_1View Code
4.文件操作
文件操作过程
打开文件获得文件句柄-操作-关闭文件


#文件句柄
#f = open('yesterday','r',encoding='utf-8')
#r模式为读模式
#f = open('yesterday','r',encoding='utf-8')
#w模式为写,创建文件
#f = open('yesterday2','w',encoding='utf-8')
#f.write("我爱北京天安门,\n")
#f.write("天安门上太阳升\n")
#a模式为追加,创建文件
#f = open('yesterday2','a',encoding='utf-8')
#f.write("我爱北京天安门,\n")
#f.write("天安门上太阳升\")
#关闭文件
#f.close()
#读前5行
'''
f = open('yesterday2','r',encoding='utf-8')
for i in range(5):
print (f.readline())
'''
#
'''
f = open('yesterday2','r',encoding='utf-8')
for i in f.readlines():
print (i,)
'''
#high bige
'''
count = 0
f = open('yesterday2','r',encoding='utf-8')
for line in f:
if count == 9:
print ('------我是分割线-------')
count += 1
continue
print (line.strip())
count += 1
'''
#seek和tall用法
'''
f = open('yesterday2','r',encoding='utf-8')
print (f.tell())
print (f.readline().strip())
print (f.readline().strip())
print (f.readline().strip())
print (f.tell())
f.seek(0)
print (f.readline().strip())
'''
#
#f = open('yesterday2','r',encoding='utf-8')
#print (f.encoding)
#强制刷新保存
#f.flush()
#截断
#f = open('yesterday2','r',encoding='utf-8')
#f.truncate(10)
#读写,r+,读和追加
'''
f = open('yesterday2','r+',encoding='utf-8')
print (f.readline())
print (f.readline())
print (f.readline())
f.write('-----diao----\n')
print (f.readline())
'''
#写读,w+,先创建一个文件
'''
f = open('yesterday2','w+',encoding='utf-8')
f.write('-----diao----\n')
f.write('-----diao----\n')
f.write('-----diao----\n')
f.write('-----diao----\n')
print (f.tell())
f.seek(10)
print (f.readline())
f.write('should\n')
'''
#追加读,a+
#读二进制文件
#f = open('yesterday2','rb')
#print (f.readline())
#写二进制文件
#f = open('yesterday2','wb')
#f.write('hello\n'.encode('utf-8'))
#f.close()
#文件修改
f = open('yesterday2','r',encoding='utf-8')
f_new = open('yesterday3','w',encoding='utf-8')
for line in f:
if '肆意的快乐' in line:
line = line.replace('肆意的快乐等我享受','肆意的快乐等贾晨享受')
f_new.write(line)
f.close() View Code5.打印进度条


import sys,time
for i in range(100):
sys.stdout.write('#')
sys.stdout.flush()
time.sleep(0.5) View Code6.字符编码与转码
gbk -> decode -> unicodeunicode -> encode -> gbkutf-8 > decode -> unicodeunicode -> encode -> utf-8
打印默认编码


import sys print (sys.getdefaultencoding())View Code
7.函数介绍-1
三种编程方式:面向对象、面向过程、函数式编程
函数式编程与面向过程编程的区别


1 #函数
2 def func1():
3 '''testing1'''
4 print ('in the func1')
5 return 0
6
7 #过程
8 def func2():
9 '''testing2'''
10 print ('in the func2')
11
12 x = func1()
13 y = func2()
14
15 print ('from func1 return is %s' % x)
16 print ('from func2 retrun is %s' % y) View Code函数介绍-2
代码重用
保持一致性
可扩展


import time
def logger():
with open('a.txt','a+') as f:
time_format = '%Y-%m-%d %X'
time_current = time.strftime(time_format)
f.write('%s end action\n' % time_current)
def test1():
'''testing1'''
print ('test1 starting action...')
logger()
def test2():
'''testing2'''
print ('test2 starting action...')
logger()
def test3():
'''testing3'''
print('test3 starting action...')
logger()
test1()
test2()
test3() View Code8.函数返回值
return用来结束函数,返回值可以赋值给一个变量
没有return,返回None
return一个值,返回一个值
return多个值,返回一个元祖
为什么要有返回值,因为我们需要一个函数执行的结果


1 def test1():
2 print ('in the test1')
3
4 def test2():
5 print ('in the test2')
6 return 0
7
8 def test3():
9 print ('in the test3')
10 return 1,'hello',[12,3],{1:1,2:2}
11
12 x = test1()
13 y = test2()
14 z = test3()
15 print (x)
16 print (y)
17 print (z) View Code9.函数的参数
带参数的函数,x、y形参,xxx、yyy实参


def test(x,y,z=3):
print (x)
print (y)
print (z) View Code位置参数调用,实参和形参要一一对应


test('xxx','yyy') View Code关键字参数调用,与形参顺序无关


test(y=2,x=1)View Code