python学习笔记Day4
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: lizhenyang_2008@163.com
# 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(['王盼','wangpan0810@126.com'])
msg['To'] = formataddr(['王盼','1471333927@qq.com'])
msg['Subject'] = '主题'
server = smtplib.SMTP('smtp.126.com',25)
server.login('wangpan0810@126.com','密码')
server.sendmail('wangpan0810@126.com',[user,],msg.as_string())
server.quit()
except Exception:
ret = 'false'
return ret
ret = mail('1471333927@qq.com')
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