set 集合 和 函数

基本数据类型补充

set

set集合,是一个无序且不重复的元素集合

a.创建两种方式

       列表创建   1.  list = ['123','23']     2.  list()  执行 list _init_,内部执行for循环

        set创建  1. s1= {'123','45'}          2.  s2 = set() #空集合 s3 = set([123,123,12,])

set内置函数

      add (  )    添加元素

      clear() 清除内容

      操作集合        

>>> s = set()
>>> print(s)
set()
>>> s.add(123)
>>> s.add(123)
>>> s.add(123)
>>> print(s)
{123}
>>> s.clear()
>>> print(s)

        difference ()   A中存在,B中不存在

>>> s1 = {11,22,33}
>>> s2 = {22,33,44}
>>> s3 = s1.difference(s2)
>>> print (s3)
{11}
A中存在,B中不存在

         symmetric_difference()对称差集

>>> s1 = {11,22,33}
>>> s2 = {22,33,44}
>>> s3 = s1.symmetric_difference(s2)
>>> print (s3)
{11, 44}

          difference_update ()从当前集合中删除和B中相同的元素

>>> s1 = {11,22,33}
>>> s2 = {22,33,44}
>>> s1.difference_update(s2)
>>> print (s1)
{11}

           symmetric_difference_update()对称差集,并更新到A中

>>> s1 = {11,22,33}
>>> s2 = {22,33,44}
>>> s1.symmetric_difference_update(s2)
>>> print (s1)
{11, 44}

            discard () 移除指定的元素,不存在不保错

            pop () 移除元素

            remove ()移除元素,不存在报错

>>> s1 = {11,22,33}
>>> s1.discard(6666)
>>> s1.remove(66)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 66
>>> s1.remove(11)
>>> print (s1)
{33, 22}
>>> ret = s1.pop()
>>> print (ret)
33

             intersection () 交集

             intersection_update()取交集并更新到A中

              union ()并集    

>>> s1 = {1,2,3}
>>> s2 = {2,3,6}
>>> s3 = s1.union(s2)
>>> print (s3)
{1, 2, 3, 6}
>>> s3 = s1.intersection(s2)
>>> print (s3)
{2, 3}
>>> s1.intersection_update(s2)
>>> print (s1)
{2, 3}

        isdisjoint() 如果没有并集,返回True,否则返回False

        issubset () 是否是子序列

        issuperset()是否是父序列

练习

 

自定义函数

函数式编程和面向过程编程的区别:

  • 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
  • 面向对象:对函数进行分类和封装,让开发“更快更好更强...”

函数式编程最重要的是增强代码的重用性和可读性

格式:


def 函数名(参数):

    ...

    函数体

    ...

    返回值

函数的定义主要有如下要点:

  • def:表示函数的关键字
  • 函数名:函数的名称,日后根据函数名调用函数
  • 函数体:函数中进行一系列的逻辑计算,如:发送邮件等..
  • 参数:为函数体提供数据
  • 返回值:当函数执行完毕后,可以给调用者返回数据。

函数的有三中不同的参数:

1、普通参数(严格按照顺序,将实际参数赋值给形式参数)

2、默认参数(必须放置在参数列表的最后)

3、指定参数(将实际参数赋值给制定的形式参数)

4、动态参数:        

                    *     默认将传入的参数,全部放置在元组中, f1(*[1`1,22,33,44])      

                     **     默认将传入的参数,全部放置在字典中   f1(**{"kl":"v1", "k2":"v2"})

5、万能参数,   *args,**kwargs 

       普通参数

# ######### 定义函数 ######### 

# name 叫做函数func的形式参数,简称:形参
def func(name):
    print name

# ######### 执行函数 ######### 
#  'wupeiqi' 叫做函数func的实际参数,简称:实参
func('wupeiqi')

       默认参数

def func(name, age = 18):
    
    print "%s:%s" %(name,age)

# 指定参数
func('wupeiqi', 19)
# 使用默认参数
func('alex')

注:默认参数需要放在参数列表最后

        动态参数

def func(*args):

    print args


# 执行方式一
func(11,33,4,4454,5)

# 执行方式二
li = [11,2,2,3,3,4,54]
func(*li)

        万能参数 

def func(**kwargs):

    print args


# 执行方式一
func(name='wupeiqi',age=18)

# 执行方式二
li = {'name':'wupeiqi', age:18, 'gender':'male'}
func(**li)

发送邮件实例

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
  
  
msg = MIMEText('邮件内容', 'plain', 'utf-8')
msg['From'] = formataddr(["武沛齐",'wptawy@126.com'])
msg['To'] = formataddr(["走人",'424662508@qq.com'])
msg['Subject'] = "主题"
  
server = smtplib.SMTP("smtp.126.com", 25)
server.login("wptawy@126.com", "邮箱密码")
server.sendmail('wptawy@126.com', ['424662508@qq.com',], msg.as_string())
server.quit()
str.format()
str format格式化输出
>>> s1 = 'i am {0},age{1}'.format('alex',32)
>>> print (s1)
i am alex,age32
>>> s1 = 'i am {0},age{1}'.format(*['alex',32])
>>> print (s1)
i am alex,age32
>>> s1 = 'i am {name},age{age}'.format(name='alex',age=32)
>>> print (s1)
i am alex,age32
>>> dic = {'name':'alex','age':32}
>>> s2 = 'i am {name},age {age}'.format(**dic)
>>> print (s2)
i am alex,age 32
全局变量,所有作用域都可读
对全局变量进行【重新赋值】,需要global
特殊:列表字典,可修改,不可重新赋值

 

转载于:https://my.oschina.net/u/2551551/blog/679271

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值