元类
元类的写法时固定的,必须继承自type
元类一般命名以MetaClass结尾
py基础总结练习
简单的图形打印
打印实心矩形
1 print('第一种')
2 print("* * * * *") # print("* " * 5)
3 print("* * * * *") # print("* " * 5)
4 print("* * * * *") # print("* " * 5)
5 print("* * * * *") # print("* " * 5)
6
7 print("第二种")
8 for i in range(0, 4): # for循环输出
9 print("* " * 5)
10
11 print("第三种")
12 for i in range(0, 4):
13 for j in range(5):
14 print("* ", end="") # 每打印一个*不换号
15 print("") # 打印一行后换行
打印空心矩形
1 print("第一种")
2 for i in range(0, 4):
3 if i == 0:
4 print("* " * 5)
5 elif i == 3:
6 print("* " * 5)
7 else:
8 print("* *")
9
10 print("第二种")
11 for i in range(0, 4):
12 if i == 0 or i == 3:
13 print("* " * 5)
14 else:
15 print("* *")
16 print("第三种")
17 for i in range(4):
18 if i == 0 or i == 3:
19 print("* " * 5)
20 else:
21 for j in range(5):
22 if j == 0 or j == 4:
23 print("* ", end="")
24 else:
25 print(" ", end="")
26 print()
打印三角形
1 print("第一种")
2 print("*")
3 print("* *")
4 print("* * *")
5 print("* * * *")
6 print("* * * * *")
7
8 print("第二种")
9 for i in range(0, 6):
10 for j in range(i):
11 print("* ", end="")
12 print()
13
14 print("第三种")
15 for j in range(0, 6):
16 print("* " * j)
打印空心三角形
1 print("第一种")
2 print("*")
3 print("* *")
4 print("* *")
5 print("* *")
6 print("* * * * *")
7
8 print("第二种")
9 for i in range(5):
10 for j in range(i + 1):
11 if i == 4:
12 print("* ", end="")
13 continue
14 if j == 0 or j == i:
15 print("* ", end="")
16 else:
17 print(" ", end="")
18 print()
打印倒三角
1 print("第一种")
2 print("* " * 5)
3 print("* " * 4)
4 print("* " * 3)
5 print("* " * 2)
6 print("*")
7
8 print("第二种")
9 for i in range(5, -1, -1):
10 print("* " * i)
打印空倒三角
1 print("第一种")
2 print("* * * * *")
3 print("* *")
4 print("* *")
5 print("* *")
6 print("*")
7
8 print("第二种")
9 for i in range(5):
10 for j in range(5 - i):
11 if i == 0:
12 print("* ", end="")
13 continue
14 if j == 0 or j == 5 - i - 1:
15 print("* ", end="")
16 else:
17 print(" ", end="")
18 print()
打印正三角形
1 print("第一种")
2 print(" * ")
3 print(" * * ")
4 print(" * * * ")
5 print(" * * * * ")
6 print(" * * * * * ")
7
8 print("第二种")
9 for i in range(0, 5):
10 for j in range(5 - i):
11 print(" ", end="")
12 for m in range(i + 1):
13 print("* ", end="")
14 print()
打印空心正三角
1 print("第一种")
2 print(" * ")
3 print(" * * ")
4 print(" * * ")
5 print(" * * ")
6 print(" * * * * * ")
7 print("第二种")
8 for i in range(0,6):
9 for j in range(0,6-i):
10 print(end=" ")
11 for n in range(6-i,6):
12 if i == 5 or n == 6-i or n ==5:
13 print("* ",end="")
14 else:
15 print(" ",end="")
16 print()
python数据类型内置函数
字符串相加
s1 = "123"
s2 = "456"
s3 = s1+s2
print(s3)
print(s3+ '789')
字符串当列表
s1 = "0123456"
print(s1[5])
print(s1[-1])
print(s1[1:4]) # 切片
字符串常用函数
1 # capitalize 首字母大写,其余小写 返回字符串
2 s = "i love python"
3 print(s.capitalize())
4 # title() 将每个单词的首字母变为大写 返回字符串
5 print(s.title())
6 # upper() 将所有字母变为大写字母 返回字符串
7 print(s.upper())
8 # swapcase() 大小写互换
9 a = "I Love Python"
10 print(a.swapcase())
11 # len 返回字符串长度
12 print(len(a))
13 # find() 查找指定字符串,找不到返回1- 第一次找到返回索引值
14 print(a.find('v',))
15 # index() 查找指定字符串,找不到报错
16 print(a.index("o"))
17 # isalpha() 检测字符串是否是英文字母组成,,注意:中文前后被英文包裹会被认为是英文
18 a = '小 gege'
19 b = 'smile哥ge'
20 print(a.isalpha())
21 print(b.isalpha())
22 # isalnum() 检测字符串是否由数字组成
23 print(a.isalnum())
24 c = '123456'
25 print(c.isalnum())
26 # split() 用制定的字符切割字符串
27 a = "小白兔/白又白/两只耳朵竖起来"
28 p = a.split('/')
29 print(a.split('/'))
30 # join() 将列表按照指定字符串连接 返回字符串
31 print("/".join(p))
32 # ljust() 指定字符串长度,内容靠左,不足的位置用指定字符串填充默认空格
33 # center() 内容居中
34 # rjust() 内容靠右
35 s = 'abc'
36 print(s.ljust(8)+'a')
37 # strip() 去掉两边指定字符串,默认空格
38 # lstri() 去掉左侧指定字符串
39 # rstri() 去掉右侧指定字符串
40 s = ' abc '
41 k = 'abcde'
42 print(s.split())
43 print(k.lstrip('a'))
44 print(k.rstrip('e'))
45 # maketrans() 生成用于字符串替换的映射表
46 # translate() 进行字符串替换
47 s = "今晚吃的麻辣烫,辣死了"
48 t = s.maketrans('麻辣烫', '酸辣粉')
49 print(s.translate(t))