第四天学习笔记

字符串
数字
列表
元组
字典

#############变量的分类
可变与不可变
1、可变:列表,字典
2、不可变:字符串、元组、数字

访问顺序:
1、顺序访问:字符串、列表、元组  有序
2、映射:字典  字典查询速度快 但是内存占得多 无序
3、直接访问:数字


###################集合
1、无序、不同元素组成、集合中的元素必须位不可变型
2、表示 {} set
3、具有去重特性
    s = set('hello')
    s
    {'h','o','l','e'}

##################方法
    1、 add(self, *args, **kwargs): # real signature unknown
        末位添加
        Add an element to a set.
        This has no effect if the element is already present.
    2、clear(self, *args, **kwargs): # real signature unknown
        清空字符串
        """ Remove all elements from this set. """
    3、copy(self, *args, **kwargs): # real signature unknown
        赋值字符串
        """ Return a shallow copy of a set. """
    4、 pop(self, *args, **kwargs): # real signature unknown
        随机删除一个值
        """
        Remove and return an arbitrary set element.
        Raises KeyError if the set is empty.
        """
    5、remove(self, *args, **kwargs): # real signature unknown
        直接删除指定值,不存在会报错
        Remove an element from a set; it must be a member.
        
        If the element is not a member, raise a KeyError.
    6、 discard(self, *args, **kwargs): # real signature unknown
        指定删除,不存在不会报错
        """
        Remove an element from a set if it is a member.
        If the element is not a member, do nothing.
        """
        pass
    7、intersection(self, *args, **kwargs): # real signature unknown
       求交集 也可以使用     &
       Return the intersection of two sets as a new set.
        p_s =set(python_name)
        l_s = set(linux_name)
        print(p_s.intersection(l_s))
        print(p_s & l_s)
    8、union(self, *args, **kwargs): # real signature unknown
        求并集  也可以使用    |
        只求一个结果,并不赋值
        Return the union of sets as a new set.
        (i.e. all elements that are in either set.)
    9、difference(self, *args, **kwargs): # real signature unknown
        差集 返回左边的集合减去公共部分的集合 也可以使用符号    -
        Return the difference of two or more sets as a new set.
        (i.e. all elements that are in this set but not the others.)
    10、symmetric_difference(self, *args, **kwargs): # real signature unknown
        交叉补集  先取并集减去交集  可以用符号    ^
        '''Return the symmetric difference of two sets as a new set.
        (i.e. all elements that are in exactly one of the sets.)
        '''
    11、difference_update(self, *args, **kwargs): # real signature unknown
        作完差集直接赋值
       """ Remove all elements of another set from this set. """
    12、isdisjoint(self, *args, **kwargs): # real signature unknown
        如果两个集合没有交集返回    True
        """ Return True if two sets have a null intersection. """
    13、 issubset(self, *args, **kwargs): # real signature unknown
        print(s1.issubset(s2))判断s1是不是s2子集
        """ Report whether another set contains this set. """
    14、issuperset(self, *args, **kwargs): # real signature unknown
        print(s1.issuperset(s2))判断s1是不是s2父集
        """ Report whether this set contains another set. """
    15、update(self, *args, **kwargs): # real signature unknown
        更新值 可以迭代就能传值
        s1.update(s2)
        
        """ Update a set with the union of itself and others. """


#####################字符串格式化
常用格式化
    1、msg = 'i am %s hobby ' %'zhouyang'
       msg = 'i am %s hobby %s ' %('zhouyang',1) # %s  能添加任意值   %d 对应的为整型
    打印浮点型
    2、tpl = 'percentage %.2f' %99.3424324  .2 小数点后保留两位
    3、打印百分比
        tpl = 'percentage %.2f%%' %99.3424324  两个百分号
    4、传个字典
        tp1 = 'i am %(name)s haha %(key)s' %{'name':'fsda','key':'dsa'}
    5、左对齐
        tp1 = 'i am %(name)-60s haha %(key)s' %{'name':'fsda','key':'dsa'}
        print(tp1)
        >>>i am fsda                                                         haha dsa
    6、加颜色
            tp1 = 'i am 33[45;1m %(name)-60s33[Om haha %(key)s' %{'name':'fsda','key':'dsa'}
            033[44;1m   开头 44代表颜色          33[Om 结尾 
format 的格式化
    1、tp1 = 'i am {},age{},{}'.format('seven','234','erw')  #无索引 一一对应 否则报错
    2、tp1 = 'i am {1},age{2},{0}'.format('seven','234','erw') #按索引填充
    3、tp1 = 'i am {:s},age{:d},{:s}'.format('seven',234,'erw')
    4、tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])  #* 表示传了个列表
    5、tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) # ** 传个字典
    6、tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
    7、tp1 = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
        b:二进制 o:八进制 d:整型 x:十六进制 小写的  X: 十六进制 字母大写 % 保留小数默认六位

        
############################函数#######################

函数创建
    def test(x):
    '注释'
    x+=1 #代码块
    return x
可以带参数也可以不带参数    
有无return都是函数
形参和实参 
        形参不占用空间只有在调用时才占用空间 实参占用空间
        
        def calc(x,y):  #形参
            res = x**y
            return res #函数一碰到return 就结束了
        c = calc(a,b) #实参
        print(c)
        
        
        def test(x,y,z):
            print(x)
            print(y)
            print(z)
        test(1,2,3)  #位置参数 ;一一对应 多一个不行,少一个也不行
        
        关键字参数位置无需固定
        def test(y=1,x=3,z=3)
        test(1,2,4,y=1)#报错 一一对应
        位置参数和关键字参数混合使用 :位置参数必须在关键词参数左边

默认参数
        def handle(x,type='mysql')
            print(x)
            print(type)
            

参数组:**字典 *列表
*args接收的还是列表  接收的位置参数
def test(x,*args):  
    print(x)
    print(args)
test(1,23,43,342,9)
test(1,{'name':'alex'})#还是把这个字典当成一个元组的值
test(1,*['z','d','y'])#加一个*遍历,不加*还是把这个列表当成一个元组的值
*args 输出的是元组

**args 接收关键字参数 如 x=123 

def test(x,*args,**kargs)#可以输入任何内容   *args和**kargs一起出现必须按照顺序



    
原文地址:https://www.cnblogs.com/zy0718/p/8444224.html