標籤:技術分享 upd 簡單函數 檔案 cti str net def class
1、set集合
set集合是一種無序且不重複的集合
添加功能:
s1 = set()
s1.add("wang")
print(s1)
difference功能(從當前集合裡找出不同的元素並產生一個新的集合):
s1 = set(["wang","pan","lai"])
s2 = s1.difference(["wang","pan"])
print(s2)
結果:{‘lai‘}
difference_update(從當前集合裡去掉重複的元素,不產生新的集合):
s1 = set(["wang","pan","lai"])
s1.difference_update(["pan","lai"])
print(s1)
結果:{‘wang‘}
1、隊列
單項隊列(queue):先進先出
雙向隊列(collections):兩邊都可以進和取
2、字典{}、元組()、列表[]
3、深copy(copy.deepcopy())和淺copy(copy.copy())
對於數字和字串來說,兩者的結果記憶體位址是一樣的
4、函數
函數裡的try語句:
# -*- coding:utf-8 -*-
# Author: cslzy
# Email: [email protected]
# Description:send something to someone.
# Date: 20151104 18:33:08
import smtplib
from email.mime.text import MIMEText as mt
def mail(user):
ret = ‘ture‘
try:
msg = MIMEText(‘你好!‘,‘plaon‘,‘utf-8‘)
msg[‘From‘] = formataddr([‘王盼‘,‘[email protected]‘])
msg[‘To‘] = formataddr([‘王盼‘,‘[email protected]‘])
msg[‘Subject‘] = ‘主題‘
server = smtplib.SMTP(‘smtp.126.com‘,25)
server.login(‘[email protected]‘,‘密碼‘)
server.sendmail(‘[email protected]‘,[user,],msg.as_string())
server.quit()
except Exception:
ret = ‘false‘
return ret
ret = mail(‘[email protected]‘)
print(ret)
預設參數必須寫在後面。
動態參數(*:元組,**字典):
def han(*arg,**karg):
print(arg,type(arg))
print(karg,type(karg))
han(7,89,3,62,n1=78,n2=88)
def han(*arg,**karg):
print(arg,type(arg))
print(karg,type(karg))
l1 = [7,89,3,62]
l2 = {‘n1‘:78,‘n2‘:88}
han(*l1,**l2)
#l1 = ‘{0} is {1}‘
l1 = ‘{name} is {role}‘
n1 = {‘name‘:‘ren‘,‘role‘:‘dashen‘}
#l2 = l1.format(‘ren‘,‘dashen‘)
#l2 = l1.format(name=‘ren‘,role=‘dashen‘)
l2 = l1.format(**n1)
print(l2)
簡單函數lambda運算式:
Python的編碼注釋# -*- coding:utf-8 -*-
如果要在python2的py檔案裡面寫中文,則必須要添加一行聲明檔案編碼的注釋,否則python2會預設使用ASCII編碼。
參考:http://blog.csdn.net/arbel/article/details/7957782
python內建函數:
http://www.runoob.com/python/python-built-in-functions.html
python學習筆記Day4