zoukankan      html  css  js  c++  java
  • day14

    变量
    name='alex'

    age=0

    level=

    life=True
    life=False

    字符串
    数字
    列表
    元组
    字典


    可变不可变
    1、可变
    列表
    字典

    2、不可变
    字符串
    数字
    元组

    访问顺寻
    1、顺序访问
    字符串
    列表
    元组
    2、映射
    字典

    3、直接访问
    数字


    存放元素个数
    容器类型
    列表
    元组
    字典

    原子类型
    数字
    字符串


    集合
    定义:由不同元素组成的集合,集合中是一组无序排列的可hash值,可作为字典的key
    #不同元素组成 无续 集合中的元素必须是不可变类型

    s={1,2,3,4,5,6}
    s=set("hello")


    s=set("hello")
    print(s)

    s=set(['alex','alex','sb'])
    print(s)

    #{'h', 'l', 'o', 'e'}
    #{'alex', 'sb'}
    特性:
    集合的目的是将不同的值存放到一起,不同的集合间用来做关系运算,无需纠结几何中单个值


    集合的内置方法
    s={1,2,3,4,5,6,'sb'}
    s.add('s')
    print(s)

    s.clear()

    s1=s.copy

    s.pop()
    print(s) 随机删

    s.remove('sb') 删除指定元素,删除元素不存在会报错
    print(s)

    s.discard('sb') 删除指定元素,删除元素不存在不会报错
    p_y=[]
    py=['lcg','szw','zjw']
    li=['lcg','szw','c']
    for i in py:
      if i in li:
        p_y.append(i)
    print(p_y)

    py=['lcg','szw','zjw','lcg']
    li=['lcg','szw','sb']
    p=set(py)
    l=set(li)
    print(p,l)
    print(p.intersection(l)) #求交集
    #或者 print(p&l)

    #求并集
    print(p.union(l)) #或者print(p|l)

    #求差集
    print(p-l) #print(p.difference(l))

    # 交叉补集 先合在一起,然后减去共同的部分
    print(p.symmetric_difference(l)) #print(p^l)

    p=p-l 相当于 p.difference_update(l)

    s1={1,2}
    s2={3,5}
    print(s1.isdisjoint(s2)) #判断是否有交集

    s1<=s2 ?
    s1={1,2}
    s2={1,2,3}
    print(s1.issubset(s2)) #issubset #判断是否是子集
    print(s2.issuperset(s1)) # 判断是否是父集


    s1={1,2}
    s2={1,2,3}
    s1.update(s2) #可增加多个值 add只能加一个
    print(s1) #{1,2,3}

    s=frozenset('hello') #定义不可变集合

    names=['alex','alex','wq']
    s=set(names)
    print(s)
    names=list(s)
    print(name)


    字符串格式化
    1、百分号方式
    msg='i am %s my hobby is %s' %('lhf','alex') #%s 可接受一切
    print(msg)

    %d 整数
    %.2f 浮点数 保留两位小数
    %% 百分比

    tp1="i am %(name)s age %(age)d" %{"name":"alex","age":18} #传字典

    print('root','x','0','0',sep=':')

    format
    tp1="i am {},age{},{}".format("seven",18,"alex")
    print(tp1)

    tp1="i am {0},age{1},{2}".format("seven",18,"alex")
    print(tp1)

    tp1="i am {name},age{age}".format(name="seven",age=18)
    print(tp1)

    tp1="i am {name},age{age}".format(**{name:"seven",age:18}) #传字典
    print(tp1)

    tp1="i am {0[0]},age{0[1]}".format([1,2,3],[11,22,33])
    print(tp1)

    tp1="i am {:s},age{:d}".format(*["seven",18])
    print(tp1)

    tp1="numbers:{:b},{:o},{:d},{:x},{:X},{:%}".format(15,15,15,15,15,15,15.87654,2)
    print(tp1)



    函数

    数学意义上的函数
    y=2*x+1

    python中函数定义方法

    def test(x):
    "The function definition"
    x+=1
    return x


    def test(x):
    y=2*x+1
    return y
    v=test(3)
    print(v)

    调试运行:可以带参数也可以不带参数


    使用函数的好处
    1、代码重用
    2、保持一致性,易维护
    3、可扩展性

    函数和过程
    函数:
    过程: 就是没有返回值的函数

    def test01():
    msg='hello The little green frog'
    print(msg)

    def test02():
    msg='hello wudalang'
    print(msg)
    return msg

    def test03():
    msg='hello wudalang'
    print(msg)
    return 1,2,3,4,'a'

    t1=test01()
    t2=test02()
    t3=test03()
    print(t1,t2,t3)

    总结
    返回值=0 返回None
    返回值=1 返回object
    返回值>1 返回tuple


    函数的参数

    形参
    def cal(x,y)

    实参
    cal(a,b)


    cal(2,3)

    a=10
    b=10
    cal(a,b)

    def test(x,y,z):
    print(x)
    print(y)
    print(z)

    test(1,2,3) #位置参数必须一一对应,缺一不可
    test(y=1,x=3,z=4) #关键字参数,位置无需固定 不可缺
    #混合使用 位置参数必须在关键字参数左边

    #默认参数
    def handle(x,type='mysql'):
    print(x)
    print(type)
    handle('hello')
    handle('hello',type='sqlite')
    handle('hello','sqlite')

    参数组
    ** 字典
    *列表
    def test(x,*args):
    print(x)
    print(args)
    print(args[0])
    test(1,2,3,4,5,6)

    结果:
    1
    (2, 3, 4, 5, 6)

    test(1,{'name':'alex'})
    test(1,['x','y','z'])
    #结果
    1
    (['x', 'y', 'z'],)

    test(1,*['x','y','z'])
    #结果:
    1
    ('x', 'y', 'z')


    def test(x,**kwargs):
    print(x)
    print(kwargs)
    test(1,y=2,z=3)
    #结果:
    1
    {'y': 2, 'z': 3}


    def test(x,*args,**kwargs):
    print(x)
    print(args)
    print(kwargs)
    test(1,1,2,1,1,11,1,y=2,z=3)

    #结果
    1
    (1, 2, 1, 1, 11, 1)
    {'y': 2, 'z': 3}



    test(1,*[1,2,3],**{})

     2018-08-17


  • 相关阅读:
    poj 1392 Ouroboros Snake
    poj 1780 Code
    poj 2513 Colored Sticks
    ZOJ 1455 Schedule Problem(差分约束系统)
    poj 3169 Layout (差分约束)
    ZOJ1260/POJ1364国王(King)
    poj 1201/zoj 1508 intervals 差分约束系统
    zoj 2770 Burn the Linked Camp (差分约束系统)
    构造函数和析构函数
    PHP面向对象——静态属性和静态方法
  • 原文地址:https://www.cnblogs.com/jiangjunfeng/p/9492736.html
Copyright © 2011-2022 走看看