码迷,mamicode.com
首页 > 编程语言 > 详细

python之路 - 基础3

时间:2017-01-12 17:19:09      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:ogg   集合   ade   关系   bytes   val   分享   北京   关闭   

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 Code

 

2.字典操作

技术分享
 1 info = {
 2     stu01:Tom,
 3     stu02:Jack,
 4     stu03:Ben
 5 }
 6 
 7 #创建
 8 #info[‘stu04‘] = ‘petter‘
 9 #修改
10 #info[‘stu01‘] = ‘汤姆‘
11 #删除
12 #del info[‘stu01‘]
13 #info.pop(‘stu02‘)
14 #随机删
15 #info.popitem()
16 #查询
17 #print (info[‘stu01‘])
18 #print (‘stu01‘ in info)
19 
20 #取字典的value
21 #print (info.values())
22 #清空字典
23 #info.clear()
24 #复制字典
25 #info2 = info.copy()
26 #初始化一个字典
27 #print (info.fromkeys([‘stu04‘,‘stu05‘],‘look‘))
28 #取某个key的值
29 #print (info.get(‘stu01‘))
30 #key-value元祖所有添加到列表中
31 #print (info.items())
32 #取字典的key
33 #print (info.keys())
34 #查找key如果不存在使用默认值
35 #print (info.setdefault(‘stu04‘,‘Jane‘))
36 #将一个字典添加到另一个字典中
37 #info2 = {‘name‘:‘jiachen‘}
38 #info.update(info2)
39 #print (info)
View Code

 

3.元祖操作

集合、去重、关系测试

集合是一个无序的,不重复的数据组合

技术分享
 1 list_1 = [1,4,5,7,3,6,7,9]
 2 list_1 = set(list_1)
 3 
 4 list_2 = set([2,6,0,66,22,8,4])
 5 print (list_1,list_2)
 6 ‘‘‘
 7 #求交集
 8 print (list_1.intersection(list_2))
 9 #求并集
10 print (list_1.union(list_2))
11 #求差集,1里面有2里面没有
12 print (list_1.difference(list_2))
13 #求子集,1是2的子集
14 print (list_1.issubset(list_2))
15 #求父集,1是2的父集
16 print (list_1.issuperset(list_2))
17 #求对称差集,两个互相没有的,去掉重复的
18 print (list_1.symmetric_difference(list_2))
19 #判断是否有交集,有为false,无为true
20 print (list_1.isdisjoint(list_2))
21 ‘‘‘
22 
23 #交集
24 print (list_1 & list_2)
25 #并集
26 print (list_1 | list_2)
27 #差集
28 print (list_1 - list_2)
29 #对称差集
30 print (list_1 ^ list_2)
31 
32 #添加
33 list_1.add(999)
34 list_1.update([222,223,224])
35 #删除
36 list_1.remove(999)  #不存在报错
37 list_1.discard(888) #不存在不报错
38 #长度
39 len(list_1)
40 #测试x是否是a的成员
41 999 in list_1
42 #测试x是否不是a的成员
43 999 not in list_1
View Code

 

4.文件操作

文件操作过程

打开文件获得文件句柄-操作-关闭文件

技术分享
  1 #文件句柄
  2 #f = open(‘yesterday‘,‘r‘,encoding=‘utf-8‘)
  3 
  4 #r模式为读模式
  5 #f = open(‘yesterday‘,‘r‘,encoding=‘utf-8‘)
  6 
  7 #w模式为写,创建文件
  8 #f = open(‘yesterday2‘,‘w‘,encoding=‘utf-8‘)
  9 #f.write("我爱北京天安门,\n")
 10 #f.write("天安门上太阳升\n")
 11 
 12 #a模式为追加,创建文件
 13 #f = open(‘yesterday2‘,‘a‘,encoding=‘utf-8‘)
 14 #f.write("我爱北京天安门,\n")
 15 #f.write("天安门上太阳升\")
 16 
 17 #关闭文件
 18 #f.close()
 19 
 20 #读前5行
 21 ‘‘‘
 22 f = open(‘yesterday2‘,‘r‘,encoding=‘utf-8‘)
 23 for i in range(5):
 24     print (f.readline())
 25     ‘‘‘
 26 
 27 #
 28 ‘‘‘
 29 f = open(‘yesterday2‘,‘r‘,encoding=‘utf-8‘)
 30 for i in f.readlines():
 31     print (i,)
 32     ‘‘‘
 33 
 34 #high bige
 35 ‘‘‘
 36 count = 0
 37 f = open(‘yesterday2‘,‘r‘,encoding=‘utf-8‘)
 38 for line in f:
 39     if count == 9:
 40         print (‘------我是分割线-------‘)
 41         count += 1
 42         continue
 43     print (line.strip())
 44     count += 1
 45     ‘‘‘
 46 
 47 #seek和tall用法
 48 ‘‘‘
 49 f = open(‘yesterday2‘,‘r‘,encoding=‘utf-8‘)
 50 print (f.tell())
 51 print (f.readline().strip())
 52 print (f.readline().strip())
 53 print (f.readline().strip())
 54 print (f.tell())
 55 f.seek(0)
 56 print (f.readline().strip())
 57 ‘‘‘
 58 
 59 #
 60 #f = open(‘yesterday2‘,‘r‘,encoding=‘utf-8‘)
 61 #print (f.encoding)
 62 #强制刷新保存
 63 #f.flush()
 64 
 65 #截断
 66 #f = open(‘yesterday2‘,‘r‘,encoding=‘utf-8‘)
 67 #f.truncate(10)
 68 
 69 #读写,r+,读和追加
 70 ‘‘‘
 71 f = open(‘yesterday2‘,‘r+‘,encoding=‘utf-8‘)
 72 print (f.readline())
 73 print (f.readline())
 74 print (f.readline())
 75 f.write(‘-----diao----\n‘)
 76 print (f.readline())
 77 ‘‘‘
 78 #写读,w+,先创建一个文件
 79 ‘‘‘
 80 f = open(‘yesterday2‘,‘w+‘,encoding=‘utf-8‘)
 81 f.write(‘-----diao----\n‘)
 82 f.write(‘-----diao----\n‘)
 83 f.write(‘-----diao----\n‘)
 84 f.write(‘-----diao----\n‘)
 85 print (f.tell())
 86 f.seek(10)
 87 print (f.readline())
 88 f.write(‘should\n‘)
 89 ‘‘‘
 90 
 91 #追加读,a+
 92 
 93 #读二进制文件
 94 #f = open(‘yesterday2‘,‘rb‘)
 95 #print (f.readline())
 96 #写二进制文件
 97 #f = open(‘yesterday2‘,‘wb‘)
 98 #f.write(‘hello\n‘.encode(‘utf-8‘))
 99 #f.close()
100 
101 #文件修改
102 f = open(yesterday2,r,encoding=utf-8)
103 f_new = open(yesterday3,w,encoding=utf-8)
104 for line in f:
105     if 肆意的快乐 in line:
106         line = line.replace(肆意的快乐等我享受,肆意的快乐等贾晨享受)
107     f_new.write(line)
108 f.close()
View Code

 

5.打印进度条

技术分享
1 import sys,time
2 
3 for i in range(100):
4     sys.stdout.write(#)
5     sys.stdout.flush()
6     time.sleep(0.5)
View Code

 

6.字符编码与转码

gbk -> decode -> unicode
unicode -> encode -> gbk
utf-8 > decode -> unicode
unicode -> encode -> utf-8

打印默认编码

技术分享
1 import sys
2 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

代码重用

保持一致性

可扩展

技术分享
 1 import time
 2 
 3 def logger():
 4     with open(a.txt,a+) as f:
 5         time_format = %Y-%m-%d %X
 6         time_current = time.strftime(time_format)
 7         f.write(%s end action\n % time_current)
 8 
 9 def test1():
10     ‘‘‘testing1‘‘‘
11     print (test1 starting action...)
12     logger()
13 
14 def test2():
15     ‘‘‘testing2‘‘‘
16     print (test2 starting action...)
17     logger()
18 
19 def test3():
20     ‘‘‘testing3‘‘‘
21     print(test3 starting action...)
22     logger()
23 
24 test1()
25 test2()
26 test3()
View Code

 

8.函数返回值

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 Code

 

9.函数的参数

带参数的函数,x、y形参,xxx、yyy实参

技术分享
1 def test(x,y,z=3):
2     print (x)
3     print (y)
4     print (z)
View Code

位置参数调用,实参和形参要一一对应

技术分享
1 test(xxx,yyy)
View Code

关键字参数调用,与形参顺序无关

技术分享
1 test(y=2,x=1)
View Code

 

python之路 - 基础3

标签:ogg   集合   ade   关系   bytes   val   分享   北京   关闭   

原文地址:http://www.cnblogs.com/qiang8216/p/6279031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!