python随机生成电话号码
import random
number = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '187', '188', '130','131', '132', '155', '156', '185', '186', '133', '153', '180', '189']
num = random.choice(number)
nums = ''.join(str(i) for i in random.sample(range(0, 10), 8))
print(num+nums)
import random
def mobile_phone():
phone="1"+random.choice(["3","5","7","8","9"])
for i in range(9):
num=random.randint(0,9)
phone+=str(num)
return phone
if __name__ == '__main__':
print(mobile_phone())
random.choice()返回一个列表,元组或字符串的随机项
sample(seq, n) 从序列seq中选择n个随机且独立的元素
range()生成的是整型,所以需要将整型元素转化成str类型,才能调用join函数
join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串