[持续更新...] 牛客Python专项题

1. [str] 在Python3中,有关字符串的运算结果为:

strs = 'I like python and java'
one = strs.find('n')
print(one)
two = strs.rfind('n')
print(two)
  • 12,12
  • 15,15
  • 12,15
  • None,None

find是从前往后找,返回成功匹配的第一个字符的位置索引
rfind也是从前往后找,但是返回最后一次成功匹配的字符的位置索引

2. [open] 以下哪个代码是正确的读取一个文件?

  • f = open(“test.txt”, “read”)
  • f = open(“r”,“test.txt”)
  • f = open(“test.txt”, “r”)
  • f = open(“read”,“test.txt”)

Python中,打开文件语法为text = oepn(filePath, 操作方式,编码方式)
常见操作方式:

  • ‘r’:读
  • ‘w’:写
  • ‘a’:追加
    常见编码方式:
  • utf-8
  • gbk

3. [list] 在Python3中,对于以下程序正确的是:

lists = [1, 2, 3, 4, 5, 6]
print(lists[6:])
  • 报错
  • []
  • [1,2,3,4,5,6]
  • [6]

注意区分切片,切片一般指创造新的对象,而基于原列表而创造的切片一旦其切片值超出原列表索引下标则会无法从原列表中获取元素,因此返回一个空的下标
切片是浅拷贝,没有索引越界

4. python变量的查找顺序为()

  • 局部作用域>外部嵌套作用域>全局作用域>内置模块作用域
  • 外部嵌套作用域>局部作用域>全局作用域>内置模块作用域
  • 内置模块作用域>局部作用域>外部嵌套作用域>全局作用域
  • 内置模块作用域>外部嵌套作用域>局部作用域>全局作用域

Python变量的作用域遵循LEGB原则,即

  • L:Local,局部作用域,也就是我们在函数中定义的变量;
  • E:Enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部作用域,但不是全局的;
  • G:Globa,全局变量,就是模块级别定义的变量;
  • B:Built-in,系统内置模块里面的变量,比如int, bytearray等。

5. [作用域] 执行以下程序,结果输出为()

a = [1]
b = 2
c = 1


def fn(lis, obj):
    lis.append(b)
    obj = obj + 1
    return lis, obj


fn(a, c)
print(fn(a, c))

  • ([1, 2, 2], 2)
  • ([1, 2, 2], 3)
  • ([1, 2], 2)
  • ([1, 2], 3)

fn(a,c):调用了一次fn函数,这时候a变成了[1,2],c还是1,因为函数虽然返回了obj,但是没有赋值,出了函数之后c还是全局变量的那个c;
print(fn(a,c)):又调用了一次fn函数,过程同上,但是打印的时候打印的是函数的返回值,也就是此时的形参lis和obj的值,而此时lis是[1,2,2],而obj是2。

6. [逻辑运算符] python3中,执行 not 1 and 1的结果为

  • True
  • False
  • 0
  • 1

在Python3中,not 表示逻辑非,and 表示逻辑与,逻辑非(not)的运算优先级大于逻辑与(and)的优先级,则 not 1False,则 not 1 and 1 结果为 False
优先级not>and>or,谐音not at all
not 1 = False
False and 1 = False

7. [str] 在Python3中关于下列字符串程序运行结果为?

str1 = "exam is a example!" 
str2 = "exam" 
print(str1.find(str2, 7))
  • -1
  • 14
  • 0
  • 10

在Python3中 strs.find(str, beg=0, end=len(strs))表示在strs中返回第一次出现str的位置下标,beg表示在strs中的开始索引,默认为0(此题中为7),end结束索引,默认为strs的长度。但需要注意的是,返回的索引还是默认从0开始的,并不是beg的值(beg只是告诉程序是从该字符串第几个索引开始寻找),因此此题的结果为10

8. [str] 执行下列选项的程序,会抛出异常的是()

s1 = 'aabbcc'
s2 = 'abc'
count = s1.count(s2)

if count > 0 :
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

"""---------------------------"""

s1 = 'aabbcc'
s2 = 'abc'
index = s1.index(s2)

if index > -1:
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

"""---------------------------"""

s1 = 'aabbcc'
s2 = 'abc'
find = s1.find(s2)

if find != -1 :
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

"""---------------------------"""

s1 = 'aabbcc'
s2 = 'abc'
if s2 in s1:
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

正确答案为B。

  • count()函数没有匹配到对象返回0
  • index()函数没有匹配到对象报错value Error
  • find()函数没有匹配到对象返回-1
  • in 没有匹配到对象返回False

9. [Python 2] what gets printed? Assuming python version 2.x()

print type(1/2)
  • <type ‘int’>
  • <type ‘number’>
  • <type ‘float’>
  • <type ‘double’>
  • <type ‘tuple’>
  • Python2 中除法默认向下取整,因此 1/2 = 0,为整型。
  • 而 Python3 中的除法为正常除法,会保留小数位,因此 1/2 = 0.5,为浮点型(float)。

10. [class] 有如下类定义,下列描述错误的是?

class A(object):
    pass

class B(A):
    pass

b = B()
  • isinstance(b, A) == True
  • isinstance(b, object) == True
  • issubclass(B, A) == True
  • issubclass(b, B) == True
  • isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()
  • issubclass() 函数用于判断参数是否是类型参数的子类。

11. [排序] 对列表 a = [1,2,3,1,2,4,6] 进行去重后,得到列表b,在不考虑列表元素的排列顺序的前提下,下列方法错误的是()

b = list(set(a))

"""---------------------------"""

b = {
    
    }
b = list(b.fromkeys(a))

"""---------------------------"""

a.sort()
b = []
i = 0

while i < len(a):
    if a[i] not in b:
        b.append(a[i])
    else:
        i += 1
        
"""---------------------------"""

a.sort()
for i in range(len(a)-1):
    if a[i] == a[i+1]:
        a.remove(a[i])
    else:
        continue
b = a        

D选项错误原因在于for循环的计数次数是不变的,但是随着a重复元素不断的移除,会导致列表出现IndexError。

猜你喜欢

转载自blog.csdn.net/weixin_44878336/article/details/125552530