1 函数 2 1.def 3 2.名字 4 3.函数体 5 4.返回值 6 5.参数 7 普通参数 8 指定参数 9 默认参数 10 动态参数 11 *ages 12 **kwargs 13 万能参数 14 *args **kwargs 15 6.补充: 16 a. 17 def f1 18 def f1 19 b.引用 20 c.全局变量 21 读,均可读 22 赋值,global 23 字典,列表,可修改 24 ==> 全局变量用大写 25 #1.set 无序,不重复,可嵌套 26 #函数 def 名字 函数体 返回值 >>定义函数不执行 27 #def sendmail(xxoo):形式参数 28 #ret = sendmail('liangml')实际参数 29 # x='ok'默认参数 30 #send(x='liangml',xxoo='123')制定参数 31 函数的参数 32 #1.普通参数 33 #2.默认参数(默认参数必须放到参数列表的后边) 34 #3.指定参数 35 #4.*接受人任意实际参数 36 #5.*动态参数 37 # * 默认将传入的参数,全部放置在元组中,f1(*[11,22,33,44]) 38 # ** 默认将传入的参数,全部放置在字典中,f1(**{'k1':'v1','k2':'v2'}) 39 #6.万能参数 *args **kwargs 40 # str.format() 41 # str format()#格式化输出 42 #%s %d 43 # s1 = "I am {0},age {1}".format("liangml",18) 44 # print(s1) 45 # s2 = "I am {0},age {1}".format(*["liangml",18]) 46 # print(s2) 47 # s1 = "I am {name},age {age}".format(name='liangml',age='18') 48 # print(s1) 49 # dic = {'name':'liangml','age':"18"} 50 # s2 = 'I am {name},age {age}'.format(**dic) 51 # print(s2) 52 def sendmail(): 53 import smtplib 54 from email.mime.text import MIMEText 55 from email.utils import formataddr 56 57 msg = MIMEText('邮件内容','plain','utf-8') 58 msg['From'] = formataddr(['名字','liangml0538@163.com']) 59 msg['To'] = formataddr(["走人",'17295621220@qq.com']) 60 msg['Subject'] = '主题' 61 62 server = smtplib.SMTP('smtp.163.com',25) 63 server.login('liangml0538@163.com','邮箱密码') 64 server.sendmail('liangml0538@163.com',['172951020@qq.com'],msg.as_string()) 65 server.quit() 66 67 sendmail() 68 优化后 69 def sendmail(): 70 try: 71 import smtplib 72 from email.mime.text import MIMEText 73 from email.utils import formataddr 74 msg = MIMEText('邮件内容','plain','utf-8') 75 msg['From'] = formataddr(['名字','liangml0538@163.com']) 76 msg['To'] = formataddr(["走人",'17295622120@qq.com']) 77 msg['Subject'] = '主题' 78 server = smtplib.SMTP('smtp.163.com',25) 79 server.login('liangml0538@163.com','邮箱密码') 80 server.sendmail('liangml0538@163.com',['1729512020@qq.com'],msg.as_string()) 81 server.quit() 82 except: 83 #发送失败执行 84 return False 85 else: 86 #发送成功 87 return True 88 ret = sendmail() 89 print(ret) 90 if ret == True: 91 print('发送成功') 92 else: 93 print('发送失败') 94 95 return讲解,如果没有return python回自动return一个值None 96 def f1(): 97 print(123) 98 #在函数中,一旦执行return,函数执行过程立即终止 99 return '111' 100 print(456) 101 r = f1() 102 print(r) 103 104 再次优化 105 def sendmail(xxoo): 106 try: 107 import smtplib 108 from email.mime.text import MIMEText 109 from email.utils import formataddr 110 msg = MIMEText('邮件内容','plain','utf-8') 111 msg['From'] = formataddr(['名字','liangml0538@163.com']) 112 msg['To'] = formataddr(["走人",'1721262020@qq.com']) 113 msg['Subject'] = '主题' 114 server = smtplib.SMTP('smtp.163.com',25) 115 server.login('liangml0538@163.com','邮箱密码') 116 server.sendmail('liangml0538@163.com',['xxoo'],msg.as_string()) 117 server.quit() 118 except: 119 #发送失败执行 120 return False 121 else: 122 #发送成功 123 return 'cc' 124 125 while True: 126 em = input("请输入邮箱地址:") 127 result = sendmail(em) 128 if result == "cc": 129 print("发送成功") 130 else: 131 print("发送失败") 132 133 补充 134 # def f1(a1,a2): 135 # return a1 + a2 136 # def f1(a1,a2): 137 # return a1 * a2 138 # ret = f1(8,8) 139 # print(ret) 140 141 # def f1(a1): 142 # a1.append(999) 143 # li = [11,22,33,44] 144 # f1(li) 145 # print(li) 146 #当函数传送元素时使用的索引 147 148 #全局变量,所有的作用域都可读 149 #对全局变量进行重新赋值,需要global 150 #特殊的,列表字典,可修改,不可重新赋值 151 #定义全局变量的时候全部都是大写 152 # def f1(): 153 # name = 'liangml' 154 # print(name) 155 # def f2(): 156 # print(name) 157 #name = 'liangml'#全局变量 158 NAME = [11,22,33,44] 159 def f1(): 160 # age = 18 161 # global name #表示,name是全局变量 162 # name = '123' 163 print(NAME) 164 name.append(32) 165 print(age,NAME) 166 f1() 167 def f2(): 168 age = 19 169 print(age,NAME) 170 f2()