函数
一、 邮箱发送程序
def sendmail(sendman, content, topic):
try:
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
msg = MIMEText(content, "plain", "UTF-8")
msg["From"] = formataddr(["dsj", "deng_shj@hisuntech.com"])
# msg["To"]=formataddr(["dsj","deng_shj@hisuntech.com"])
msg["Subject"] = topic
server = smtplib.SMTP("smtp.hisuntech.com", 25)
server.login("deng_shj@hisuntech.com", "Twoman89837475")
server.sendmail("deng_shj@hisuntech.com", [sendman, ], msg.as_string())
server.quit()
except:
return "lost"
else:
return "good"
while True:
em = input("pls input mailbox name: ")
ct = input("pls input the content of the mail:")
topic=input("pls input the topic:")
if sendmail(em, ct,topic) == "lost":
print("发送失败")
break
else:
print("发送成功")
二、动态参数
def fun(*args1): #等同于传元组 print(args1,type(args1)) fun([1,23,4,5,]) # 输出 ([1, 23, 4, 5],) <class 'tuple'> fun("test") # 输出('test',) <class 'tuple'> def fun2(**args): #等同于传字典 print(args,type(args)) fun2(姓名="ddd",年龄="30") # 输出{'姓名': 'ddd', '年龄': '30'} <class 'dict'>
def fun(*args):
print(args,type(args))
fun(["alex",22])
# 输出(['alex', 22],) <class 'tuple'>
fun(*["alex",22])
# 输出('alex', 22) <class 'tuple'>
三、占位符
s="i am {0} , age {1}".format("alex",30) print(s)
四、动态函数举例说明:
s1="i am {0} , age {1}".format("alex",30) print(s1) s2="i am {0} , age {1}".format(*["alex",30]) print(s2) s3="i am {name}, age{age}".format(name='alex',age='30') print(s3) dic={"name":"alex","age":30} s4="i am {name}, age{age}".format(**dic) print(s4)
# 以上全部输出 i am alex, age30
五、传值还是传引用
# 内存指向 def f1(a1,a2): return a1+a2 def f1(a1,a2): return a1*a2 ret= f1(8,8) print(ret) # 传值还是传引用 def f2(a1): a1.append(999) li=[11,22,33,44] f2(li) print(li) # 传递引用
浙公网安备 33010602011771号