1. 程式人生 > >第四天學習筆記

第四天學習筆記

inux linux from lex 添加 是不是 不可變 type 默認

字符串
數字
列表
元組
字典

#############變量的分類
可變與不可變
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 \033[45;1m %(name)-60s\033[Om haha %(key)s %{name:fsda,key:dsa} \033[44;1m 開頭 44代表顏色 \033[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一起出現必須按照順序

第四天學習筆記