day3 ord,chr,random,string
day3复习
>>> for i in range(10):
... if i == 3:
... break
... print(i)
...
0
1
2
>>> for i in range(10):
... if i == 3:
... continue
... print(i)
...
0
1
2
4
>>> while True:
... i = int(input("请输入一个数字:"))
... if i%2 == 0:
... print("偶数")
... else:
... print("奇数")
... if i == 100:
... break
>>> ord("杜")
26460
>>> chr(26460)
'杜'
>>> d = "杜崇崇"
>>> type(d)
<class 'str'>
>>> d.encode("gbk")
b'\xb6\xc5\xb3\xe7\xb3\xe7'
>>> d.encode("utf8")
b'\xe6\x9d\x9c\xe5\xb4\x87\xe5\xb4\x87'
>>> type(d.encode("gbk"))
<class 'bytes'>
>>> type(d.encode("gbk").decode("gbk"))
<class 'str'>
unicode 不能直接写入文件也不能直接在网络传输,必须是bytes
练习1:
>>> a = "测试"
>>> type(a)
<class 'str'>
>>> a.encode("gbk")
b'\xb2\xe2\xca\xd4'
>>> type(a.encode("gbk"))
<class 'bytes'>
>>> type(a.encode("gbk").decode("gbk"))
<class 'str'>
encode 将str(unicode) 转为bytes类型
decode 将bytes类型转为str(unicode)类型
import random
a = [1,2,4,"as","ba"]
random.random() # 随机小数
100+random.random() # 整数+小数
random.randint(1,100) # 随机1,100之间的整数
random.choice() # 随机一个列表里面的值
random.uniform(1,100) # 随机1,100之间的小数点数
random.shuffle(a) # 随机对列表的顺序进行打乱
练习2:随机生成一个小写字母
chr(random.randint(97,123))
chr(97+random.randint(1,25))
chr(ord("A")+random.randint(1,25))
生成随机的10位小写字母:
s = ""
for i in range(10):
... ss = chr(ord("a")+random.randint(1,25))
... s+=ss
生成随机的10位小写字母:
for i in range(10):
... ss = chr(ord("A")+random.randint(1,25))
... s+=ss
生成随机的5个小写字母和5个大写字母:
>>> for i in range(5):
... s+= chr(ord("a")+random.randint(1,25))
...
>>> for i in range(5):
... s+= chr(ord("A")+random.randint(1,25))
生成随机不限定固定大小写个数的10个字母:
lower_num = random.randint(1,9)
uper_num = 10-lower_num
for i in range(lower_num):
s+= chr(ord("a")+random.randint(1,25))
for i in range(uper_num ):
s+= chr(ord("A")+random.randint(1,25))
>>> lower_case = string.ascii_lowercase
>>> lower_case
'abcdefghijklmnopqrstuvwxyz'
>>> low_list = list(lower_case)
>>> low_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> low_list_s = low_list[:10]
>>> low_list_s
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
>>> "".join(low_list_s)
'abcdefghij'
>>>
>>> letters = string.ascii_letters
>>> letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> letters_list = list(letters)
>>> random.shuffle(letters_list)
>>> letters_list
['Q', 'Y', 'V', 'q', 'g', 'p', 'K', 'a', 'T', 'X', 's', 'U', 'H', 'N', 't', 'i', 'b', 'n', 'r', 'h', 'G', 'f', 'd', 'O', 'j', 'A', 'P', 'F', 'R', 'l', 'x', 'z', 'J', 'c', 'E', 'e', 'k', 'o', 'Z', 'D', 'y', 'M', 'I', 'C', 'W', 'w', 'S', 'L', 'v', 'B', 'u', 'm']
>>> "".join(letters_list[] )
KeyboardInterrupt
>>> "".join(letters_list[:10])
'QYVqgpKaTX'
>>> [random.choice(list(string.ascii_letters)) for i in range(1,9)]
['Y', 'g', 'y', 'j', 'G', 'K', 'I', 's']
>>> [random.choice(list(string.ascii_letters)) for i in range(10)]
['M', 'q', 'y', 'x', 'I', 'F', 'I', 'W', 'm', 's']
>>> "".join([random.choice(list(string.ascii_letters)) for i in range(10)])
'RZqoHNduTe'
学习最大的乐趣在于分享,我是绝世老中医,欢迎来诊
个人qq:1978529954