1.
def print_fuhe(n):
for i in range(1,n+1):
if (i%5 ==0 or i%6==0) and not (i%5 ==0 and i%6==0):
print(i)
n = int(input("输入需要处理的范围从0开始:"))
print_fuhe(n)
2
冒泡法
import random
lst = [random.randint(2,15) for i in range(10)]
print(lst)
#[3, 9, 14, 8, 10, 13, 15, 2, 13, 9]
length = len(lst)
for i in range(length):
flag = False
for j in range(length-i-1):
if lst[j] > lst[j+1]:
lst[j],lst[j+1] = lst[j+1],lst[j]
flag = True
if not flag:
break
print(lst)
#[2, 3, 8, 9, 9, 10, 13, 13, 14, 15]
3
>>> path = 'C:\\ProgramData\\Dell\\InventoryCollector\\Log\\ICDebugLog.txt'
>>> print(path)
C:\ProgramData\Dell\InventoryCollector\Log\ICDebugLog.txt
# 定义 path 路径
path = 'C:\\ProgramData\\Dell\\InventoryCollector\\Log\\ICDebugLog.txt'
# 获取文件路径
>>> ls1 = path.split('\\')
>>> ls1
['C:', 'ProgramData', 'Dell', 'InventoryCollector', 'Log', 'ICDebugLog.txt']
>>> ls2 = ls1[0:len(ls1)-1:]
>>> ls2
['C:', 'ProgramData', 'Dell', 'InventoryCollector', 'Log']
>>> new_path = '\\'.join(ls2)
>>> print(new_path)
C:\ProgramData\Dell\InventoryCollector\Log
#获取文件名
>>> ls1[len(ls1)-1]
'ICDebugLog.txt'
#获取文件名后缀
>>> tt = ls1[len(ls1) -1]
>>> tt
'ICDebugLog.txt'
>>> tt.split('.')
['ICDebugLog', 'txt']
>>> ls = tt.split('.')
>>> ls[1]
'txt'
4.
string = input()
num = 0
ch = 0
space = 0
other = 0
for x in string:
# if x.isnumeric():
if '\u0030'<=x<='\u0039':
num+=1
# elif x=='\u0020'
elif x.isspace():
space+=1
elif x.isalpha() or '\u4e00' <= x <= '\u9fff':
ch+=1
else:
other+=1
print(ch,space,num,other)
5.
#encoding:UTF-8
msg = 'www.BAIDU.com.123'
for num in msg:
if 97 <= ord(num) <= 122: #小写字母
upper_num = ord(num)-32 #大小写字母之前差了32
#chr()函数可以将编码数值转为字符(python没有字符的概念)
print (chr(upper_num),end='')
else:
print(num,end='') #不是小写字符,原样输出
print() #输出一个换行符
msg = 'www.BAIDU.com.123'
for num in msg:
if 65 <= ord(num) <= 90: #大写字母
upper_num = ord(num)+32 #大小写字母之前差了32
#chr()函数可以将编码数值转为字符(python没有字符的概念)
print (chr(upper_num),end='')
else:
print(num,end='') #不是大写字符,原样输出
6.
import re
line="乃使使割adsad243524河西之, 地献于秦以和€。"
linec=re.findall(r"[\w']+", line)
print(linec)
result=" ".join(linec)
print(result)