1、找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)
def func():
for i in range(10001):
if (i % 5 == 0 or i % 6 == 0) and i % 30 != 0:
print(i,end = " ")
func()
![](https://img-blog.csdnimg.cn/direct/342ef9d6f90b44af875269c999f9497e.png)
2、写一个方法,计算列表所有偶数下标元素的和(注意返回值)
def sumEvenIndex(nums):
total = 0
for i in range(0, len(nums), 2):
total += nums[i]
return total
nums = [1, 2, 3, 4, 6, 10, 18, 22]
result = sumEvenIndex(nums)
print(result)
![](https://img-blog.csdnimg.cn/direct/e2b3bd97f91e49538099626f9a7e37a2.png)
3、根据完整的路径从路径中分离文件路径、文件名及扩展名
import os
def separate_path(full_path):
directory = os.path.dirname(full_path)
filename_with_extension = os.path.basename(full_path)
filename, extension = os.path.splitext(filename_with_extension)
return directory, filename, extension
full_path = '/Desktop/欧鹏/python/练习.py/a.txt'
directory, filename, extension = separate_path(full_path)
print(f'Directory: {directory}')
print(f'Filename: {filename}')
print(f'Extension: {extension}')
![](https://img-blog.csdnimg.cn/direct/cbb7348f6dcf47718a4131dfb7dcb8f6.png)
4、根据标点符号对字符串进行分行
def split_by_punctuation(text):
punctuation = [".", ",", ";", ":", "!", "?"]
for char in text:
if char in punctuation:
text = text.replace(char, char + "\n")
return text
input_text = input("请输入字符串:")
result = split_by_punctuation(input_text)
print(result)
![](https://img-blog.csdnimg.cn/direct/4ce71520db62460d861ded12d64bac8c.png)
5、去掉字符串数组中每个字符串的空格
string_array = [" Hello ","China "," Python "," C++"]
cleaned_array = [s.strip() for s in string_array]
print(cleaned_array)
![](https://img-blog.csdnimg.cn/direct/10ff133cb25c4d6495b75b634d2e6e10.png)
6、两个学员输入各自最喜欢的游戏名称,判断是否一致,如 果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输 出你们俩喜欢不相同的游戏。
A = input("学员1请输出你喜欢的游戏:")
B = input("学员2请输出你喜欢的游戏:")
if A == B:
print("你们俩喜欢相同的游戏")
else:
print("你们俩喜欢不相同的游戏")
![](https://img-blog.csdnimg.cn/direct/fd2ba607e08b4e13933f94b49e7ce955.png)
7、上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?
8、让用户输入一个日期格式如“2008/08/08”,将 输入的日 期格式转换为“2008年-8月-8日”。
input_date = input("请输入日期(格式为year/month/day):")
year, month, day = input_date.split("/")
formatted_date = f"{year}年-{int(month)}月-{int(day)}日"
print("转换后的日期为:", formatted_date)
![](https://img-blog.csdnimg.cn/direct/62be221cae764ed9af43bd91859c743e.png)
9、接收用户输入的字符串,将其中的字符进行排序(升 序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”
input_str = input("请输入一个字符串: ")
sorted_str = ''.join(sorted(input_str))
reversed_sorted_str = sorted_str[::-1]
print(sorted_str)
print(reversed_sorted_str)
![](https://img-blog.csdnimg.cn/direct/07cad49c3e06436ba1663ff9487a0ae9.png)
10、接收用户输入的一句英文,将其中的单词以反序输 出,“hello c java python”→“python java c hello”。
sentence = input("请输入一句英文: ")
words = sentence.split()
reversed_words = list(reversed(words))
reversed_sentence = ' '.join(reversed_words)
print(reversed_sentence)
![](https://img-blog.csdnimg.cn/direct/0d530aedf6604a6d902bb88fb4eae1d5.png)
from urllib.parse import urlparse, parse_qs
url = "http://www.163.com?userName=admin&pwd=123456"
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)
if 'userName' in query_params:
username = query_params['userName'][0]
else:
username = None
domain = parsed_url.netloc
print("用户名:", username)
print("域名:", domain)
![](https://img-blog.csdnimg.cn/direct/7d90f8007af347da88f0ee41929d39b3.png)
12、有个字符串数组,存储了10个书名,书名有长有短,现 在将他们统一处理,若书名长度大于10,则截取长度8的 子串并且最后添加“...”,加一个竖线后输出作者的名字。
def process_book_names(book_names):
processed_names = []
author_name = "张三"
for name in book_names:
if len(name) > 10:
processed_name = name[:8] + "..."
else:
processed_name = name
processed_names.append(processed_name + " | " + author_name)
return processed_names
book_names = ["Python Programming", "Data Science Handbook", "Machine Learning", "Deep Learning Basics",
"Java Programming", "C++ Primer", "JavaScript for Beginners", "HTML & CSS",
"Artificial Intelligence", "Computer Networks"]
processed_books = process_book_names(book_names)
for book in processed_books:
print(book)
![](https://img-blog.csdnimg.cn/direct/20fadf0fdd69425199020eec0118dbe0.png)
13、让用户输入一句话,找出所有"呵"的位置。
sentence = input("请输入一句话:")
positions = [pos for pos, char in enumerate(sentence) if char == "呵"]
print("所有'呵'的位置:", positions)
![](https://img-blog.csdnimg.cn/direct/3be825ee4a50455ab8327d1676a73c2d.png)
14、让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变 成”老牛很**”;
sentence = input("请输入一句话:")
if "邪恶" in sentence:
new_sentence = sentence.replace("邪恶", "**")
print(new_sentence)
else:
print("这句话中没有邪恶。")
![](https://img-blog.csdnimg.cn/direct/158280dc7da04928a1d82e51772619f4.png)
15、判断一个字符是否是回文字符串 "1234567654321" "上海自来水来自海上"
def is_palindrome(s):
return s == s[::-1]
string = "1234567654321"
if is_palindrome(string):
print(string, "是回文字符串。")
else:
print(string, "不是回文字符串。")
![](https://img-blog.csdnimg.cn/direct/21688eb41d9b48009c69ca336a177639.png)
16、过滤某个文件夹下的所有"xx.py"python文件
17、用户管理系统的,密码加密
import hashlib
def encrypt_password(password):
hashed_password = hashlib.sha256(password.encode()).hexdigest()
return hashed_password
password = input("请输入密码:")
encrypted_password = encrypt_password(password)
print("加密后的密码:", encrypted_password)
![](https://img-blog.csdnimg.cn/direct/7085ec7046734f04a761207a7709b719.png)