1、普通参数(严格按照顺序,将实际参数赋值给形式参数)
2、默认参数(必须放置在参数列表的最后)
3、指定参数(将实际参数赋值给制定的形式参数)
4、动态参数:
* 默认将传入的参数,全部放置在元组中, f1(*[1`1,22,33,44])
** 默认将传入的参数,全部放置在字典中 f1(**{"kl":"v1", "k2":"v2"})
5、万能参数, *args,**kwargs
------------------------------------------------------------------------------
def sendmail():
try:
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
msg = MIMEText('邮件内容', 'plain', 'utf-8')
msg['From'] = formataddr(["武沛齐",'登录邮箱帐号'])
msg['To'] = formataddr(["走人",'接收邮箱帐号’])
msg['Subject'] = "主题"
server = smtplib.SMTP("smtp.126.com", 25)
server.login("登录邮箱帐号", "邮箱密码")
server.sendmail('登录邮箱帐号', ['接收邮箱帐号',], msg.as_string())
server.quit()
except:
发送失败
return "失败"
else:
发送成功
return "cc"
ret = sendmail()
print(ret)
if ret == "cc":
print('发送成功')
else:
print("发送失败")
print(123) #发送成功会提示发送成功,否则会提示发送失败
------------------------------------------------------------------------
def f1():
print(123)
在函数中,一旦执行return,函数执行过程立即终止
return "111"
print(456)
r = f1()
print(r) #将不打印456
------------------------------------------------------------------------
def f2():
print(123)
r = f2()
print(r)
------------------------------------------------------------------------
def sendmail(xxoo, content): #xxoo content 为形式参数
#xxoo = a
try:
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
msg = MIMEText(content, 'plain', 'utf-8')
msg['From'] = formataddr(["武沛齐",'同实例1'])
msg['To'] = formataddr(["走人",'同上'])
msg['Subject'] = "主题"
server = smtplib.SMTP("smtp.126.com", 25)
server.login("同上", "同上")
server.sendmail('同上', [xxoo,], msg.as_string())
server.quit()
except:
# 发送失败
return "失败"
else:
# 发送成功
return "cc"
while True:
em = input("请输入邮箱地址:")
result = sendmail(em, "SB") #实际参数 em SB
if result == "cc":
print("发送成功")
else:
print("发送失败")
-------------------------------------------------------------------------
def send(xxoo, content, xx="OK"): #形式参数 默认参数 xx 默认值为OK(默认参数必须放在最后)
print(xxoo, content, xx)
print("发送邮件成功:", xxoo, content)
return True
while True:
em = input("请输入邮箱地址:")
result = send(em, "SB", "ok") #实际参数 第一个实际参数对应第一个形式参数,第二个实际参数对应第二个形式参数,.........
if result == True: #默认参数如无,则为默认值,否则对应实际参数传入值
print("发送成功")
else:
print("发送失败")
---------------------------------------------------------------------------
def send(xxoo, content, xx="OK"): 同上例
print(xxoo, content, xx)
# print("发送邮件成功:", xxoo, content)
return True
send('a', "sb")
send('a', "sb", "BB")
---------------------------------------------------------------------------
def send(xxoo, content):
print(xxoo, content)
# print("发送邮件成功:", xxoo, content)
return True
send(content="a", xxoo="sb") #指定参数值,参数看以不按照顺序写
-----------------------------------------------------------------------------
| 1、普通参数(严格按照顺序,将实际参数赋值给形式参数)
| 2、默认参数(必须放置在参数列表的最后)
| 3、指定参数(将实际参数赋值给制定的形式参数)
| 4、动态参数:
| * 默认将传入的参数,全部放置在元组中, f1(*[1`1,22,33,44])
| ** 默认将传入的参数,全部放置在字典中 f1(**{"kl":"v1", "k2":"v2"})
| 5、万能参数, *args,**kwargs
------------------------------------------------------------------------------
str.format()
str format格式化输出
"%s %d"
---------------------------------------------------------------
s1 = "i am {0}, age {1}".format("alex", 18)
print(s1)
---------------------------------------------- -----------------
s2 = "i am {0}, age {1}".format(*["alex", 18])
print(s2)
----------------------------------------------------------------
s1 = "i am {name}, age {age}".format(name='alex', age=18)
print(s1)
----------------------------------------------------------------
dic = {'name': 'alex', "age": 18}
s2 = "i am {name}, age {age}".format(**dic)
print(s2)
-----------------------------------------------------------------
def f1(*args, **kwargs): 万能参数
print(args)
print(kwargs)
f1(k1="v1")
-----------------------------------------------------------------
def f1(*args): #适用于元组,列表等
# args = (11,)
# args = ([11,22,"alex", "hhhh"],"12")
print(args, type(args))
f1(11,22,33,44) #分别转换为元组中一个元素
li = [11,22,33,44]
f1(li) #列表所有元素转换为元组中一个元素
f1(*li) #列表中每个元素转换为元组中一个元素
-------------------------------------------------------------------
def f1(**args): #适用于字典
# args = (11,)
# args = ([11,22,"alex", "hhhh"],"12")
print(args, type(args))
f1(n1="a", n2=18) #转换为字典 k1=n1,v1=a k2=n2 v2=18
dic = {'k1': "v1", "k2":"v2"}
f1(kk=dic) #转换为字典 keys为kk values为字典dic
--------------------------------------------
dic = {'k1': "v1", "k2":"v2"}
f1(**dic) #转换为字典
{'k2': 'v2', 'k1': 'v1'}
--------------------------------------------
li = [11,22,"a", "hhhh"]
f1(li, '12')
#结果([11,22,"a","hhhh"],12)
li = [11,22,"a", "hhhh"]
f1(li) #结果 ([11,22,"a","hhhh"])
f1(*li) #结果 (11,22,"a","hhhh")
li = "abcdefghigk"
f1(*li) #结果 ("a","b","c","d","e".....)
------------------------------------------------------------------------------
def send(xxoo, xx, content="OK", oo="xxx"):
print(xxoo, content, xx)
# print("发送邮件成功:", xxoo, content)
return True
send('alex', "sb")
send('alex', "sb", "BB")
def f1(a1, a2):
return a1 + a2
def f1(a1, a2):
return a1 * a2
ret = f1(8, 8)
print(ret) #为64 f1第一次指向a1+a2 变化之后指向为a1*a2
*********************************
name = "aasd"
name = "eric"
print(name)
--------------------------------------------------------------------------------
def f1(a1):
a1.append(999) #指向列表li,是索引;而非又复制了一份列表li
li = [11,22,33,44]
f1(li)
print(li)
[11,22,33,44,999]
---------------------------------------------------------------------------------
**********
* 全局变量*
**********
def f1():
name = "a"
print(name)
def f2():
print(name)
# 全局变量,所有作用域都可读
# 对全局变量进行【重新赋值】,需要global
# 特殊:列表字典,可修改,不可重新赋值
# 全局变量
NAME = None
def f1():
age = 18
global NAME # 表示,name是全局变量
# NAME = "123"
print(age, NAME)
def f2():
age = 19
print(age, NAME)
f1()
f2()
---------------------------------------------------------------------------------
**********************************************
# 三元运算,三目运算,if else简写
if 1 == 1:
name = "a"
else:
name = "SB"
#如果 1==1 成立,name = "a",否则name = "SB"
**********************************************
name = "a" if 1 == 1 else "SB"
等于上面的if,else
**********************************************
def f1(a1):
return a1 + 100
lambda函数: #在这里f1和f2是一样的
f2 = lambda a1, a2=9: a1 + a2 + 100
ret = f1(10)
print(ret)
r2 = f2(9)
print(r2)
-----------------------------------------------------------------------
函数实例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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 line_list[0] == username and line_list[1] == password:
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()
def main():
t = input("1:登录;2:注册")
if t == "1":
user = input("请输入用户名:")
pwd = input("请输入密码:")
r = login(user, pwd)
if r:
print("登录成功")
else:
print("登录失败")
elif t == "2":
user = input("请输入用户名:")
pwd = input("请输入密码:")
register(user, pwd)
main()
----------------------------------------------------------------------