首页 百科 正文

编程学生成绩管理系统

百科 编辑:伊洋 日期:2024-04-30 04:20:47 1006人浏览

编程题目及答案

```python

def add_numbers(a, b):

return a b

result = add_numbers(5, 3)

print(result) 输出: 8

```

```python

def is_palindrome(s):

return s == s[::1]

result = is_palindrome("radar")

print(result) 输出: True

```

```python

def calculator(a, operator, b):

if operator == ' ':

return a b

elif operator == '':

return a b

elif operator == '*':

return a * b

elif operator == '/':

if b == 0:

return "除数不能为0"

return a / b

else:

return "无效运算符"

result = calculator(10, ' ', 5)

print(result) 输出: 15

```

```python

def find_max_min(lst):

return max(lst), min(lst)

numbers = [3, 7, 2, 9, 5]

max_num, min_num = find_max_min(numbers)

print(f"最大值: {max_num}, 最小值: {min_num}") 输出: 最大值: 9, 最小值: 2

```

```python

def 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 True

result = is_prime(11)

print(result) 输出: True

```

```python

def remove_duplicates(lst):

return list(set(lst))

numbers = [1, 2, 2, 3, 4, 4, 5]

result = remove_duplicates(numbers)

print(result) 输出: [1, 2, 3, 4, 5]

```

```python

def find_common_elements(lst1, lst2):

set1 = set(lst1)

set2 = set(lst2)

return list(set1.intersection(set2))

list1 = [1, 2, 3, 4, 5]

list2 = [3, 4, 5, 6, 7]

result = find_common_elements(list1, list2)

print(result) 输出: [3, 4, 5]

```

```python

def capitalize_words(s):

return ' '.join(word.capitalize() for word in s.split())

sentence = "hello world, it's a beautiful day"

result = capitalize_words(sentence)

print(result) 输出: Hello World, It's A Beautiful Day

```

编程学生成绩管理系统

```python

def count_characters(s):

char_count = {}

for char in s:

char_count[char] = char_count.get(char, 0) 1

return char_count

sentence = "hello, world!"

result = count_characters(sentence)

print(result) 输出: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ',': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1, '!': 1}

```

```python

def is_leap_year(year):

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:

return True

return False

result = is_leap_year(2024)

print(result) 输出: True

```

```python

def fibonacci(n):

a, b = 0, 1

for _ in range(n):

a, b = b, a b

return a

result = fibonacci(7)

print(result) 输出: 13

```

```python

def odd_even_sort(lst):

return sorted(lst, key=lambda x: x % 2 != 0)

numbers = [1, 2, 3, 4, 5, 6, 7]

result = odd_even_sort(numbers)

print(result) 输出: [1, 3, 5, 7, 2, 4, 6]

```

13. 实

分享到

文章已关闭评论!