Python就业班——Python基础知识
Posted 嘆世殘者——華帥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python就业班——Python基础知识相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python3 # coding=utf-8 # Version:python3.6.1 __date__ = ‘2020/5/23 10:21‘ __author__ = ‘Lgsp_Harold‘ # account = "888888" # amt = 123456789 # # str = format(amt, "0,.2f") # str = "请你向{}账户转账¥{:0,.2f}元".format(account, amt) # print(str) # str1 = "{p1},{p2}".format(p1=account, p2=amt) # print(str1) # ———————————————————————————— # num = int(input("输入阶乘数").strip()) # if num < 0 or num > 100: # print("0-100之间的数字") # else: # i = 1 # result = 1 # while i <= num : # result = result * i # if i % 5 == 0: # print(i) # i = i + 1 # print(result) # ———————————————————————————— # num = 1000 # j = 2 # # while j <= num: # is_prime = True # i = 2 # while i < j: # if j % i == 0: # is_prime = False # break # i += 1 # if is_prime: # print("{}是质数".format(j)) # j += 1 # ———————————————————————————— # list1 = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘] # len1 = len(list1) # print(len1) # i = 0 # for l in list1: # if list1[i] == ‘c‘: # ri = len1 * -1 + i # print(l, i, ri) # i += 1 # # i = 0 # len1 = len(list1) # while i < len1: # if list1[i] == ‘c‘: # ri = len1 * -1 + i # print(list1[i], i, ri) # i += 1 # ———————————————————————————— # emp_list = [] # # while True: # info = input("请输入:") # if info == "": # break # info_list = info.split(",") # emp_list.append(info_list) # # print(emp_list) # # for emp in emp_list: # print("姓名:{n},年龄:{a},薪资:{s}".format(n=emp[0],a=emp[1],s=emp[2])) # ———————————————————————————— # # 创建字典 # dict1 = {"name": "aa", "age": 19, "sex": "1"} # print(dict1) # dict2 = dict(name="bb", age=45, sex="0") # print(dict2) # dict3 = dict.fromkeys([‘name‘, ‘age‘, ‘sex‘]) # print(dict3) # dict4 = dict.fromkeys([‘name‘, ‘age‘], ‘N/A‘) # print(dict4) # # 查找 # print(dict1["name"]) # print(dict2.get("name")) # print(dict2.get("dept")) # print(dict2.get("dept", "客服部")) # print("sex" in dict1) # for key in dict1: # print(dict1[key]) # print(dict1.get(key)) # # for key, value in dict2.items(): # print(key, value) # # # 新增、修改 # dict1["name"] = "cc" # print(dict1) # dict2.update(name="dd", age=99) # print(dict2) # dict2.update(dept="研发部") # print(dict2) # dict2.update(weight=80, dept="客服部") # print(dict2) # # 删除 # dict1.pop(‘age‘) # print(dict1) # dict1.popitem() # # print(dict1) # dict2.clear() # print(dict2) # # # 设置默认值 # dict4.setdefault(‘sex‘, 0) # print(dict4) # # # 获取字典视图 # ks = dict4.keys() # print(ks) # vs = dict4.values() # print(vs) # its = dict4.items() # print(its) # # dict2 = dict(name="bb", age=45, sex="0") # # 老版本字符串格式化 # dict2_str = "姓名:%(name)s, 年龄:%(age)s, 性别:%(sex)s" %(dict2) # print(dict2_str) # # # 新版本字符串格式化 # dict2_str = "姓名:{name}, 年龄:{age}, 性别:{sex}".format_map(dict2) # print(dict2_str) # ———————————————————————————— # # 散列值 # # (单次运行,多次生成一样) # h1 = hash("abc") # h2 = hash("abc") # print(h1) # print(h2) # ———————————————————————————— # #处理员工数据 # source = "77,cl1,ma1,sal1,5000$76,cl2,ma2,sal2,4000$75,cl3,ma3,sal3,3000" # # emp_list = source.split("$") # print(emp_list) # # # 保存所有解析后的员工信息,key是员工编号,value是包含员工完整信息的字典 # all_emp = {} # for i in range(0, len(emp_list)): # print(i) # e = emp_list[i].split(",") # print(e) # # 创建员工字典 # emp = {‘no‘: e[0], ‘name‘: e[1], ‘job‘: e[2], ‘depa‘: e[3], ‘salary‘: e[4]} # print(emp) # all_emp[emp[‘no‘]] = emp # print(all_emp) # # empno = input("请输入员工编号:") # if empno in all_emp: # emp = all_emp.get(empno) # print(emp) # print(type(emp)) # print("工号:{no}, 姓名:{name}, 岗位:{job}, 部门:{depa}, 工资:{salary}".format_map(emp)) # else: # print("员工不存在") # source = "77,cl1,ma1,sal1,5000$76,cl2,ma2,sal2,4000$75,cl3,ma3,sal3,3000" # emp_list = source.split("$") # print(emp_list) # print(type(emp_list)) # # emp_all = {} # for i in range(0, len(emp_list)): # print(i) # print(emp_list[i]) # e = emp_list[i].split(",") # print(e) # print(type(e)) # emp = {"no": e[0], "name": e[1], ‘a‘: e[2], ‘b‘: e[3], ‘c‘: e[4]} # print(emp) # emp_all[emp[‘no‘]] = emp # # print(emp_all) # source = ‘77,cl1,ma1,sal1,5000$76,cl2,ma2,sal2,4000$75,cl3,ma3,sal3,3000‘ # emp_list = source.split(‘$‘) # all_emp = {} # for i in range(0, len(emp_list)): # e = emp_list[i].split(‘,‘) # emp = {‘id‘: e[0], ‘A‘: e[1], ‘B‘: e[2]} # all_emp[emp[‘id‘]] = emp # # print(all_emp) # ———————————————————————————— # c = ‘abcdef‘ # # for i in range(0, len(c)): # letter = c[i] # print(letter) # ———————————————————————————— # # 斐波那契数列 # result = [] # for i in range(0, 50): # if i == 0 or i == 1: # result.append(1) # else: # result.append(result[i-2] + result[i-1]) # # print(result) # # 判断质数 # # l = 776351721 # # is_prime = True # for i in range(2, l): # if l % i == 0: # print(i) # is_prime = False # break # # if is_prime == True: # print("{0}是质数".format(l)) # else: # print("{0}不是质数".format(l)) # ———————————————————————————— # 序列相互转换 # l1 = [‘a‘, ‘b‘, ‘c‘] # t1 = (‘a‘, ‘b‘, ‘c‘) # s1 = ‘abc123‘ # s2 = ‘abc,123‘ # r1 = range(1,4) # # print(list(t1)) # print(list(s1)) # print(s2.split(‘,‘)) # print(list(r1)) # # print(tuple(l1)) # print(tuple(s1)) # print(tuple(s2.split(‘,‘))) # print(tuple(r1)) # # print(str(l1)) # print(type(str(l1))) # print(‘‘.join(t1)) # print(‘‘.join(r1)) # join必须要求所有元素都是字符串 ‘‘‘ # 将包含数字的序列输出 s3 = ‘‘ for i in r1: s3 += str(i) print(s3) ‘‘‘ # ———————————————————————————— # college1 = {‘A‘, ‘B‘, ‘C‘, ‘D‘} # print(college1) # college2 = set(["E", "F", "G", "H"]) # print(college2) # college3 = set(‘中国广西南宁‘) # print(college3) # # college4 = set() # 集合的数学运算 # college1 = {‘A‘, ‘B‘, ‘C‘, ‘D‘} # college2 = set(["E", "F", "G", "H", "A", "C"]) # 交集 # c3 = college1.intersection(college2) # print(c3) # college1.intersection_update(college2) # print(college1) # ———————————————————————————— # 并集 # c4 = college1.union(college2) # print(c4) # ———————————————————————————— # 差集 # 单项差集 # c5 = college1.difference(college2) # print(c5) # c6 = college2.difference(college1) # print(c6) # college1.difference_update(college2) # ———————————————————————————— # # 双项差集 # c7 = college1.symmetric_difference(college2) # print(c7) # college1.symmetric_difference_update(college2) # ———————————————————————————— # 判断是否为子集issubset:college1.issubset(college2) # 判断是否为父集issuperset:college1.issuperset(college2) # 判断两个集合是否存在重复元素isdisjoint(True为不存在,False为存在):college1.isdisjoint(college2) # ———————————————————————————— # 新增 # college1.add(‘AAAA‘) # college1.update([‘CCC‘, ‘DDD‘, ‘BBB‘]) # college1.update((‘CC‘, ‘DD‘, ‘BB‘)) # 删除,(remove)如不存在,报错;(discard)如不存在,忽略删除操作 # college1.remove(‘DD‘) # college1.discard(‘DDDDDDDD‘) # 不支持更新,须删除后再添加 # ———————————————————————————— # 三种内置生成式 # 生成式语法:[被追加的数据 循环语句 循环或者判断语句] 、 {} # 列表生成式 # ‘‘‘ # lst = [] # for i in range(10, 20): # lst.append(i * 10) # ‘‘‘ # lst = [i * 10 for i in range(10, 20)] # lst1 = [i * 10 for i in range(10, 20) if i % 2 == 0] # print(lst1) # lst2 = [i * j for i in range(1, 5) for j in range(1, 5)] # print(lst2) # ———————————————————————————— # 字典生成式 # lst5 = [‘AA‘, ‘BB‘, ‘CC‘, ‘DD‘] # ‘‘‘ # for i in range(0, len(lst5)): # dict[i+1] = lst5[i] # ‘‘‘ # dict1 = {i+1: lst5[i] for i in range(0, len(lst5))} # print(dict1) # ———————————————————————————— # 集合生成式 # set1 = {i * j for i in range(10, 20) for j in range(10, 20) if i == j} # print(set1) if __name__ == ‘__main__‘: pass
以上是关于Python就业班——Python基础知识的主要内容,如果未能解决你的问题,请参考以下文章
Python就业班——Python基础知识——考试——货币兑换系统(慕K)