元组
Python 的元组与列表类似,不同之处在于元组的元素不能修改。

元组使用小括号,列表使用方括号。

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可

#元组也是一个list,但是它的值不能改变
s=[1,2,3]  #list
s1=(1,2,3) #元组tuple
# tuple 只有两个方法一个是index,一个是count
mysql =(
'cuimeiping',
'c123456',
'192.168.1.10',
)
print(mysql[0])
# mysql.index()#取下标
# mysql.count()#取个数
#小括号不仅能定义元组还能提高运算优先级
#如果定义元组只有一个元素需要在后面加逗号
oracel =(123,)
print(type(oracel))
集合
集合(set)是一个无序的不重复元素序列。

可以使用大括号{ }或者set()函数创建集合,注意:创建一个空集合必须用set()而不是{ },因为{ }是用来创建一个空字典

定义一个空集合set()

#集合天生就可以去重
list=[1,1,2,3,4,4]
lest =set(list)
print(lest)
alist={'a','b','c'}
blist={'b','e','a'}
#交集
res1=alist.intersection(blist)
print(res1)
res2=alist &blist
print(res2)
#并集,把两个集合合并到一起,然后去除重复
aes1=alist.union(blist)
print(aes1)
aes2=alist|blist
print(aes2)
#差集:在前面一个有,在后面一个没有
bes1 =alist-blist
print(bes1)
bes2=blist.difference(alist)
print(bes2)
#对称差集 只在一个集合里面出现过的找出来
ces1=alist.symmetric_difference(blist)
print(ces1)
ces2=alist^blist
print(ces2)
#怎么给集合增加元素
alist.add('ebc')#add增加一个元素
print(alist)
alist.pop()#随机删除一个元素
print(alist)
alist.remove('c')#指定一个元素删除
print(alist)
#集合也可以循环
for ebc in alist:
print(alist)

import string
print(string.ascii_uppercase)#所有A-Z的大写字母
print(string.ascii_lowercase)#所有a-z的小写字母
print(string.digits)#取0-9的数字
print(string.ascii_letters)#取a-z的大小写字母
print(string.punctuation)#取所有特殊字符
import random
print(random.randint(1,223))#指定一个范围产生一个整数
l =[1,2,3,4]
s ='abcdefg'
print(random.choice(s))#随机选择一个元素
print(random.sample(s,3))#随机选择几个元素
random.shuffle(l)#只能传list,用来打乱顺序,这个方法没有返回值
print(l)
print(random.uniform(1,19))#指定一个范围,取小数
f =random.uniform(1,19)#取随机的几位小数
newf = round(f,3) #保留几位小数用round
print(newf)
函数
#实现某个功能的一些代码,函数的作用是提高代码的复用性
# 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
# 函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。
def hello():#定义一个函数
print('hello')
#函数必须被调用才会执行
#调用函数
hello()
def weijing(fill_name,count):#定义函数,传函数入参
# 形参,形式参数
with open('a.txt','a+',encoding='utf-8') as fw:
fw.write(count)
# print(fill_name,count)
weijing('a.txt','123')#调用
#实参,实际参数
def readfill(fill_name):
with open(fill_name, 'a+', encoding='utf-8') as fw:
fw.seek(0)
count=fw.read()
return count#return返回,函数里面如果不写return默认返回none
#在函数里面代码是函数体
res=readfill('a.txt')
print(res)
#函数定义的变量都是局部变量,只要一出了函数都不能用了
示例1:通过手机号取钱
def get_mony(phon):#需要实现这个功能的时候需要给什么信息,给的信息就是入参    #出参,就是调用完接口后,返回给你的信息
info ={
'123456':100,
'133009':200
}
mony=info.get(phon)
return mony
user1=get_mony('123456')
print(user1)
user2=get_mony('133009')
print(user2)
练习:
1、写个一函数,这个函数的功能是,传入一个数字,产生N条手机号,产生的手机号不能重复。
[150,189,188,170,132,150,186]
def phone(500):
phone.txt
1861232323
23423423
import random
#方法一:
def phone(num):
# print(type(all_phone))
start_s=['150','130','158']
s='012345789'
all_phone = set()
# print(len(all_phone))
num=int(num)
while len(all_phone) !=num:
end = ''.join(random.sample(s,8))
all_phone.add(random.choice(start_s)+end+'
')
with open('phone1.txt', 'w', encoding='utf-8') as fw:
fw.writelines(all_phone)
return all_phone
res=input('请输入你想产生的条数')
res1=phone(res)
print(res1)
#方法二:
def phone(count):
count=int(count)
dict=set()
while len(dict) !=count:
start = ['150','130', '158', '138']
res_start = random.choice(start)
end = random.randint(0, 99999999)
res = '%s%08d
'%(res_start, end)
dict.add(res)
with open('hh', 'a+', encoding='utf-8')as fr:
fr.writelines(dict)
return dict
res=input('请输入你想产生的条数')
res1=phone(res)
print(res1)

2、写一个函数,这个函数的功能是,传入一个数字,产生N条邮箱,产生的手机号不能重复。
邮箱前面的长度是6-12之间,产生的邮箱必须包含大写字母、小写字母、数字和特殊字符
1、交集
s='abc123'
abc123@163.com
abcdef12@sina.com
[163.com,qq.com,sina.com,126.com]

import string
#方法一:
def email(num):
emails=set()
num=int(num)
while len(emails) !=num:
email_len = random.randint(6, 12)
email_end = ['163.com', 'qq.com', 'sina.com', '126.com']
endemail = random.choice(email_end)
upper = random.choice(string.ascii_uppercase)
lower = random.choice(string.ascii_lowercase)
digit = random.choice(string.digits)
punctu = random.choice(string.punctuation)
start = upper + lower + digit + punctu
other = email_len - 4
other_email = random.sample(string.punctuation + string.digits + string.ascii_lowercase + string.ascii_uppercase, other)
end_email = other_email + list(start)
email=''.join(end_email)+endemail+'
'
emails.add(email)
with open('ii','a+',encoding='utf-8') as fg:
fg.writelines(emails)
return emails
res=input('请输入你想产生的邮箱条数')
res1=email(res)
print(res1)
# 方法二:
def email(count):
count=int(count)
dict=set()
email_end = ['163.com', 'qq.com', 'sina.com', '126.com']
upper = random.choice(string.ascii_uppercase)
lower = random.choice(string.ascii_lowercase)
digit = random.choice(string.digits)
punctu = string.punctuation
for i in range(count):
res=random.randint(6,12)
emailend=random.choice(email_end)
res1=random.sample(upper,1)
res2=random.sample(lower,1)
res3=random.sample(digit,1)
other=res-3
res4=random.sample(punctu,other)
otheremail=res1+res2+res3+res4
emailoo=''.join(otheremail)
allemail=emailoo+emailend+'
'
dict.add(allemail)
print(dict)
with open('gg','a+',encoding='utf-8') as fo:
fo.writelines(dict)
return dict
res=input('请输入你想产生的邮箱条数')
res1=email(res)
print(res1)

#方法三:取交集判断是否包含大写、小写、数字、特殊字符
def email(suzi):
count=int(suzi)
if count<1:
print('count不能为负数')
return #函数里面遇到return会结束函数
dict=set()
email_end = ['163.com', 'qq.com', 'sina.com', '126.com']
endemail = random.choice(email_end)
while len(dict) !=suzi:
res = random.randint(6, 12)
res1=random.sample(string.ascii_letters+string.digits+string.punctuation,res)
if set(res1)&set(string.ascii_uppercase) and set(res1)&set(string.ascii_letters) and set(res1)&set(string.digits) and set(res1)&set(string.punctuation):
emails=''.join(res1)+endemail+'
'
dict.add(emails)
with open('rr','a+',encoding='utf-8') as fo:
fo.writelines(dict)
return dict
res=input('请输入你想产生的邮箱条数')
res1=email(res)
print(res1)