给我二十道python的编程题和代码
时间: 2023-06-14 13:07:28 浏览: 53
好的,以下是20道Python编程题和代码:
1. 输入一个字符串,输出该字符串中最长的连续数字的长度。
```python
def longest_number(s):
longest_len = 0
current_len = 0
for c in s:
if c.isdigit():
current_len += 1
else:
longest_len = max(longest_len, current_len)
current_len = 0
return max(longest_len, current_len)
s = "abc12345def6789ghi"
print(longest_number(s)) # 4
```
2. 输入一个字符串,统计其中每个字符出现的次数。
```python
def count_chars(s):
char_count = {}
for c in s:
if c in char_count:
char_count[c] += 1
else:
char_count[c] = 1
return char_count
s = "hello world"
print(count_chars(s)) # {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
```
3. 输入一个字符串,判断该字符串是否为回文字符串。
```python
def is_palindrome(s):
return s == s[::-1]
s = "racecar"
print(is_palindrome(s)) # True
```
4. 输入一个字符串,输出该字符串中出现次数最多的字符和出现次数。
```python
def most_common_char(s):
char_count = {}
for c in s:
if c in char_count:
char_count[c] += 1
else:
char_count[c] = 1
max_count = 0
max_char = ''
for char, count in char_count.items():
if count > max_count:
max_count = count
max_char = char
return max_char, max_count
s = "hello world"
print(most_common_char(s)) # ('l', 3)
```
5. 输入一个列表,输出其中的偶数。
```python
def even_numbers(lst):
return [num for num in lst if num % 2 == 0]
lst = [1, 2, 3, 4, 5, 6]
print(even_numbers(lst)) # [2, 4, 6]
```
6. 输入一个列表,输出其中的奇数。
```python
def odd_numbers(lst):
return [num for num in lst if num % 2 != 0]
lst = [1, 2, 3, 4, 5, 6]
print(odd_numbers(lst)) # [1, 3, 5]
```
7. 输入一个列表,输出其中所有元素的和。
```python
def sum_list(lst):
return sum(lst)
lst = [1, 2, 3, 4, 5]
print(sum_list(lst)) # 15
```
8. 输入一个列表,输出其中所有元素的乘积。
```python
def product_list(lst):
product = 1
for num in lst:
product *= num
return product
lst = [1, 2, 3, 4, 5]
print(product_list(lst)) # 120
```
9. 输入一个列表,输出其中的最大值。
```python
def max_list(lst):
return max(lst)
lst = [1, 2, 3, 4, 5]
print(max_list(lst)) # 5
```
10. 输入一个列表,输出其中的最小值。
```python
def min_list(lst):
return min(lst)
lst = [1, 2, 3, 4, 5]
print(min_list(lst)) # 1
```
11. 输入两个列表,输出它们的并集。
```python
def union_lists(lst1, lst2):
return list(set(lst1 + lst2))
lst1 = [1, 2, 3]
lst2 = [3, 4, 5]
print(union_lists(lst1, lst2)) # [1, 2, 3, 4, 5]
```
12. 输入两个列表,输出它们的交集。
```python
def intersection_lists(lst1, lst2):
return list(set(lst1) & set(lst2))
lst1 = [1, 2, 3]
lst2 = [3, 4, 5]
print(intersection_lists(lst1, lst2)) # [3]
```
13. 输入两个列表,输出它们的差集。
```python
def difference_lists(lst1, lst2):
return list(set(lst1) - set(lst2))
lst1 = [1, 2, 3]
lst2 = [3, 4, 5]
print(difference_lists(lst1, lst2)) # [1, 2]
```
14. 输入一个字符串,输出其中所有大写字母的个数。
```python
def count_uppercase(s):
count = 0
for c in s:
if c.isupper():
count += 1
return count
s = "Hello World"
print(count_uppercase(s)) # 2
```
15. 输入一个字符串,输出其中所有小写字母的个数。
```python
def count_lowercase(s):
count = 0
for c in s:
if c.islower():
count += 1
return count
s = "Hello World"
print(count_lowercase(s)) # 8
```
16. 输入一个字符串,输出其中所有数字的个数。
```python
def count_digits(s):
count = 0
for c in s:
if c.isdigit():
count += 1
return count
s = "abc12345def6789ghi"
print(count_digits(s)) # 8
```
17. 输入一个字符串,输出其中所有非字母和数字的字符个数。
```python
def count_special_chars(s):
count = 0
for c in s:
if not c.isalnum():
count += 1
return count
s = "Hello, World!"
print(count_special_chars(s)) # 2
```
18. 输入一个字符串,输出其中所有单词的个数。
```python
def count_words(s):
words = s.split()
return len(words)
s = "Hello, World!"
print(count_words(s)) # 2
```
19. 输入一个字符串,将其中所有单词首字母大写。
```python
def capitalize_words(s):
words = s.split()
capitalized_words = [word.capitalize() for word in words]
return ' '.join(capitalized_words)
s = "hello world"
print(capitalize_words(s)) # Hello World
```
20. 输入一个字符串,将其中所有单词反转。
```python
def reverse_words(s):
words = s.split()
reversed_words = [word[::-1] for word in words]
return ' '.join(reversed_words)
s = "hello world"
print(reverse_words(s)) # olleh dlrow
```