1.作用域+输出语句
执行下面代码,请问输出结果为:顺顺 顺顺
name = “顺顺”#全局变量
def f1():
print(name)
def f2():
name = “丰丰”#局部变量
f1()
f1()
f2()
2.filter()+map()+upper()
在Python3中,下列程序运行结果为:['ABC', 'PYTHON']
strs = ['a', 'ab', 'abc', 'python']
y = filter(lambda s: len(s) > 2, strs)#过滤不符合,返回符合
tmp = list(map(lambda s: s.upper(), y))#使用 list() 转换为列表
print(tmp)
filter(function,iterable)
map(function, iterable, ...)
function -- 判断函数 iterable -- 可迭代对象
upper() 为字符大写转换函数
3.endswith()
下列代码输出为:True
str = "Hello,Python"
suffix = "Python"
print (str.endswith(suffix,2))#判断从2开始的str是否以suffix结尾,结果为True
str.endswith(suffix[, start[, end]])
suffix -- 该参数可以是一个字符串或者是一个元素。
start -- 字符串中的开始位置。
end -- 字符中结束位置。
4.strip()+split()
在Python3中,下列程序结果为:['', 'I', 'like', 'python', ''],'I like python'
strs = ' I like python '
one = strs.split(' ')
two = strs.strip()#删除首尾空格
print(one)
print(two)
str.split(str="", num=string.count(str))
split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num -- 分割次数。默认为 -1, 即分隔所有。
str.strip([chars])
chars -- 移除字符串头尾指定的字符序列。
5.dict()
在Python3中,关于程序运行结果说法正确的是:报错
dicts = {}
dicts[([1, 2])] = 'abc'
print(dicts)
字典的键要求可哈希,而列表list不可哈希,会报错
可哈希对象:数字类型(int,float,bool)、字符串str、元组tuple、自定义类的对象
不可哈希对象:字典dict,列表list,集合set,要使用tuple( iterable )来转换成元组
dicts = {}
dicts[tuple([1, 2])] = 'abc'
print(dicts)
6.range()
Which numbers are printed?(0, 1, 4, 5)
for i in range(2):
print i
for i in range(4,6):
print i
range(start, end[, step])