标签:
#方式1: se = {‘123‘,‘456‘} #与字典类似dict1 = {‘k1‘,‘v1‘,‘k2‘,‘v2‘} #方式2: se = set() #创建一个空的集合 list1 = [11,22,33,44] se = set(list1) print(se)
s = set() print(s) s.add(123) #添加集合元素 s.add(123) s.add(123) #体现集合不重复性,添加多个但是集合中只有一个123 print(s) s.clear() #clear方法是清空s集合元素 print(s) set() {123} set() #找不同 s1 = {11,22,33} s2 = {22,33,44} s3 = s1.difference(s2) #A中存在,B中不存在,例子中为存在s1中不存在s2中的集合 s4 = s2.difference(s1) #s2中存在s1中不存在的集合 s5 = s1.symmetric_difference(s2) #A中存在B中不存在并且B中存在A中不存在的集合 print(s1) #{11,22,33} print(s2) #{22,33,44} print(s3) #{11} print(s4) #{44} print(s5) #{11, 44} s1.difference_update(s2) #将s1中存在s2种不存在的元素更新覆盖到s1集合中 print(s1) #{11} s1.symmetric_difference_update(s2) #将s1中存在s2中不存在并且s2中存在s1中不存在的元素更新追加到s1集合中 print(s1) #{33, 11, 44, 22} #删除元素的几种方法 s1 = {11,22,33} s1.discard(1111)#移除1111的元素,不存的元素不报错 s1.remove(11111)#不存在的元素报错 s1.pop() #随机删除元素 ret = s1.pop() #取随机移除的元素 print(s1) print(ret) #并集和交集 s1 = {11,22,33} s2 = {22,33,44} print(s1,s2) #{33, 11, 22} {33, 44, 22} s3 = s1.union(s2) #取s1和s2的并集 print(s3) #{33, 22, 11, 44} s4 = s1.intersection(s2) #s1和s2的交集 print(s4) #{33, 22} s1.intersection_update(s2) #将s1和s2的交集结果写入覆盖到s1中 print(s1) #{33, 22} #追加方法 s1 = {11,22,33} s1.add(11) #add方法 s1.add(12) s1.add(13) #li = [11,22,3,11,2] #列表update进集合 #li = (11,22,3,11,2) #元组update进集合 #li = "alexalex" #字符串按照字符循环方式update到s1中 s1.update(li) #update方法 print(s1) #{33, ‘e‘, 11, 12, 13, ‘a‘, ‘l‘, ‘x‘, 22}
练习:
cmdb练习 #代表槽位,数字代表内存大小,由old状态变到new的状态
要删除哪些槽位
要更新哪些槽位
要增加哪些槽位
old_dic = { "#1":8, "#2":4, "#4":2, } new_dic = { "#1":4, "#2":4, "#3":2, }
具体操作过程
new_set = set(new_dict.keys()) old_set = set(old_dict.keys()) remove_set = old_set.difference(new_set) #删除的槽位 add_set = new_set.difference(old_set) #增加的槽位 update_set = old_set.intersection(new_set) #更新的槽位
发送邮件练习
#!/usr/bin/env python # -*- codind:utf-8 -*- #Author:ljb def sendmail(): try: import smtplib from email.mime.text import MIMEText from email.utils import formataddr msg = MIMEText(‘邮件内容‘, ‘plain‘, ‘utf-8‘) msg[‘From‘] = formataddr(["郎建彬",‘ljb880903@163.com‘]) msg[‘To‘] = formataddr(["走人",‘1037258416@qq.com‘]) msg[‘Subject‘] = "主题" server = smtplib.SMTP("smtp.163.com", 25) server.login("ljb880903@163.com", "密码") server.sendmail(‘ljb880903@163.com‘, [‘1037258416@qq.com‘,], msg.as_string()) server.quit() except: return ‘失败‘ else: return ‘cc‘ sendmail() ret = sendmail() print(ret) if ret == ‘cc‘: print(‘发送成功‘) else: print(‘发送失败‘)
参数练习
def send(addr,content,stat=‘ok‘):#形式参数,stat=ok 为默认参数 print(‘发送%s的内容%s结果为%s‘% (addr,content,stat)) return ‘ok‘ ret = send(‘ljb‘,‘test‘) #默认参数,实际参数,ljb和test为传过来的普通参数 ret = send(‘ljb‘,‘test‘,‘fail‘) ret = send(content=‘ljb‘,addr=‘test‘) #指定参数 if ret == ‘ok‘: print(‘发送成功‘) else: print(‘发送失败‘)
动态参数*练习
def f1(*args): print(args) li = [11,22,‘ljb‘,‘test‘,] f1(11,22,33,44) f1(li,12) f1(*li) ‘‘‘ (11, 22, 33, 44) #以元组方式打印出来 ([11, 22, ‘ljb‘, ‘test‘], 12) #传人列表的话将列表作为一个元素写入元组 (11, 22, ‘ljb‘, ‘test‘) ‘‘‘
万能参数练习
#万能参数 print(‘万能参数‘) def f1(*args,**kwargs): print(args) print(kwargs) f1(11,22,33,44,k1=‘v2‘,k2=‘v2‘) print(‘**参数‘) def f2(**args): print(args,type(args)) f2(n1 = ‘ljb‘,n2=18) dict = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} f2(k2=dict) f2(**dict) 万能参数 (11, 22, 33, 44) {‘k1‘: ‘v2‘, ‘k2‘: ‘v2‘} **参数 {‘n1‘: ‘ljb‘, ‘n2‘: 18} <class ‘dict‘> {‘k2‘: {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘}} <class ‘dict‘> {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘} <class ‘dict‘>
动态参数和万能参数的应用
str.format()格式化输出
s1 = ‘I am {0},age {1}‘.format(‘ljb‘,23) print(s1) s2 = ‘I am {0},age {1}‘.format(*[‘ljb‘,23]) print(s2) s3 = ‘I am {name},age {age}‘.format(name = ‘ljb‘,age = 23) print(s3) dic1 = {‘name‘:‘ljb‘,‘age‘:23} s4 = ‘I am {name},age {age}‘.format(**dic1) print(s4) I am ljb,age 23 I am ljb,age 23 I am ljb,age 23 I am ljb,age 23
补充相同函数程序会执行最下面的函数体
#定义2个一样的def ,以后面的def名称为准了 def f1 (a1,a2): return a1+a2 def f1 (a1,a2): return a1*a2 ret = f1(8,8) print(ret) 64
补充引用列表练习
def f1(a1): a1.append(999) #这里引用的是li列表的地址,所以li也会append 999 li = [11,22,33,44] f1(li) print(li) #[11, 22, 33, 44, 999]
补充全局变量练习
#特殊列表字典可修改不可重新赋值 def f1 (a1): a1.append(999) li = [11,22,33,44] f1(li) print(li) [11, 22, 33, 44, 999] #global +变量名 #表示变量为全局变量,全局变量赋值一律都用大写 NAME = ‘ljb‘ def f1(): age = 18 global NAME NAME = ‘123‘ print(NAME,age) def f2(): age = 19 print(NAME,age) f1() f2() 123 18 123 19
函数练习写登陆程序
#!/usr/bin/env python # -*- codind:utf-8 -*- #Author:ljb def login(username,password): ‘‘‘ 用于用户登录 :param username:登陆用户名 :param password:登陆密码 :return:true 登陆成功,false登录失败 ‘‘‘ f = open(‘db‘,‘r‘) for line in f: line_list = line.strip().split(‘|‘) if username == line_list[0] and password == line_list[1]: return True return False def register(username,password): ‘‘‘ 用于用户注册 :param username:注册用户名 :param password: 注册密码 :return:none ‘‘‘ f = open(‘db‘,‘a‘) temp = ‘\n‘ + username + ‘|‘+ password f.write(temp) f.close() return True def main(): t = input("1 登陆 2 注册") if t == ‘1‘: user = input(‘输入用户名:‘) passwd = input(‘输入密码:‘) r = login(user,passwd) if r : print(‘登陆成功‘) else: print(‘登陆失败‘) elif t == ‘2‘: user = input(‘输入用户名:‘) passwd = input(‘输入密码:‘) r1 = register(user,passwd) if r1 : print(‘注册成功‘) else: print(‘注册失败‘) main()
内置函数练习
# abs绝对值 # n = abs(-1) # print(n) # all() # any() # 0,None,"", [], () # print(bool(())) # 所有为真,才为真 # n = all([1,2,3,None]) # print(n) # 只要有真,就为真 # n = any([[],0,"",None]) # print(n) # ascii() # 自动执行对象的 __repr__ # class Foo: # def __repr__(self): # return "444" # # n = ascii(Foo()) # print(n) # bin() # oct() # hex() # print(bin(5)) # print(oct(9)) # print(hex(15)) """ # utf-8 一个汉字:三个字节 # gbk 一个汉字:二个字节 # utf-8 s = "李杰" # 一个字节8位,一个汉字三个字节 # 0101010 10101010 101010101 0101010 10101010 101010101 # 23 23 23 23 23 23 15 # 2f 2a 2c 2c 2e 2f f # 字符串转换字节类型 # bytes(只要转换的字符串, 按照什么编码) n = bytes("李杰", encoding="utf-8") print(n) n = bytes("李杰", encoding="gbk") print(n) # 字节转化成字符串 new_str = str(bytes("李杰", encoding="utf-8"), encoding="utf-8") """ # 1、打开文件 # f = open(‘db‘, ‘r‘) # 只读 # f = open(‘db‘, ‘w‘) # 只写,先清空原文件 # f = open(‘db‘, ‘x‘) # 文件存在,报错;不存在,创建并只写 # f = open(‘db‘, ‘a‘) # 追加 # f = open(‘db‘,‘r‘, encoding="utf-8") # data = f.read() # print(data, type(data)) # f.close() # f = open(‘db‘,‘r‘) # data = f.read() # print(data,type(data)) # f = open(‘db‘,‘rb‘) # data = f.read() # print(data,type(data)) # f = open("db", ‘a‘) # f.write("李杰") # f.close() # # f = open("db", ‘ab‘) # f.write(bytes("李杰", encoding="utf-8")) # f.close() # f = open("db", ‘r+‘, encoding="utf-8") # # f.fileno() # # 如果打开模式无 b,则read,按照字符读取 # data = f.read(1) # # tell当前指针所在的位置(字节) # print(f.tell()) # # 调整当前指着你的位置(字节) # f.seek(f.tell()) # # 当前指针位置开始向覆盖 # f.write("888") # f.close() # 2、操作文件 # read() # 无参数,读全部;有参数, # b,按字节 # 无b,按字符 # tell() 获取当前指针位置(字节) # seek(1) 指针跳转到指定位置(字节) # write() 写数据,b,字节;无b,字符 # close # fileno # flush 强刷 # readline 仅读取一行 # truncate 截断,指针为后的清空 # for循环文件对象 f = open(xxx) # for line in f: # print(line) # f = open("db", ‘r+‘, encoding="utf-8") # f.seek(3) # f.truncate() # f.close() # f = open("db", ‘r+‘, encoding="utf-8") # for line in f: # print(line) # 通过源码查看功能 # 3、关闭文件 # f.close() # with open(‘xb‘) as f: # pass with open(‘xb‘) as f: pass with open(‘db1‘, ‘r‘, encoding="utf-8") as f1, open("db2", ‘w‘,encoding="utf-8") as f2: for line in f1: if line == "xx": f2.write() f2.write() # new_str = line.replace("alex", ‘st‘) # f2.write(new_str) # f = open("db", ‘a‘,encoding="utf-8") # f.write("123") # f.flush() # input("asdfasd")
# 三元运算,三目运算,if else简写 """ if 1 == 1: name = "alex" else: name = "SB" # 如果 1==1 成立, # name = "alex" # 否则 # name = "SB" name = "alex" if 1 == 1 else "SB" """ def f1(a1): return a1 + 100 f2 = lambda a1, a2=9: a1 + a2 + 100 ret = f1(10) print(ret) r2 = f2(9) print(r2)
标签:
原文地址:http://www.cnblogs.com/Jabe/p/5520521.html