python之路day3_python之路:day3
內容
變量的創建過程
身份運算和None
數據類型
一、 變量創建過程
首先,當我們定義了一個變量name = ‘oldboy’的時候,在內存中其實是做了這樣一件事:
程序開辟了一塊內存空間,將‘oldboy’存儲進去,再讓變量名name指向‘oldboy’所在的內存地址。如下圖所示:
我們可以通過id()方法查看這個變量在內存中的地址
1 >>> name = "oldboy"
2 >>>id(name)
3 4317182304
變量的修改
一般我們認為修改一個變量就是用新值把舊值覆蓋掉, 可python是這樣實現的么?
1 >>> name = "oldboy"
2 >>>id(name)
3 4317182304
4 >>>
5 >>> name = "alex"
6 >>> id(name) # 如果只是在原有地址上修改,那么修改后內存地址不應該變化呀。
7 4317182360
實際的原理什么樣的呢? 程序先申請了一塊內存空間來存儲‘oldboy’,讓name變量名指向這塊內存空間
執行到name=‘alex’之后又申請了另一塊內存空間來存儲‘alex’,并讓原本指向‘oldboy’內存的鏈接斷開,讓name再指向‘alex’。
? ? ? ?
變量的指向關系
提問:下面這段代碼為何出現這樣的現象?
1 >>> name1 = 'oldboy'
2 >>> name2 = name1 # 把name1賦值給name2,這樣name2的值也是oldboy了
3 >>> print(name1,name2)
4 oldboy oldboy
5 >>>
6 >>> name1 = 'alex'
7 >>> print(name1,name2) #改了name1后,name2為何沒跟著改?
8 alex oldboy
要想知道上面問題的結果是為什么,首先要了解在內存中兩個變量的存儲情況
從上面的示意圖中我們可以知道,當執行name2=name1這句話的時候,事實上是讓name2指向了‘oldboy’所在的內存地址。
修改name1的值,相當于斷開了name1到‘oldboy’的鏈接,重新建立name1和‘alex’之間的鏈接。在這個過程中,始終沒有影響到name2和‘oldboy‘之間的關系,因此name2還是‘oldboy’,而name1變成了‘alex’。
二、身份運算和None
python 中有很多種數據類型, 查看一個數據的類型的方法是type().
>>> name="克里斯"
>>> age = 29
>>>
>>>name
'克里斯'
>>>
>>>type(name),type(age)
(, )
判斷一個數據類型是不是str, or int等,可以用身份運算符is
>>> type(name) isstr
True
>>>
>>> type(name) is notint
True
空值None
代表什么都沒有的意思,一般用在哪呢? 比如玩游戲,你要初始化一個女朋友, 需要填上姓名、年齡、身高、體重等信息, 這些信息是讓玩家填的,在填之前,你要先把變量定義好,那就得存個值 ,這個值用0,1來占位不合適 ,用True,False也不合適 ,用None最合適
>>> name=None
>>> age=None
>>> height=None
>>> weight=None
>>>
>>>name,age,height,weight
(None, None, None, None)
>>>
此時可用is 運算符來判斷變量是不是None
1 >>> if name isNone:
2 ... print("你的女朋友還沒起名字呢.")
3 ...
4 你的女朋友還沒起名字呢.
其實用==判斷也行,但是不符合開發規范
>>> name ==None
True
三元運算
顯的很NB的代碼寫法。
1 name = "Eva"
2 sex =None
3 # 普通寫法
4 if name == "Eva":
5 sex = "Female"
6 else:
7 sex = "Male"
8 # 用三元運算來寫
9 sex = "Female" if name == "Eva" else "Male"
三、數據類型
列表
定義:[]內以逗號分隔,按照索引,存放各種數據類型,每個位置代表一個元素
再回顧下列表的特點:
1.可存放多個值
2.按照從左到右的順序定義列表元素,下標從0開始順序訪問,有序
3.可修改指定索引位置對應的值,可變
列表增加操作
追加,數據會追加到尾部
1 >>>names2 ['alex', 'jack']3 >>> names.append("rain")4 >>> names.append("eva")5 >>>
6 >>>names7 ['alex', 'jack', 'rain', 'eva']
插入,可插入任何位置
1 >>> names.insert(2,"黑姑娘")2 >>>names3 ['alex', 'jack', '黑姑娘', 'rain', 'eva']4 >>>
合并,可以把另一外列表的值合并進來
1 >>> n2 = ["狗蛋","綠毛","雞頭"]2 >>>names3 ['alex', 'jack', '黑姑娘', 'rain', 'eva']4 >>>names.extend(n2)5 >>>names6 ['alex', 'jack', '黑姑娘', 'rain', 'eva', '狗蛋', '綠毛', '雞頭']
列表嵌套
>>> names.insert(2,[1,2,3])>>>names
['alex', 'jack', [1, 2, 3], '黑姑娘', 'rain', 'eva', '狗蛋', '綠毛', '雞頭']>>> names[2][1]2
刪除操作
del 直接刪
1 >>>names2 ['alex', 'jack', [1, 2, 3], '黑姑娘', 'rain', 'eva', '狗蛋', '綠毛', '雞頭']3 >>> del names[2]4 >>>names5 ['alex', 'jack', '黑姑娘', 'rain', 'eva', '狗蛋', '綠毛', '雞頭']
pop 刪
1 >>>names2 ['alex', 'jack', '黑姑娘', 'rain', 'eva', '狗蛋', '綠毛', '雞頭']3 >>> names.pop() #默認刪除最后一個元素并返回被刪除的值
4 '雞頭'
5 >>>names6 ['alex', 'jack', '黑姑娘', 'rain', 'eva', '狗蛋', '綠毛']7 >>>help(names.pop)8 >>> names.pop(1) #刪除指定元素
9 'jack'
clear 清空
1 >>>n22 ['狗蛋', '綠毛', '雞頭']3 >>>n2.clear()4 >>>n25 []
修改操作
>>>names
['alex', '黑姑娘', 'rain', 'eva', '狗蛋', '綠毛']>>> names[0] = "金角大王"
>>> names[-1] = "銀角大王"
>>>names
['金角大王', '黑姑娘', 'rain', 'eva', '狗蛋', '銀角大王']
查操作
1 >>>names2 ['金角大王', '黑姑娘', 'rain', 'eva', '狗蛋', '銀角大王', 'eva']3 >>>
4 >>> names.index("eva") #返回從左開始匹配到的第一個eva的索引
5 3
6 >>> names.count("eva") #返回eva的個數
7 2
切片
切片就像切面包,可以同時取出元素的多個值
1 names[start:end]2 >>>names3 ['金角大王', '黑姑娘', 'rain', 'eva', '狗蛋', '銀角大王', 'eva']4 >>> names[1:4] #不包含下標4的元素
5 ['黑姑娘', 'rain', 'eva']
*切片的特性是顧頭不顧尾,即start的元素會被包含,end-1是實際取出來的值
倒著切
1 >>> names[-5:-1]2 ['rain', 'eva', '狗蛋', '銀角大王']
但其實我想要的是后5個,只打印了4個,’eva’這個值沒出來,為什么,因為上面提到的顧頭不顧尾
可是想把后5個全取出來如何做呢?
1 >>> names[-5:]2 ['rain', 'eva', '狗蛋', '銀角大王', 'eva']
如果取前幾個值 ,一樣可以把:號左邊的省掉
>>>names
['金角大王', '黑姑娘', 'rain', 'eva', '狗蛋', '銀角大王', 'eva']>>> names[0:3]
['金角大王', '黑姑娘', 'rain']>>> names[:3] #跟上面一樣的效果
['金角大王', '黑姑娘', 'rain']
步長,?允許跳著取值
1 names[start:end:step] #step 默認是1
2 >>>a3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]4 >>> a[0:7:2] #設置步長為2
5 [0, 2, 4, 6]6 >>>a7 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]8 >>>
9 >>> a[::3] #按步長3打印列表,第1個:是省略掉的start:end
10 [0, 3, 6, 9]
列表反轉
1 >>> a[::-1] #通過把步長設置成負值,可達到列表返轉的效果
2 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]3 >>> a[::-2]4 [9, 7, 5, 3, 1]
排序&反轉
排序
1 >>> a = [83,4,2,4,6,19,33,21]2 >>>a.sort()3 >>>a4 [2, 4, 4, 6, 19, 21, 33, 83]
下面的排序結果為何如何解釋?
1 >>> names=['金角大王', 'rain', '@', '黑姑娘', '狗蛋', "4","#",'銀角大王', 'eva']2 >>>names.sort()3 >>>names4 ['#', '4', '@', 'eva', 'rain', '狗蛋', '金角大王', '銀角大王', '黑姑娘']
答案全在這張表上,雖然后面我們會講,但現在先知道,排序的優化級規則是按這張表來的
反轉
1 >>>names2 ['#', '4', '@', 'eva', 'rain', '狗蛋', '金角大王', '銀角大王', '黑姑娘']3 >>>names.reverse()4 >>>names5 ['黑姑娘', '銀角大王', '金角大王', '狗蛋', 'rain', 'eva', '@', '4', '#']
循環列表
>>> for i innames:
...print(i)
...
黑姑娘
銀角大王
金角大王
狗蛋
rain
eva
@4
#
編程練習-購物車程序開發
根據以下數據結構:
1 goods =[2 {"name": "電腦", "price": 1999},3 {"name": "鼠標", "price": 10},4 {"name": "游艇", "price": 20},5 {"name": "美女", "price": 998},6 ......7 ]
實現功能要求:
1、啟動程序后,讓用戶輸入工資,然后進入循環,打印商品列表和編號
2、允許用戶根據商品編號選擇商品
3、用戶選擇商品后,檢測余額是否夠,夠就直接扣款,并加入購物車, 不夠就提醒余額不足
4、可隨時退出,退出時,打印已購買商品和余額
元組
有些時候我們的列表數據不想被人修改時怎么辦? 就可以用元組存放,元組又被稱為只讀列表,不能修改。
定義:與列表類似,只不過[]改成()
特性:
1.可存放多個值
2.不可變
3.按照從左到右的順序定義元組元素,下標從0開始順序訪問,有序
創建
1 ages = (11, 22, 33, 44, 55)2 #或
3 ages = tuple((11, 22, 33, 44, 55))
常用操作
1 #索引
2 >>> ages = (11, 22, 33, 44, 55)3 >>>ages[0]4 11
5 >>> ages[3]6 44
7 >>> ages[-1]8 55
9 #切片:同list
10 #循環
11 >>> for age inages:12 print(age)13 11
14 22
15 33
16 44
17 55
18 #長度
19 >>>len(ages)20 5
21 #包含
22 >>> 11 inages23 True24 >>> 66 inages25 False26 >>> 11 not inages27 False
注意:元組本身不可變,如果元組中還包含其他可變元素,這些可變元素可以改變
1 >>>data2 (99, 88, 77, ['Alex', 'Jack'], 33)3 >>> data[3][0] = '金角大王'
4 >>>data5 (99, 88, 77, ['金角大王', 'Jack'], 33)
為啥呢? 因為元組只是存每個元素的內存地址,上面[‘金角大王’, ‘Jack’]這個列表本身的內存地址存在元組里確實不可變,但是這個列表包含的元素的內存地址是存在另外一塊空間里的,是可變的。
字符串
定義
字符串是一個有序的字符的集合,用于存儲和表示基本的文本信息,’ ‘或’’ ‘’或’’’ ‘’’中間包含的內容稱之為字符串
創建:
s ='Hello,Eva!How are you?'
特性:
按照從左到右的順序定義字符集合,下標從0開始順序訪問,有序
可以進行切片操作
不可變,字符串是不可變的,不能像列表一樣修改其中某個元素,所有對字符串的修改操作其實都是相當于生成了一份新數據。
補充:
1.字符串的單引號和雙引號都無法取消特殊字符的含義,如果想讓引號內所有字符均取消特殊意義,在引號前面加r,如name=r’l\thf’
字符串的常用操作
字符串操作方法有非常多,但有些不常用 ,我們只講重要的一些給大家,其它100年都用不上的有興趣可以自己研究
1 defcapitalize(self):2 首字母大寫3 defcasefold(self):4 把字符串全變小寫5 >> > c = 'Alex Li'
6 >> >c.casefold()7 'alex li'
8 def center(self, width, fillchar=None):9 >> > c.center(50, "-")10 '---------------------Alex Li----------------------'
11 def count(self, sub, start=None, end=None):12 """
13 S.count(sub[, start[, end]]) -> int14 >>> s = "welcome to apeland"15 >>> s.count('e')16 317 >>> s.count('e',3)18 219 >>> s.count('e',3,-1)20 221 def encode(self, encoding='utf-8', errors='strict'):22 """
23 編碼,日后講24 def endswith(self, suffix, start=None, end=None):25 >> > s = "welcome to apeland"
26 >> > s.endswith("land") 判斷以什么結尾27 True28 def find(self, sub, start=None, end=None):29 """
30 S.find(sub[, start[, end]]) -> int31 Return the lowest index in S where substring sub is found,32 such that sub is contained within S[start:end]. Optional33 arguments start and end are interpreted as in slice notation.34 Return -1 on failure.35 """
36 return037 def format(self, *args, **kwargs): #known special case of str.format
38 >> > s = "Welcome {0} to Apeland,you are No.{1} user."
39 >> > s.format("Eva", 9999)40 'Welcome Eva to Apeland,you are No.9999 user.'
41 >> > s1 = "Welcome {name} to Apeland,you are No.{user_num} user."
42 >> > s1.format(name="Alex", user_num=999)43 'Welcome Alex to Apeland,you are No.999 user.'
44 defformat_map(self, mapping):45 """
46 S.format_map(mapping) -> str47 Return a formatted version of S, using substitutions from mapping.48 The substitutions are identified by braces ('{' and '}').49 """
50 講完dict再講這個51 def index(self, sub, start=None, end=None):52 """
53 S.index(sub[, start[, end]]) -> int54 Return the lowest index in S where substring sub is found,55 such that sub is contained within S[start:end]. Optional56 arguments start and end are interpreted as in slice notation.57 Raises ValueError when the substring is not found.58 """
59 defisdigit(self):60 """
61 S.isdigit() -> bool62 Return True if all characters in S are digits63 and there is at least one character in S, False otherwise.64 """
65 returnFalse66 defislower(self):67 """
68 S.islower() -> bool69 Return True if all cased characters in S are lowercase and there is70 at least one cased character in S, False otherwise.71 """
72 defisspace(self):73 """
74 S.isspace() -> bool75 Return True if all characters in S are whitespace76 and there is at least one character in S, False otherwise.77 """
78 defisupper(self):79 """
80 S.isupper() -> bool81 Return True if all cased characters in S are uppercase and there is82 at least one cased character in S, False otherwise.83 """
84 defjoin(self, iterable):85 """
86 S.join(iterable) -> str87 Return a string which is the concatenation of the strings in the88 iterable. The separator between elements is S.89 """
90 >>> n = ['alex','jack','rain']91 >>> '|'.join(n)92 'alex|jack|rain'
93 def ljust(self, width, fillchar=None):94 """
95 S.ljust(width[, fillchar]) -> str96 Return S left-justified in a Unicode string of length width. Padding is97 done using the specified fill character (default is a space).98 """
99 return ""
100 deflower(self):101 """
102 S.lower() -> str103 Return a copy of the string S converted to lowercase.104 """
105 return ""
106 def lstrip(self, chars=None):107 """
108 S.lstrip([chars]) -> str109 Return a copy of the string S with leading whitespace removed.110 If chars is given and not None, remove characters in chars instead.111 """
112 return ""
113 def replace(self, old, new, count=None):114 """
115 S.replace(old, new[, count]) -> str116 Return a copy of S with all occurrences of substring117 old replaced by new. If the optional argument count is118 given, only the first count occurrences are replaced.119 """
120 return ""
121 def rjust(self, width, fillchar=None):122 """
123 S.rjust(width[, fillchar]) -> str124 Return S right-justified in a string of length width. Padding is125 done using the specified fill character (default is a space).126 """
127 return ""
128 def rsplit(self, sep=None, maxsplit=-1):129 """
130 S.rsplit(sep=None, maxsplit=-1) -> list of strings131 Return a list of the words in S, using sep as the132 delimiter string, starting at the end of the string and133 working to the front. If maxsplit is given, at most maxsplit134 splits are done. If sep is not specified, any whitespace string135 is a separator.136 """
137 return[]138 def rstrip(self, chars=None):139 """
140 S.rstrip([chars]) -> str141 Return a copy of the string S with trailing whitespace removed.142 If chars is given and not None, remove characters in chars instead.143 """
144 return ""
145 def split(self, sep=None, maxsplit=-1):146 """
147 S.split(sep=None, maxsplit=-1) -> list of strings148 Return a list of the words in S, using sep as the149 delimiter string. If maxsplit is given, at most maxsplit150 splits are done. If sep is not specified or is None, any151 whitespace string is a separator and empty strings are152 removed from the result.153 """
154 return[]155 def startswith(self, prefix, start=None, end=None):156 """
157 S.startswith(prefix[, start[, end]]) -> bool158 Return True if S starts with the specified prefix, False otherwise.159 With optional start, test S beginning at that position.160 With optional end, stop comparing S at that position.161 prefix can also be a tuple of strings to try.162 """
163 returnFalse164 def strip(self, chars=None):165 """
166 S.strip([chars]) -> str167 Return a copy of the string S with leading and trailing168 whitespace removed.169 If chars is given and not None, remove characters in chars instead.170 """
171 return ""
172 defswapcase(self):173 """
174 S.swapcase() -> str175 Return a copy of S with uppercase characters converted to lowercase176 and vice versa.177 """
178 return ""
179 defupper(self):180 """
181 S.upper() -> str182 Return a copy of S converted to uppercase.183 """
184 return ""
185 defzfill(self, width):186 """
187 S.zfill(width) -> str188 Pad a numeric string S with zeros on the left, to fill a field189 of the specified width. The string S is never truncated.190 """
191 return ""
字典
引子
我們學了列表 , 現在有個需求, 把你們公司每個員工的姓名、年齡、職務、工資存到列表里,你怎么存?
1 staff_list =[2 ["Alex",23,"CEO",66000],3 ["黑姑娘",24,"行政",4000],4 ["佩奇",26,"講師",40000],5 #[xxx,xx,xx,xxx]
6 #[xxx,xx,xx,xxx]
7 #[xxx,xx,xx,xxx]
8 ]
這樣存沒問題,不過你要查一個人的工資的話, 是不是得把列表遍歷一遍
fori instaff_list:
ifi[0]=='黑姑娘':
print(i)
break
但假如你公司有2萬人,如果你要找的黑姑娘正好在列表末尾,那意味著你要遍歷2萬次,才能找到這個信息。列表越大,查找速度越慢。
好了,現在福音來了, 接下來學要的字典可以 查詢數據又快、操作又方便,是日后開發中必備神器。
字典是Python語言中唯一的映射類型。
定義:
{key1:value1,key2:value2}
1、鍵與值用冒號“:”分開;
2、項與項用逗號“,”分開;
示例:
info ={
"name":"小猿圈",
"mission":"幫一千萬極客高效學編程",
"website":"http://apeland.com"
}
特性:
key-value結構
key必須為不可變數據類型、必須唯一
可存放任意多個value、可修改、可以不唯一
無序
查詢速度快,且不受dict的大小影響,至于為何快?我們學完hash再解釋。
創建操作
1 >>>person = {"name": "alex", 'age': 20}2 #或
3 >>>person = dict(name='seven', age=20)4 #或
5 >>>person = dict({"name": "egon", 'age': 20})6 #或
7 >>> {}.fromkeys([1,2,3,4,5,6,7,8],100)8 {1: 100, 2: 100, 3: 100, 4: 100, 5: 100, 6: 100, 7: 100, 8: 100}
增加操作
1 names ={2 "alex": [23, "CEO", 66000],3 "黑姑娘": [24, "行政", 4000],4 }5 #新增k
6 names["佩奇"] = [26, "講師", 40000]7 names.setdefault("oldboy",[50,"boss",100000]) #D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
刪除操作
1 names.pop("alex") #刪除指定key
2 names.popitem() #隨便刪除1個key
3 del names["oldboy"] #刪除指定key,同pop方法
4 names.clear() #清空dict
修改操作
dic['key'] = 'new_value',如果key在字典中存在,'new_value'將會替代原來的value值;
dic.update(dic2) 將字典dic2的鍵值對添加到字典dic中
查操作
1 dic['key'] #返回字典中key對應的值,若key不存在字典中,則報錯;
2 dic.get(key, default = None)#返回字典中key對應的值,若key不存在字典中,則返回default的值(default默認為None)
3 'key' in dic #若存在則返回True,沒有則返回False
4 dic.keys() 返回一個包含字典所有KEY的列表;5 dic.values() 返回一個包含字典所有value的列表;6 dic.items() 返回一個包含所有(鍵,值)元組的列表;
循環
1 1、for k indic.keys()2 2、for k,v indic.items()3 3、for k in dic #推薦用這種,效率速度最快
4 info ={5 "name":"小猿圈",6 "mission": "幫一千萬極客高效學編程",7 "website": "http://apeland.com"
8 }9 for k ininfo:10 print(k,info[k])11 輸出12 name 小猿圈13 mission 幫一千萬極客高效學編程14 website http://apeland.com
求長度
len(dic)
練習題
用你能想到的最少的代碼生成一個包含100個key的字典,每個value的值不能一樣
{‘k0’: 0, ‘k1’: 1, ‘k2’: 2, ‘k3’: 3, ‘k4’: 4, ‘k5’: 5, ‘k6’: 6, ‘k7’: 7, ‘k8’: 8, ‘k9’: 9} 請把這個dict中key大于5的值value打印出來。
把題2中value是偶數的統一改成-1
請設計一個dict, 存儲你們公司每個人的信息, 信息包含至少姓名、年齡、電話、職位、工資,并提供一個簡單的查找接口,用戶按你的要求輸入要查找的人,你的程序把查到的信息打印出來
集合
定義
集合跟我們學的列表有點像,也是可以存一堆數據,不過它有幾個獨特的特點,令其在整個Python語言中占有一席之地,
里面的元素不可變,代表你不能存一個list、dict 在集合里,字符串、數字、元組等不可變類型可以存
天生去重,在集合里沒辦法存重復的元素
無序,不像列表一樣通過索引來標記在列表中的位置 ,元素是無序的,集合中的元素沒有先后之分,如集合{3,4,5}和{3,5,4}算作同一個集合
基于上面的特性,我們可以用集合來干2件事,去重和關系運算
語法
創建集合
1 >>> a = {1,2,3,4,2,'alex',3,'rain','alex'}2 >>>a3 {1, 2, 3, 4, 'alex', 'rain'}
由于它是天生去重的,重復的值你根本存不進去
幫列表去重
幫列表去重最快速的辦法是什么? 就是把它轉成集合,去重完,再轉回列表
1 >>>b2 [1, 2, 3, 4, 2, 'alex', 3, 'rain', 'alex']3 >>>set(b)4 {1, 2, 3, 4, 'alex', 'rain'}5 >>>
6 >>> b = list(set(b)) #一句代碼搞定
7 >>>b8 [1, 2, 3, 4, 'alex', 'rain']
增刪改查
1 >>>a2 {1, 2, 3, 4, 'alex', 'rain'}3 #新增
4 >>> a.add('黑姑娘')5 #刪除discard
6 >>>a7 {2, 3, '黑姑娘', 'alex', 'rain'}8 >>> a.discard('rain') #刪除一個存在的值
9 >>> a.discard('rain2') #如果這個值不存在,do nothing.
10 >>>a11 {2, 3, '黑姑娘', 'alex'}12 >>>
13 #隨機刪除,少用,或特定場景用
14 >>> a.pop() #刪除并返回
15 1
16 #刪除remove
17 >>> a.remove(4)18 #查
19 >>>a20 {2, 3, '黑姑娘', 'alex', 'rain'}21 >>> 'alex' ina22 True23 #改
24 呵呵,不能改。。。
關系運算
1 s_1024 = {"佩奇","老男孩","海峰","馬JJ","老村長","黑姑娘","Alex"}2 s_pornhub = {"Alex","Egon","Rain","馬JJ","Nick","Jack"}3 print(s_1024 & s_pornhub) #交集, elements in both set
4 print(s_1024 | s_pornhub) #并集 or 合集
5 print(s_1024 - s_pornhub) #差集 , only in 1024
6 print(s_pornhub - s_1024) #差集, only in pornhub
7 print(s_1024 ^ s_pornhub) #對稱差集, 把腳踩2只船的人T出去
兩個集合之間一般有三種關系,相交、包含、不相交。在Python中分別用下面的方法判斷:
1 print(s_1024.isdisjoint(s_pornhub)) #判斷2個集合是不是不相交,返回True or False
2 print(s_1024.issubset(s_pornhub)) #判斷s_1024是不是s_pornhub的子集,返回True or False
3 print(s_1024.issuperset(s_pornhub)) #判斷s_1024是不是s_pornhub的父集,返回True or False
總結
以上是生活随笔為你收集整理的python之路day3_python之路:day3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP 进阶:代码整洁之道
- 下一篇: 惠普HP Officejet K7103