字典python
set集合:是一個無序不重複元素的集app
#建立空集合,必須使用set() 而不是 {} s=set();#空集合 print(s); s1=set({1,2,3,4,5}); print(s1); s2={"a","ss",132,343.03,(33,33,44),True}; print(s2);
se=set('abcd'); #空集合,若是隻有一個值,須要加逗號 print(se);#打印結果{'d', 'a', 'c', 'b'} se1=set(('abcd',)); print(se1);#打印結果{'abcd'}
集合的增、刪、改、查函數
s={"a","ss",132,343.03,(33,33,44),True}; s.add("add"); print(s);
s={"a","ss",132,343.03,(33,33,44),True}; s.remove("a"); print(s);
a={1,72,3,4,5,6}; p=a.pop();#隨機刪除集合裏面的元素,括號裏面寫元素名就拋異常; print(a); print(p);#返回刪除的值
集合演示對象
a={1,2,3,4,5,"a"}; b={"a","b","c","d","e"}; print(a|b);
a={1,2,3,4,5,"a"}; b={"a","b","c","d","e"}; print(a&b);
a={1,2,3,4,5,"a"}; b={"a","b","c","d","e"}; print(a-b);#集合a比集合b 多的其餘元素 print(b-a);#集合b比集合a 多的其餘元素
a={1,2,3,4,5,"a"}; b={"a","b","c","d","e"}; print(a^b);
a={1,3,5,7,9}; b={2,4,6,8,10}; a.update(b); print(a)
a={1,2,3,4,5,"a"}; for i in a: print(i,end=" ");
通信錄管理系統rem
flag=True; names=[]; phones=[]; while flag: print("=======通信錄管理系統=======") print("1.增長姓名和手機") print("2.刪除姓名") print("3.修改手機") print("4.查詢全部用戶") print("5.根據姓名查找手機號") print("6.退出") print("============================") i=int(input("請輸入服務序號:")); if i==6: flag=False; elif i==1: names.append(input('請輸入姓名:')); phones.append(input('請輸入電話號碼:')); elif i==2: name=input('請輸入要刪除的姓名:'); if name in names: index=name.index(name); names.pop(index); phones.pop(index); print('刪除成功'); else: print('姓名不存在'); elif i==3: phone=input('請輸入舊的手機號:') if phone in phones: new_phones=input('請輸入新的手機號:') index=phones.index(phone); phones[index]=new_phones print('手機號修改爲功'); else: print('手機號碼不存在'); elif i==4: for i in range(len(names)): print('姓名:'+names[i]); print('電話:'+phones[i]); elif i==5: name=input('請輸入姓名:'); if name in names: index=name.index(name); print('手機號:',phones[index]); else: print('姓名不存在');
flag = True; User={}; phones = []; while flag: print("=======通信錄管理系統=======") print("1.增長姓名和手機") print("2.刪除姓名") print("3.修改手機") print("4.查詢全部用戶") print("5.根據姓名查找手機號") print("6.退出") print("============================") i = int(input("請選擇:")); if i == 6: flag = False; elif i == 1: name = input('請輸入姓名:'); phone = input('請輸入手機號:'); User[name] = phone; elif i == 2: name = input("請輸入姓名:"); if name in User: del User[name] # User.pop(name) print("刪除成功!") else: print("姓名不存在!") elif i == 3: name = input("請輸入姓名:"); if name in User: new_phone = input("請輸入手機號:") User[name] = new_phone; print("更新成功!") else: print("姓名不存在!") elif i == 4: for k, v in User.items(): print(k, v) elif i == 5: name = input("請輸入姓名:"); if name in User: print("手機號是:", User[name]) else: print("姓名不存在!")
函數 組織好的,可重複使用的,用來實現單一,或相關聯功能的代碼段字符串
def add():#def 函數名(參數列表): a=3; #函數體 b=3; print(a+b); add(); def add(a,b):#def 函數名(參數列表): print(a+b); add(3,3); def add(a,b): print("函數內部:",a+b); return a+b; ret=add(3,3); print("返回值:",ret);
可更改與不可更改對象input
#不可變類型:變量賦值a=5後再次賦值a=10,這裏實際是新生成一個int值對象10,再讓a指向它,而5被丟棄, # 不是改變a的值;至關於新生成了a #不可變類型:值傳遞(複製副本),如fun(a),傳遞的只是a的值,沒有影響a對象自己;如在fun(a)內部修改 # a的值,只是修改另外一個複製的對象,不會影響a自己 def fun(a): a=10; print("函數內部a:",a); a=5; print("調用前:",a);#調用前: 5 fun(a); #函數內部a: 10 print("調用後:",a);#調用後: 5 def fun(a1): a1="李四"; print("函數內部a:",a1); a="張三"; print("調用前:",a);#調用前: 張三 fun(a); #函數內部a: 李四 print("調用後:",a);#調用後: 張三
ff = ['悟空', '八戒', '沙僧']; def fun(nums): nums[0]="張飛"; nums[1]="劉備"; nums = ff; nums = ["李逵", "松江"]; print("調用前:", nums) fun(nums) print("調用後:", nums)