python
Lxq0218
这个作者很懒,什么都没留下…
展开
-
python中字符串常见操作
python中字符串常见操作str1="hello python he"str2="我的外号是'大蟒蛇'"#(1)获取字符串中 的字符print(str1[1])#eprint(str2[0])#我#(2)遍历字符串所有字符for char in str2: print(char)#(3)字符串长度print(len(str1))#12print(len(str2))#10#(4)查找子串出频率print(str1.count("l"))#字符l出现的频率print(str原创 2021-05-13 18:46:34 · 111 阅读 · 0 评论 -
python中异常处理案例
python中异常处理案例def input_id(): #(1)请输入学号 id=input("请输入学号:") #假设id为5已存在 if id=="5": e=Exception("id学号已经存在")#创建一个异常对象(存储了异常信息) raise e #抛出异常 else: return idid=""try: id=input_id()except Exception as e: p原创 2021-05-13 18:43:27 · 181 阅读 · 0 评论 -
python往Excel写数据
python往Excel写数据#提前写好的(字典)productList = [{ "sku": 1, "name": "iphone 12 pro max", "price": 12999, "brand": "苹果" }, { "sku": 2, "name": "mate 40", "price": 7999, "brand": "华为" }, {原创 2021-05-12 22:24:01 · 95 阅读 · 0 评论 -
python/文件的读写操作及语法
## python中文件读写#往csv中写文件def csv_write(): with open("data.csv",'w',encoding="utf-8")as file: file.write("id,name,url,num") file.write("\n") file.write("1,百度,www,baidu.com,100") file.write("\n") file.write("2,goo原创 2021-05-12 21:49:14 · 89 阅读 · 0 评论 -
亲密数对(3~3000)
亲密数对(3~3000)## 整体思路: 分别计算A:3,4,5,6他们因子之和 B 对因子和B计算 因子和C 如果A==C,说明A和B是亲密数对如果整数A的全部因子(包括1,不包括A本身)之和等于B;且整数B的全部因子(包括1,不包括B本身)之和等于A,则将整数A和B称为亲密数。A:1,3B:1,2 A220..284 1184.. 1210 2620.. 2924代码如下def factorsSum(n): a=1 sum=0 while a原创 2021-05-03 19:55:48 · 341 阅读 · 0 评论 -
2021-05-03
循环的案例# 找出三位数的所有水仙花数# 首先要限定范围```sqln = 100while (n<=999): # 获取各个个位数之和 # (1)提取百位 a =n // 100 # (2)提取十位 b = n // 10 % 10 # (3)提取个位 c = n% 10 sum = a**3 + b**3 + c**3 #符合条件干什么 if sum == n : print('%d是水仙花数'原创 2021-05-03 09:15:04 · 81 阅读 · 0 评论