python学习之路

Python 12

2017-12-28  本文已影响0人  Jack_Hsin

一. 课上代码

>>> a = list()
>>> a
[]
>>> b = 'I love fishc'
>>> b
'I love fishc'
>>> b = list(b)
>>> b
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'f', 'i', 's', 'h', 'c']
>>> c = (1, 1, 2, 3, 5, 8)
>>> c = list(c)
>>> c
[1, 1, 2, 3, 5, 8]
>>> len(a)
0
>>> len(b)
12
>>> max(1, 2, 3, 4, 5)
5
>>> max(b)
'v'
>>> numbers = [1, 18, 13, 0, -98, 34, 56, 76, 32]
>>> max(numbers)
76
>>> min(numbers)
-98
>>> chars = '1234567890'
>>> min(chars)
'0'
>>> tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
>>> max(tuple1)
9
>>> numbers.append('a')
>>> numbers
[1, 18, 13, 0, -98, 34, 56, 76, 32, 'a']
>>> max(numbers)
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    max(numbers)
TypeError: '>' not supported between instances of 'str' and 'int'
#max()只能在同种元素之间返回最大值
>>> tuple2 = (3.1, 2.3, 3.4)
>>> sum(tuple2)
8.8
>>> sum(numbers)
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    sum(numbers)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
#同理,sum()也只能返回int的和
>>> numbers.pop()
'a'
>>> numbers
[1, 18, 13, 0, -98, 34, 56, 76, 32]
>>> sum(numbers)
132
>>> sum(numbers, 8)
140
>>> chars
'1234567890'
>>> sum(chars)
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    sum(chars)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
#sum()不能计算出字符串的和
>>> sorted(numbers)
[-98, 0, 1, 13, 18, 32, 34, 56, 76]
>>> reverse(numbers)
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    reverse(numbers)
NameError: name 'reverse' is not defined
#注意是reversed()不是reverse()

>>> reversed(numbers)
<list_reverseiterator object at 0x0000019F2D750630>
>>> list(reversed(numbers))
[32, 76, 56, 34, -98, 0, 13, 18, 1]
#注意此处用法
>>> enumerate(numbers)
<enumerate object at 0x0000019F2D7ED708>
>>> list(enumerate(numbers))
[(0, 1), (1, 18), (2, 13), (3, 0), (4, -98), (5, 34), (6, 56), (7, 76), (8, 32)]
#给数组加上索引
>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
>>> b = [4, 5, 6, 7, 8]
>>> zip(a, b)
<zip object at 0x0000019F2D7D8FC8>
>>> list(zip(a, b))
[(1, 4), (2, 5), (3, 6), (4, 7), (5, 8)]
>>> 

二. 测试题

  1. 请问分别使用什么BIF,可以把一个可迭代对象转换为列表、元组和字符串?
#list([iterable])把可迭代对象转换为列表
#tuple([iterable])把可迭代对象转换为元组
#str(obj)把对象转换为字符串

三. 动动手

  1. sum()这个BIF有个缺陷,就是如果参数里有字符串类型的话就会报错,请写出一个新的实现过程,自动无视参数里的字符串并返回正确的计算结果
#此题需要好好琢磨一下
def sum(x):
    result = 0
    for i in x:
        if type(i) == int or type(i) == float:
            result = result + i
        else:
            continue
    return result
    
print(sum([False, 1, 2, 3, 4, 5, 6, 7, 'sum', True]))
  1. 写出一个查找用户名分数的程序
#个人代码
name = input("Please input your name: ")
score = [['Jack', 85], ['Rose', 80], ['Kevin', 65], ['Stella', 95], ['Shelly', 90]]
for each in score:
    if name == each[0]:
        print(name + ' score is:', each[1])
        break
if name != each[0]:
    print("Not exit!")
#参考代码
name = input("Please input your name: ")
score = [['Jack', 85], ['Rose', 80], ['Kevin', 65], ['Stella', 95], ['Shelly', 90]]
Isfind = False

for each in score:
    if name in each:
        print(name + ' score is:', each[1])
        Isfind = True
        break
        
if Isfind == False:
    print("Not exit!")
#学会True和False的使用
上一篇下一篇

猜你喜欢

热点阅读