day10今日作业 + 周末

  1. 编写一个程序,交换指定字典的key和value。

    dict1 = {'a': 1, 'b': 2, 'c': 3}
    new_dict1 = {j:i for i,j in dict1.items()}
    print(new_dict1)
    
  2. 编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串

    str1 = '12a&bc12d-+'
    new_str1 = ''
    for i in str1:
        if 'a'<=i<='z' or 'A'<=i<='Z':
            new_str1 += i
    print(new_str1)
    
  3. 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母

str1 = 'abc'
first_char = str1[0]
if first_char.islower():
    new_str = first_char.upper() + str1[1:]
else:
    new_str = first_char + str1[1:]
print(new_str)  
  1. 写程序实现endswith的功能,判断一个字符串是否已指定的字符串结束
str1 = 'abc231ab'
str2 = 'ab'
if str1[-len(str2):] == str2:
    print(True)
else:
    print(False)
  1. 写程序实现isdigit的功能,判断一个字符串是否是纯数字字符串
str1 = '1234921'
for x in str1:
    if not x.isdigit():
        print(False)
        break
else:
    print(True)
  1. 写程序实现upper的功能,将一个字符串中所有的小写字母变成大写字母
str1 = 'abH23好rp1'
new_str1 = ''
for x in str1:
    if 'a' <= x <= 'z':
        new_str1 += chr(ord(x)- 32)
    else:
        new_str1 += x
print(new_str1)
  1. 写程序获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
   str7 = {'小明': 90, '张三': 76, '路飞': 30, '小花': 98}
   list1 = []
   if type(str7) != dict:
       for i in str7:
           print(type(i))
           if type(i) == int or type(i) == float:
               list1.append(i)
           elif 'a'<=str(i)<='z' or 'A'<=str(i)<='Z':
               list1.append(ord(i))
               if max(list1) == i or max(list1) ==ord(i):
                   print(i)
   else:
       list2=[]
       for key in str7:
           list2.append(str7[key])
       print(max(list2))
  1. 写程序实现replace函数的功能,将指定字符串中指定的旧字符串转换成指定的新字符串
# 方法1:
result = new.join(str1.split(old))
print(result)

# 方法2(自己写逻辑):
index = 0
new_str = ''
len1 = len(str1)
len_old = len(old)
while True:
    if str1[index: index+len_old] == old:
        new_str += new
        index += len_old
    else:
        new_str += str1[index]
        index += 1

    if index >= len1:
        break
print(new_str)
  1. 写程序实现split的功能,将字符串中指定子串作为切割点对字符串进行切割
str1 = 'how are you? and you?'
str2 = 'you'
result = []
item = ''
index = 0
len1 = len(str1)
len2 = len(str2)
while True:
    if str1[index: index+len2] == str2:
        result.append(item)
        item = ''
        index += len2
    else:
        item += str1[index]
        index += 1
    if index >= len1:
        break
result.append(item)
print(result)

10.四大元组的思维导图
在这里插入图片描述

列表基础练习题

选择题

  1. 下列选项中能正确表示一个列表的是()。

    A. {1, 2, 3}

    B. [10, abc, 123]

    C. [10 20 30]

    D. [1, 2, 3]

    解释:D A是集合,B中abc不是字符串,C啥也不是

  2. (多选)已知一个列表nums = [10, 20, '小明', [1, 2]],以下表达式结果是小明的是?()

    A. nums[-3]

    B. nums[3]

    C. nums[-2]

    D. nums[2]

    解释:D 下标为0,1,2,3 or -4,-3,-2,-1对应的为10,20,‘小明’,[1,2]

  3. 以下选项关于列表说法错误的是?()

    A. 列表可以放在for循环的in后面

    B. 列表是可变的序列

    C. 列表是有序的,只支持增删改,不支持查操作

    D. 列表的in操作可以判断元素是否存在

    解释:C 列表是可变的有序的,支持增删改查操作,支持下标操作

  4. 已知一个列表names = ['小乔', '甄姬', '王昭君', '妲己', '女娲', '西施', '嬴政'],下面的表达式中结果是[]的是?()

    A. names[1:]

    B. names[:1]

    C. names[1:4:-1]

    D. names[1:4:2]

    解释:C 开始下标大于结束下标才能用负号

  5. 已知列表list1 = [10, [1, 2], 100, 1000],下列表达式结果是True的是?()

    A. 100 not in list1

    B. 1 in list1

    C. 2 in list1

    D. [1, 2] in list1

    解释:D A是False,100在序列中;B、C是在序列中的序列中

  6. 下列选项中不属于序列的是?()

    A. []

    B. ‘100’

    C. {1, 2}

    D. 100

    解释:D A是列表,B是字符串,C是集合

  7. 已知student = {'name': '小明', 'age': 18, 'gender':'男'}下列关于字典的操作正确的是?()

    A. student('name')

    B. student[name]

    C. student['小明']

    D. student['age']

    解释:D

  8. 下列表达式有误的是?()

    A. 100 + 30.03

    B. 188 * '12'

    C. 188 * 12

    D. 188 + '12'

    解释:D 数字不能和字符串相加减

  9. (多选)下列表达式能产生[1, 2, 3]的是?()

    A. [1, 2] + [3]

    B. [1, 2].append(3)

    C. [1, 2].extend(3)

    D. [1, 2, 3] * 1

    解释:AD B返回值为None,C报错

  10. (多选)下列选项中属于可变序列的是?()

    A. 列表

    B. 元组

    C. 字典

    D. 字符串

    解释:AC 数据类型中只有列表、集合、字典可变,其它不可变

填空题

  1. python中获取指定数据的类型可以使用( type() )函数。
  2. 查看数据地址的函数是( id )。
  3. 如果要将数据转换成列表,需要使用( list() )。
  4. ( count() )函数可以用来获取任意序列中元素的个数。
  5. 如果需要将一个数据插入到列表的中间需要使用函数( insert )。
  6. Python中数学集合运算中求交集、并集、差集和对称差集的符号分别是( & )、( | )、( - )、( ^ )。
  7. 请列举出你知道的Python中的不可变的数据类型:(整型、浮点型、布尔、字符串、元组)。
  8. 获取字符编码值和获取编码值对应的字符的函数分别是( chr(编码值) )、( ord(字符) )。
  9. 如果要判断序列中是否存在某个元素可以使用( in )来判断。
  10. 如果要判断两个数据的地址是否相等可以使用( == )。

编程题

  1. 已知一个列表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)
    
  2. 已知字典dog = {'name': '大黄', 'color': 'yellow', 'age': 3, 'kind': '土狗'}

    1)打印所有key对应的值

    key1 ={i for i in dog}
    print(key1)
    

    2)将name的值修改成 ‘旺财’

    dog['name']='旺财'
    print(dog)
    

    3)添加狗的价格对应的键值对

    dog['price']=1000
    print(dog)
    

    4)清空dog字典

    dog.clear()
    print(dog)
    
  3. 已知字符串message = 'You see see, one day day!'

    1)统计字符串中非字母的字符个数

    count += 0
    for i in message:
        if not i.isalpha():
            count +=1
    print(count)
    

    2)提取出所有的小写字母

    str1 = ''
    for i in message:
        if i.islower():
            str1 += i
    print(str1)
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值