python學習筆記Day4

來源:互聯網
上載者:User

標籤:技術分享   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.