安裝IDE(PyCharm)&破解
- 點擊下載安裝包,安裝。
- 點擊下載破解補丁。
- 進入pycharm安裝bin目錄,將破解補丁copy到當前目錄。
- 在bin目錄下找到pycharm.exe.vmoptions和pycharm64.exe.vmoptions文件,在其最後一行加上-javaagent:安裝目錄(例:-javaagent:C:\Program Files\JetBrains\PyCharm 2018.2.1\bin\JetbrainsCrack-3.1-release-enc.jar)
- 完成破解
字符串格式化
1 name = '張三';
2 age = 18;
3 infoStr = '姓名:%s;年齡:%d'%(name,age);
4 print(infoStr);#result:姓名:張三;年齡:18
for循環
1 #輸出1-100之間的奇數
2 for i in range(1,100,2):#1:起始值 100:結束值 2:步長(含首不含尾)
3 print(i);
列表(list)
list[起始索引,終止索引,步長]:切片
1 letter_list = ['a', 'b', 'c'];
2 print(letter_list[0]); # result:a
3 print(letter_list[0:3]); # result:['a', 'b', 'c']
4 print(letter_list[3::-1]); # result:['c', 'b', 'a']
append():尾部追加
1 letter_list = ['a', 'b', 'c'];
2 letter_list.append('d');
3 print(letter_list); # result:['a', 'b', 'c', 'd']
insert():指定位置插入
1 letter_list = ['a', 'b', 'c'];
2 letter_list.insert(2, 'c');
3 print(letter_list); # result:['a', 'b', 'c', 'c']
count():返回指定元素出現的次數
1 letter_list = ['a', 'b', 'c', 'c'];
2 count = letter_list.count('c');
3 print(count); # result:2
index():獲取元素索引,有多個則返回第一個
1 letter_list = ['a', 'b', 'c', 'c'];
2 index = letter_list.index('c');
3 print(index); # result:2
pop():移除指定索引處元素並返回
1 letter_list = ['a', 'b', 'c', 'c'];
2 element = letter_list.pop(2)
3 print(letter_list); # result:['a', 'b', 'c']
4 print(element); # result:c
remove():移除指定元素,有多個則移除第一個
1 letter_list = ['a', 'b', 'c', 'c'];
2 letter_list.remove('c');
3 print(letter_list); # result:['a', 'b', 'c']
reverse():反轉順序
1 letter_list = ['a', 'b', 'c'];
2 letter_list.reverse();
3 print(letter_list); # result:['c', 'b', 'a']
sort():排序
1 letter_list = ['b', 'a', 'c'];
2 letter_list.sort();
3 print(letter_list); # result:['a', 'b', 'c']
extend():擴展
1 letter_list = ['b', 'a', 'c'];
2 letter_list.extend(['d', 'e', 'f']);
3 print(letter_list); # result:['b', 'a', 'c', 'd', 'e', 'f']
無序不重複集合(set)
列表去重
1 num_list = [1, 2, 2, 3, 3, 4]
2 print(set(num_list)) # result:{1, 2, 3, 4}
union()或'|':並集
1 num_set1 = {1, 2, 3, 4}
2 num_set2 = {3, 4, 5, 6}
3 print(num_set1 | num_set2) # result:{1, 2, 3, 4, 5, 6}
4 print(num_set1.union(num_set2)) # result:{1, 2, 3, 4, 5, 6}
intersection()或'&':交集
1 num_set1 = {1, 2, 3, 4}
2 num_set2 = {3, 4, 5, 6}
3 print(num_set1 & num_set2) # result:{3, 4}
4 print(num_set1.intersection(num_set2)) # result:{3, 4}
difference()或'-':差集
1 num_set1 = {1, 2, 3, 4}
2 num_set2 = {3, 4, 5, 6}
3 print(num_set1 - num_set2) # result:{1, 2}
4 print(num_set1.difference(num_set2)) # result:{1, 2}
5 print(num_set2.difference(num_set1)) # result:{5, 6}
symmetric_difference()或'^':對稱差集(雙方並集去除交集)
1 num_set1 = {1, 2, 3, 4}
2 num_set2 = {3, 4, 5, 6}
3 print(num_set1 ^ num_set2) # result: {1, 2, 5, 6}
4 print(num_set1.symmetric_difference(num_set2)) # result: {1, 2, 5, 6}
update()或'|=':並集並賦值
1 num_set1 = {1, 2, 3, 4}
2 num_set2 = {3, 4, 5, 6}
3 num_set1.update(num_set2)
4 # 或 num_set1 |= num_set2
5 print(num_set1) # result:{1, 2, 3, 4, 5, 6}
6 print(num_set2) # result:{3, 4, 5, 6}
intersection_update或'&=':交集並賦值
1 num_set1 = {1, 2, 3, 4}
2 num_set2 = {3, 4, 5, 6}
3 num_set1.intersection_update(num_set2)
4 # 或 num_set1&=num_set2
5 print(num_set1) # result:{3, 4}
6 print(num_set2) # result:{3, 4, 5, 6}
symmetric_difference_update()或'^=':並集去除交集並賦值
1 num_set1 = {1, 2, 3, 4}
2 num_set2 = {3, 4, 5, 6}
3 num_set1.symmetric_difference_update(num_set2) # result:{1, 2, 5, 6}
4 # 或 num_set1 ^= num_set2
5 print(num_set1)
add():添加
1 num_set1 = {1, 2, 3, 4}
2 num_set1.add(5)
3 print(num_set1) # result:{1, 2, 3, 4, 5}
remove():移除,不存在引起keyError
1 num_set1 = {1, 2, 3, 4}
2 num_set1.remove(2)
3 print(num_set1) # result:{1, 3, 4}
discard():存在則移除
1 num_set1 = {1, 2, 3, 4}
2 num_set1.discard(2)
3 print(num_set1) # result:{1, 3, 4}
pop():隨機刪除並返回
1 num_set1 = {1, 2, 3, 4}
2 removeItem = num_set1.pop()
3 print(num_set1) # result:{2, 3, 4}
4 print(removeItem) # result:1
clear():清空
1 num_set1 = {1, 2, 3, 4}
2 num_set1.clear()
3 print(num_set1) # result:set()
練習
購物車
1 salary = 10000;
2 trade_list = ['iphone6s','mac book','coffee','python book','bicyle'];
3 amount_list = [5300,9000,32,80,1500];
4
5 print('餘額:%s元'%(salary));
6 print('商品列表:');
7 index = 0;
8 for trade in trade_list:
9 index += 1;
10 print('%d:%s %d元'%(index,trade,amount_list[index-1]));
11
12 while True:
13 sort_num = int(input('輸入要夠買的產品的序號:'));
14 if sort_num>trade_list.__len__() or sort_num <= 0:
15 print('輸入的序號不存在!')
16 continue;
17 price = amount_list[sort_num-1];
18 if salary > price:
19 salary = salary - price;
20 print('購買成功,當前餘額:%d'%(salary));
21 else:
22 print('餘額不足,購買失敗,當前餘額:%d'%(salary));
23 choose = input('是否退出?(確認輸入Y)');
24 if choose == 'Y':
25 print('退出成功!')
26 break;
擴展
判斷一個字符串是否是一個數字(string.isdigit())
a = 'a';
print(a.isdigit());#result:False
a = '1';
print(a.isdigit());#result:True