-
编写一个程序,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'} dict1 = {'a': 1, 'b': 2, 'c': 3} new_dict = {value: key for key, value in dict1.items()} print(new_dict)
-
编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd' str_ = '12a&bc12d-+' new_str_ = '' for i in str_: if i.islower(): new_str_ += i print(new_str_)
-
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd' str_ = 'abc' new_str_ = '' if str_[0].islower(): new_str_ = str_[0].upper()+str_[1:] print(new_str_) else: print(str_)
-
写程序实现endswith的功能,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True 字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False str1 = 'abc231ab' str2 = 'a1b1' print(str1[-1] == str2[-1])
-
写程序实现isdigit的功能,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True '23函数' 结果: False 'a2390' 结果: False str1 = '123P4921' for i in str1: if not '0' <= i <= '9': print(False) break else: print(True)
-
写程序实现upper的功能,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1' str_1 = 'abH23好rp1' str2 = '' for i in str_1: if 'a' <= i <= 'z': str2 += chr(ord(i)-32) else: str2 += i print(str2)
-
写程序获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1 序列:'abcdpzasdz' 结果: 'z' 序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98
str1=[-7, -12, -1, -9]
str1=‘abcdpzasdz’
str1={‘小明’:90, ‘张三’: 76, ‘路飞’:30, ‘小花’: 98}
if type(str1)==list:
print(max(str1))
elif type(str1)==str:
print(max(str1))
elif type(str7)==dict:
max_=0
for key,value in str1.items():
if value>max_:
max_=value
print(max_)
```
-
写程序实现replace函数的功能,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?'
-
写程序实现split的功能,将字符串中指定子串作为切割点对字符串进行切割
例如:原字符串: 'how are you? and you?' 切割点: 'you' 结果: ['how are ', '? and ', '?']
-
用思维导图(xmind)总结四大容器:列表、字典、元组、集合
列表基础练习题
选择题
-
下列选项中能正确表示一个列表的是(D)。
A.
{1, 2, 3}
B.
[10, abc, 123]
C.
[10 20 30]
D.
[1, 2, 3]
-
(多选)已知一个列表
nums = [10, 20, '小明', [1, 2]]
,以下表达式结果是小明
的是?( C D)A.
nums[-3]
B.
nums[3]
C.
nums[-2]
D.
nums[2]
-
以下选项关于列表说法错误的是?(C)
A. 列表可以放在
for
循环的in
后面B. 列表是可变的序列
C. 列表是有序的,只支持增删改,不支持查操作
D. 列表的
in
操作可以判断元素是否存在 -
已知一个列表
names = ['小乔', '甄姬', '王昭君', '妲己', '女娲', '西施', '嬴政']
,下面的表达式中结果是[]
的是?(C)A. names[1:]
B. names[:1]
C. names[1:4:-1]
D. names[1:4:2]
-
已知列表
list1 = [10, [1, 2], 100, 1000]
,下列表达式结果是True的是?(D)A.
100 not in list1
B.
1 in list1
C.
2 in list1
D.
[1, 2] in list1
-
下列选项中不属于序列的是?(D)
A. []
B. ‘100’
C. {1, 2}
D. 100
-
已知
student = {'name': '小明', 'age': 18, 'gender':'男'}
下列关于字典的操作正确的是?(A)A.
student('name')
B.
student[name]
C.
student['小明']
D.
student['age']
-
下列表达式有误的是?( B )
A.
100 + 30.03
B.
188 * '12'
C.
188 * 12
D.
188 + '12'
-
(多选)下列表达式能产生
[1, 2, 3]
的是?(A D)A.
[1, 2] + [3]
B.
[1, 2].append(3)
C.
[1, 2].extend(3)
D.
[1, 2, 3] * 1
-
(多选)下列选项中属于可变序列的是?(A C)
A. 列表
B. 元组
C. 字典
D. 字符串
填空题
- python中获取指定数据的类型可以使用( type )函数。
- 查看数据地址的函数是( id )。
- 如果要将数据转换成列表,需要使用( list )。
- ( len )函数可以用来获取任意序列中元素的个数。
- 如果需要将一个数据插入到列表的中间需要使用函数( insert )。
- Python中数学集合运算中求交集、并集、差集和对称差集的符号分别是( & )、(| )、( -)、( ^)。
- 请列举出你知道的Python中的不可变的数据类型:( 字符串、元组 )。
- 获取字符编码值和获取编码值对应的字符的函数分别是( ord )、( chr )。
- 如果要判断序列中是否存在某个元素可以使用( in )来判断。
- 如果要判断两个数据的地址是否相等可以使用( id )。
编程题
-
已知一个列表
names = ['胡歌', '王凯', '王俊凯', '杨幂', '刘德华', '张国荣', '王祖贤', '张伟']
。1)依次打印列表中的每个元素
for i in names: print(i)
2)统计列表中姓
张
的人的个数。count = 0 for i in names: if i[0] == '张': count += 1 print(count)
3)统计名字是两个字的人的个数。
count = 0
for i in names:
if len(i) == 2:
count += 1
print(count)
-
已知字典
dog = {'name': '大黄', 'color': 'yellow', 'age': 3, 'kind': '土狗'}
1)打印所有key对应的值
2)将name的值修改成 ‘旺财’
3)添加狗的价格对应的键值对
4)清空dog字典
# 1)打印所有key对应的值
for i in dog:
print(dog[i])
# 2)将name的值修改成 '旺财'
dog['name'] = '旺财'
print(dog['name'])
# 3)添加狗的价格对应的键值对
dog['price'] = 300
print(dog)
# 4)清空dog字典
dog.clear()
print(dog)
-
已知字符串
message = 'You see see, one day day!'
1)统计字符串中非字母的字符个数
print(len(message))
2)提取出所有的小写字母
lower = [i for i in message if i.islower()] print(lower)