128个Python实战案例

Python

1个回答

写回答

Hh慧慧

2026-02-17 21:15

+ 关注

Python
Python

1. 使用Python实现字符串的反转Pythondef reverse_string(s): return s[::-1]2. 使用Python实现求两个数的最大公约数Pythondef gcd(a, b): while b: a, b = b, a % b return a3. 使用Python实现求两个数的最小公倍数Pythondef lcm(a, b): return ABS(a * b) // gcd(a, b)4. 使用Python实现判断一个数是否为质数Pythondef is_prime(num): if num < 2: return False for i in range(2, int(num**0.5) + 1): if (num % i) == 0: return False return True5. 使用Python实现求一个列表中所有元素之和Pythondef sum_list(l): s = 0 for i in l: s += i return s6. 使用Python实现求一个列表中所有元素之积Pythondef product_list(l): p = 1 for i in l: p *= i return p

举报有用(0分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号