2023年7月26日发(作者:)
50道简单python函数题
Python语言是一种易学易用的编程语言,有着丰富的系统库和第三方库,以及强大的数据处理、科学计算能力。其中,函数是Python编程中最为基础和重要的概念之一。下面,我们将介绍50道简单的Python函数题,帮助你掌握Python函数的基础知识。
1. 打印出Hello World!
```python
def hello_world():
print("Hello World!")
```
2. 打印出任意给定的文本
```python
def print_text(text):
print(text)
```
3. 计算两个整数之和
```python
def add(a, b):
return a + b
```
4. 计算两个整数之差 ```python
def subtract(a, b):
return a - b
```
5. 计算两个整数之积
```python
def multiply(a, b):
return a * b
```
6. 计算两个整数之商
```python
def divide(a, b):
return a / b
```
7. 计算两个整数的余数
```python
def remainder(a, b):
return a % b
```
8. 计算两个整数的平均值
```python def average(a, b):
return (a + b) / 2
```
9. 计算一个数的平方
```python
def square(a):
return a ** 2
```
10. 判断一个数是否为偶数
```python
def is_even(a):
if a % 2 == 0:
return True
else:
return False
```
11. 判断一个数是否为奇数
```python
def is_odd(a):
if a % 2 != 0:
return True else:
return False
```
12. 判断一个数是否为质数
```python
def is_prime(a):
if a < 2:
return False
for i in range(2, int(a ** 0.5) + 1):
if a % i == 0:
return False
return True
```
13. 计算一个字符串的长度
```python
def str_length(s):
return len(s)
```
14. 判断一个字符串是否为空
```python
def is_empty(s): if s == "":
return True
else:
return False
```
15. 将一个字符串转换为大写形式
```python
def to_upper_case(s):
return ()
```
16. 将一个字符串转换为小写形式
```python
def to_lower_case(s):
return ()
```
17. 将一个字符串中的每一个单词的首字母大写
```python
def capitalize(s):
return ()
```
18. 判断一个字符串是否包含另一个字符串 ```python
def is_contained(s1, s2):
if (s2) != -1:
return True
else:
return False
```
19. 判断一个字符串是否以给定的字符串开头
```python
def starts_with(s1, s2):
if with(s2):
return True
else:
return False
```
20. 判断一个字符串是否以给定的字符串结尾
```python
def ends_with(s1, s2):
if th(s2):
return True
else: return False
```
21. 将一个字符串反转
```python
def reverse(s):
return s[::-1]
```
22. 将一个字符串中的某一子串替换为另一字符串
```python
def replace(s, old, new):
return e(old, new)
```
23. 将一个列表中的元素转换为字符串
```python
def list_to_str(l):
return "".join(str(x) for x in l)
```
24. 将一个字符串中的数字提取出来,组成一个列表
```python
def extract_numbers(s):
return [int(x) for x in s if t()] ```
25. 计算一个列表中的元素之和
```python
def list_sum(l):
return sum(l)
```
26. 计算一个列表中的元素的平均值
```python
def list_avg(l):
return sum(l) / len(l)
```
27. 判断一个列表中是否包含某个元素
```python
def is_in_list(l, x):
if x in l:
return True
else:
return False
```
28. 获取一个列表中的最大值
```python def list_max(l):
return max(l)
```
29. 获取一个列表中的最小值
```python
def list_min(l):
return min(l)
```
30. 对一个列表进行排序
```python
def list_sort(l):
return sorted(l)
```
31. 反转一个列表
```python
def list_reverse(l):
return l[::-1]
```
32. 将一个列表中的奇数和偶数分别放入两个列表中
```python
def split_list(l): even = [x for x in l if x % 2 == 0]
odd = [x for x in l if x % 2 != 0]
return even, odd
```
33. 合并两个列表
```python
def combine_lists(l1, l2):
return l1 + l2
```
34. 将一个字符串按照某个字符分割为一个列表
```python
def str_to_list(s, sep):
return (sep)
```
35. 判断一个列表是否为回文列表
```python
def is_palindrome(l):
return l == l[::-1]
```
36. 找出一个列表中的重复元素
```python def find_duplicates(l):
return set([x for x in l if (x) > 1])
```
37. 找出一个列表中的不重复元素
```python
def find_uniques(l):
return set([x for x in l if (x) == 1])
```
38. 判断一个字符串是否为回文字符串
```python
def is_palindrome_str(s):
return s == s[::-1]
```
39. 将一个整数转化为二进制形式
```python
def decimal_to_binary(d):
return bin(d)[2:]
```
40. 将一个二进制数转化为十进制形式
```python
def binary_to_decimal(b): return int(b, 2)
```
41. 将一个整数转化为十六进制形式
```python
def decimal_to_hexadecimal(d):
return hex(d)[2:]
```
42. 将一个十六进制数转化为十进制形式
```python
def hexadecimal_to_decimal(h):
return int(h, 16)
```
43. 计算一个数的阶乘
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
```
44. 计算一个数的斐波那契数列值 ```python
def fibonacci(n):
if n == 0 or n == 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
```
45. 判断一个字符串是否为回文联
```python
def is_palindrome_sentence(s):
s = ''.join(c for c in s if a())
return s == s[::-1]
```
46. 判断一个字符串是否为有效数字字符串
```python
def is_valid_number(s):
try:
float(s)
return True
except:
return False ```
47. 计算一个列表中的众数
```python
def mode(l):
from collections import Counter
c = Counter(l)
return [k for k, v in () if v == max(())]
```
48. 获取当前时间的字符串形式
```python
def current_time():
from datetime import datetime
return ().strftime('%Y-%m-%d %H:%M:%S')
```
49. 将一个字符串中的连续空格替换为单个空格
```python
def remove_extra_space(s):
return ' '.join(())
```
50. 计算列表中各元素的出现次数
```python def count_elements(l):
from collections import Counter
return Counter(l)
```
总结:
函数是Python编程中最为基础和重要的概念之一。掌握函数的基础知识,有助于初学者提升Python编程的能力。本文介绍了50道简单的Python函数题,涉及到字符串处理、列表处理以及数值计算等方面。希望本文可以帮助读者更深入地理解Python函数的应用。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1690305482a329573.html
评论列表(0条)