1. 编写一个函数,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
def exchange_key_value(dic):
for i in dict1:
value = dict1.pop(i)
dict1[value] = i
return dic
dict1={'a':1, 'b':2, 'c':3}
res = exchange_key_value(dict1)
print(res)
2. 编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串。
例如: 传入'12a&bc12d-+' --> 'abcd'
def get_letter(str1):
new_s = ''
for i in str1:
if i.islower() or i.isupper():
new_s += i
return new_s
s1 = '12a&bc12d-+A'
res = get_letter(s1)
print(res)
3. 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母。
例如: 'abc' -> 'Abc' '12asd' --> '12asd'
def new_capitalize(str1):
letter1 = str1[0].upper()
new_str1 = letter1 + str1[1:]
return new_str1
s = 'a324fw'
res = new_capitalize(s)
print(res)
4. 写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束。
例如:字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True
字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
def new_endswith(str1, str2):
length = len(str2)
if s1[-len(str2):] == str2:
return True
else:
return False
s1 = 'abc231ab'
s2 = 'abc'
res = new_endswith(s1, s2)
print(res)
5. 写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串。
例如:'1234921' 结果: True
'23函数' 结果: False
'a2390' 结果: False
def new_isdigit(str1):
count = 0
for i in str1:
if not '0' <= i <= '9':
count += 1
if count == 0:
return True
else:
return False
s = '1234921我'
res = new_isdigit(s)
print(res)
6. 写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母。
例如: 'abH23好rp1' 结果: 'ABH23好RP1'
def new_upper(str1):
new_s = ''
for i in str1:
if 'a' <= i <= 'z':
i = chr(ord(i) - 32)
new_s += i
else:
new_s += i
return new_s
s = 'AabH23好rp1a'
res = new_upper(s)
print(res)
7. 写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充。
例如:原字符:'abc' 宽度: 7 字符:'^' 结果: '^^^^abc'
原字符:'你好吗' 宽度: 5 字符:'0' 结果: '00你好吗'
def new_rjust(str1, len1, str2):
if len1 > len(str1):
new_s = str2 * (len1 - len(str1)) + str1
return new_s
else:
new_s = str1
return new_s
res = new_rjust('abc', 7, '^')
print(res)
8. 写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1。
例如:列表: [1, 2, 45, 'abc', 1, '你好', 1, 0] 元素: 1 结果: 0,4,6
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '赵云' 结果: 0,4
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '关羽' 结果: -1
def new_index(list1, ele):
new_s = ''
for i in range(len(list1)):
if list1[i] == ele:
new_s += str(i)
new_s = ','.join(new_s)
if new_s:
return new_s
else:
return -1
l1 = ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
ele = '赵云'
res = new_index(l1, ele)
print(res)
9. 写一个自己的len函数,统计指定序列中元素的个数。
例如:序列:[1, 3, 5, 6] 结果: 4
序列:(1, 34, 'a', 45, 'bbb') 结果: 5
序列:'hello w' 结果: 7
def new_len(obj):
count = 0
for i in obj:
count += 1
return count
obj = (1, 34, 'a', 45, 'bbb')
res = new_len(obj)
print(res)
10. 写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值。
例如:序列:[-7, -12, -1, -9] 结果: -1
序列:'abcdpzasdz' 结果: 'z'
序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98
def new_max(obj):
if type(obj) == dict:
new_obj = list(obj.values())
max_ele = max(new_obj)
else:
new_obj = list(obj)
max_ele = max(new_obj)
return max_ele
obj = {'小明':90, '张三': 76, '路飞':30, '小花': 98}
res = new_max(obj)
print(res)
11. 写一个函数实现自己in操作,判断指定序列中指定的元素是否存在。
例如:序列: (12, 90, 'abc') 元素: '90' 结果: False
序列: [12, 90, 'abc'] 元素: 90 结果: True
def new_in(obj, ele):
for i in obj:
if i == ele:
return True
break
else:
return False
obj = [12, 90, 'abc']
ele = '90'
res = new_in(obj, ele)
print(res)
12. 写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串。
例如:原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?'
def new_replace(str1, str2, str3):
new_s = str3.join(str1.split(str2))
return new_s
s1 = 'how are you? and you?'
s2 = 'you'
s3 = 'me'
res = new_replace(s1, s2, s3)
print(res)
Python基础小练习_函数1
于 2023-03-20 19:46:34 首次发布
这篇博客主要探讨了Python编程中函数的基础概念和使用技巧,包括函数定义、参数传递、返回值等方面,旨在帮助初学者巩固Python函数的知识。

651

被折叠的 条评论
为什么被折叠?



