python基础(3)-pycharm安装&for循环&format字符串&list列表&set集合使用

时间:2022-08-18 19:56:36

安装IDE(PyCharm)&破解

  1. 点击下载安装包,安装。
  2. 点击下载破解补丁
  3. 进入pycharm安装bin目录,将破解补丁copy到当前目录。
  4. 在bin目录下找到pycharm.exe.vmoptions和pycharm64.exe.vmoptions文件,在其最后一行加上-javaagent:安装目录(例:-javaagent:C:\Program Files\JetBrains\PyCharm 2018.2.1\bin\JetbrainsCrack-3.1-release-enc.jar)
  5. 完成破解

字符串格式化

 name = '张三';
 age = 18;
 infoStr = '姓名:%s;年龄:%d'%(name,age);
 print(infoStr);#result:姓名:张三;年龄:18

for循环

 #输出1-100之间的奇数
 for i in range(1,100,2):#1:起始值 100:结束值 2:步长(含首不含尾)
     print(i);

列表(list)

list[起始索引,终止索引,步长]:切片

 letter_list = ['a', 'b', 'c'];
 print(letter_list[0]);  # result:a
 print(letter_list[0:3]);  # result:['a', 'b', 'c']
 print(letter_list[3::-1]);  # result:['c', 'b', 'a']

append():尾部追加

 letter_list = ['a', 'b', 'c'];
 letter_list.append('d');
 print(letter_list);  # result:['a', 'b', 'c', 'd']

insert():指定位置插入

 letter_list = ['a', 'b', 'c'];
 letter_list.insert(2, 'c');
 print(letter_list);  # result:['a', 'b', 'c', 'c']

count():返回指定元素出现的次数

 letter_list = ['a', 'b', 'c', 'c'];
 count = letter_list.count('c');
 print(count);  # result:2

index():获取元素索引,有多个则返回第一个

 letter_list = ['a', 'b', 'c', 'c'];
 index = letter_list.index('c');
 print(index);  # result:2

pop():移除指定索引处元素并返回

 letter_list = ['a', 'b', 'c', 'c'];
 element = letter_list.pop(2)
 print(letter_list);  # result:['a', 'b', 'c']
 print(element);  # result:c

remove():移除指定元素,有多个则移除第一个

 letter_list = ['a', 'b', 'c', 'c'];
 letter_list.remove('c');
 print(letter_list);  # result:['a', 'b', 'c']

reverse():反转顺序

 letter_list = ['a', 'b', 'c'];
 letter_list.reverse();
 print(letter_list);  # result:['c', 'b', 'a']

sort():排序

 letter_list = ['b', 'a', 'c'];
 letter_list.sort();
 print(letter_list);  # result:['a', 'b', 'c']

extend():扩展

 letter_list = ['b', 'a', 'c'];
 letter_list.extend(['d', 'e', 'f']);
 print(letter_list);  # result:['b', 'a', 'c', 'd', 'e', 'f']

无序不重复集合(set)

列表去重

 num_list = [1, 2, 2, 3, 3, 4]
 print(set(num_list))  # result:{1, 2, 3, 4}

union()或'|':并集

 num_set1 = {1, 2, 3, 4}
 num_set2 = {3, 4, 5, 6}
 print(num_set1 | num_set2)  # result:{1, 2, 3, 4, 5, 6}
 print(num_set1.union(num_set2))  # result:{1, 2, 3, 4, 5, 6}

intersection()或'&':交集

 num_set1 = {1, 2, 3, 4}
 num_set2 = {3, 4, 5, 6}
 print(num_set1 & num_set2)  # result:{3, 4}
 print(num_set1.intersection(num_set2))  # result:{3, 4}

difference()或'-':差集

 num_set1 = {1, 2, 3, 4}
 num_set2 = {3, 4, 5, 6}
 print(num_set1 - num_set2)  # result:{1, 2}
 print(num_set1.difference(num_set2))  # result:{1, 2}
 print(num_set2.difference(num_set1))  # result:{5, 6}

symmetric_difference()或'^':对称差集(双方并集去除交集)

 num_set1 = {1, 2, 3, 4}
 num_set2 = {3, 4, 5, 6}
 print(num_set1 ^ num_set2)  # result: {1, 2, 5, 6}
 print(num_set1.symmetric_difference(num_set2))  # result: {1, 2, 5, 6}

update()或'|=':并集并赋值

 num_set1 = {1, 2, 3, 4}
 num_set2 = {3, 4, 5, 6}
 num_set1.update(num_set2)
 # 或 num_set1 |= num_set2
 print(num_set1)  # result:{1, 2, 3, 4, 5, 6}
 print(num_set2)  # result:{3, 4, 5, 6}

intersection_update或'&=':交集并赋值

 num_set1 = {1, 2, 3, 4}
 num_set2 = {3, 4, 5, 6}
 num_set1.intersection_update(num_set2)
 # 或 num_set1&=num_set2
 print(num_set1)  # result:{3, 4}
 print(num_set2)  # result:{3, 4, 5, 6}

symmetric_difference_update()或'^=':并集去除交集并赋值

 num_set1 = {1, 2, 3, 4}
 num_set2 = {3, 4, 5, 6}
 num_set1.symmetric_difference_update(num_set2)  # result:{1, 2, 5, 6}
 # 或 num_set1 ^= num_set2
 print(num_set1)

add():添加

 num_set1 = {1, 2, 3, 4}
 num_set1.add(5)
 print(num_set1)  # result:{1, 2, 3, 4, 5}

remove():移除,不存在引发keyError

 num_set1 = {1, 2, 3, 4}
 num_set1.remove(2)
 print(num_set1)  # result:{1, 3, 4}

discard():存在则移除

 num_set1 = {1, 2, 3, 4}
 num_set1.discard(2)
 print(num_set1)  # result:{1, 3, 4}

pop():随机删除并返回

 num_set1 = {1, 2, 3, 4}
 removeItem = num_set1.pop()
 print(num_set1)  # result:{2, 3, 4}
 print(removeItem)  # result:1

clear():清空

 num_set1 = {1, 2, 3, 4}
 num_set1.clear()
 print(num_set1)  # result:set()

练习

购物车

 salary = 10000;
 trade_list = ['iphone6s','mac book','coffee','python book','bicyle'];
 amount_list = [5300,9000,32,80,1500];

 print('余额:%s元'%(salary));
 print('商品列表:');
 index = 0;
 for trade in trade_list:
     index += 1;
     print('%d:%s %d元'%(index,trade,amount_list[index-1]));

 while True:
     sort_num = int(input('输入要够买的产品的序号:'));
     if sort_num>trade_list.__len__() or sort_num <= 0:
         print('输入的序号不存在!')
         continue;
     price = amount_list[sort_num-1];
     if salary > price:
         salary = salary - price;
         print('购买成功,当前余额:%d'%(salary));
     else:
         print('余额不足,购买失败,当前余额:%d'%(salary));
     choose = input('是否退出?(确认输入Y)');
     if choose == 'Y':
         print('退出成功!')
         break;

扩展

判断一个字符串是不是一个数字(string.isdigit())

a = 'a';
print(a.isdigit());#result:False
a = ';
print(a.isdigit());#result:True