PYTHON测试题

合集下载

python基础测试题

python基础测试题

python基础测试题一、选择题1、以下哪个是 Python 中的合法变量名?()A 123varB var 123C my_varD @var答案:C解析:在 Python 中,变量名只能包含字母、数字和下划线,且不能以数字开头。

选项 A 以数字开头,选项 B 中间有空格,选项 D 以特殊字符@开头,都不符合变量名的规则,只有选项 C 是合法的变量名。

2、以下代码的输出结果是什么?()```pythonx = 5y = 2print(x // y)```B 2C 3D 7答案:B解析:在Python 中,`//`是整除运算符,返回商的整数部分。

5 除以 2 的商为 25,取整数部分为 2,所以输出结果是 2。

3、以下哪个方法可以将字符串转换为整数?()A `str()`B `int()`C `float()`D `complex()`答案:B解析:`int()`函数用于将一个数字或字符串转换为整数。

`str()`函数将对象转换为字符串,`float()`函数将对象转换为浮点数,`complex()`函数用于创建复数。

4、以下哪个是 Python 中用于创建列表的符号?()A `{}`B `()`D `<>`答案:C解析:在 Python 中,使用方括号``来创建列表。

花括号`{}`用于创建字典,小括号`()`用于创建元组等。

5、以下代码的输出结果是什么?()```pythonmy_list = 1, 2, 3, 4, 5print(my_list1:4)```A `2, 3, 4`B `1, 2, 3`C `2, 3, 4, 5`D `1, 2, 3, 4`答案:A解析:列表的切片操作`start:end` 会返回从索引`start` (包含)到索引`end` (不包含)的子列表。

在这个例子中,`my_list1:4` 会返回索引 1 到索引 3 的元素,即`2, 3, 4` 。

二、填空题1、 Python 中的注释可以使用_____(单行注释)和_____(多行注释)。

python考试试卷题目和答案

python考试试卷题目和答案

python考试试卷题目和答案**Python考试试卷题目和答案**一、选择题(每题2分,共20分)1. Python中,以下哪个是正确的变量命名方式?A. 2variableB. variable-nameC. variableNameD. $variable答案:B2. Python中,以下哪个是合法的列表元素?A. [1, 2, 3, 4]B. (1, 2, 3, 4)C. {1, 2, 3, 4}D. <1, 2, 3, 4>答案:A3. 在Python中,以下哪个是正确的字符串?A. 'Hello, World!'B. "Hello, World!"C. `Hello, World!`D. "Hello, World!"答案:A、B、C4. Python中,以下哪个是正确的字典定义方式?A. dict = {1: 'one', 2: 'two'}B. dict = {'one': 1, 'two': 2}C. dict = (1: 'one', 2: 'two')D. dict = [1: 'one', 2: 'two']答案:A、B5. Python中,以下哪个是正确的函数定义?A. def my_function(): passB. my_function = def(): passC. function my_function(): passD. def my_function(): return答案:A6. Python中,以下哪个是正确的条件语句?A. if x > 0:B. if x > 0 then:C. if x > 0 else:D. if x > 0 elif x < 0:答案:A、D7. Python中,以下哪个是正确的循环语句?A. for i in range(10): print(i)B. for i = 0 to 10: print(i)C. while i < 10: print(i)D. while i < 10 do: print(i)答案:A8. Python中,以下哪个是正确的异常处理语句?A. try: pass except: passB. try: pass except Exception: passC. try: pass except error: passD. try: pass except Exception as e: print(e)答案:B、D9. Python中,以下哪个是正确的模块导入方式?A. import moduleB. from module import *C. import * from moduleD. from module import function答案:A、B、D10. Python中,以下哪个是正确的类定义?A. class MyClass: passB. def MyClass: passC. class my_class(): passD. class MyClass(): pass答案:A、D二、填空题(每题2分,共20分)1. Python中的内置数据类型包括整数(int)、浮点数(float)、字符串(str)、_________、_________、_________。

python基础能力测试习题(带答案详解)

python基础能力测试习题(带答案详解)

Python 能力测试题一、选择题1.运行下列程序后,输入了一个数字“100”,并按下Enter 键。

下列说法正确的是( ) 1.a = input("请输入任意内容:") 2.if a == '100':3. print ("你输入的是",a)A 、程序不能运行,出现错误提示信息B 、程序能够运行,没有任何错误信息C 、程序不能运行,出现乱码信息D 、程序能够运行,输出“你输入的是:100”2.下述while 循环体执行的次为( )1.k = 100 2.while k > 1: 3.k= k // 3 A 、6 B 、5 C 、4 D 、33.运行下列程序后,程序输出的结果是( ) 1.sum = 0 2.n = 3 3.for i in range(2, 4): 4.n *= 2 5.sum += n 6.print (sum) A 、9 B 、18 C 、21 D 、364.初始时列表L=[10,13,15,12,14,11],列表中的元素经过一系列位置交换后,最大的元素移动到了列表尾部,位置交换后L=[10,13,12,14,11,15],下述能实现这个功能的代码是( )。

5 5、若元组 y=(‘A ’,‘B ’, ‘C ’, ‘D ’),则能够返回元素‘D ’的语句是( )。

A 、y[0]B 、y[3]C 、y[-2]D 、y[4]6、以下程序的最终打印结果( )。

l = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 ]print ( l [1 : 4] )A 、[1,2,3]B 、[1,2,3,4]C 、[2,3,4]D 、[2,3]A 、for i in range(6):if L[i] < L[i -1]:L[i],L[i -1] = L[i -1],L[i]B 、for i in range(5): if L[i] < L[i -1]: L[i],L[i -1] = L[i -1],L[i]C 、for i in range(6):if L[i] > L[i+1]:L[i],L[i+1] = L[i+1],L[i] D 、for i in range(5): if L[i] > L[i -1]: L[i],L[i+1] = L[i+1],L[i]7、初始时x = 3,x // 2+x**2 运算后的值为()A、7.5B、10C、8D、10.58、下面程序的作用是()。

Python考试模拟题(含参考答案)

Python考试模拟题(含参考答案)

Python考试模拟题(含参考答案)一、单选题(共57题,每题1分,共57分)1.下面代码的输出结果是( )x=[] for num in range(2, 10): if num > 1: for i in range(2, num): if(num % i) != 0: break else: x.append(num) print(x)A、[4,4,8,8]B、[2,3,5,7]C、[2,4,6,8]D、[4,6,6,8]正确答案:D2.关于Python组合数据类型,以下选项中描述错误的是( )A、*Python的str、tuple和list类型都属于序列类型B、序列类型是二维元素向量,元素之间存在先后关系,通过序号访问C、组合数据类型可以分为3类:序列类型、集合类型和映射类型D、*Python组合数据类型能够将多个同类型或不同类型的数据组织起来,通过单一的表示使数据操作更有序、更容易正确答案:B3.字典对象的______________方法返回字典的“值”列表A、items()B、key()C、keys()D、values()正确答案:D4.关于高维数据,以下选项中描述错误的是( )A、高维数据可用于表达一二维数据B、高维数据用来表达索引和数据之间的关系C、高维数据只能表达键值对数据D、“键值对”是高维数据的主要特征正确答案:C5.以下( )类型不可以进行切片操作 ( )A、strB、tupleC、dictD、list正确答案:C6.对于序列s,能够返回序列s中第i到j以k为步长的元素子序列的表达是( )A、s[i; j; k]B、s(i, j, k)C、s[i:j:k]D、s[i, j, k]正确答案:C7.以下关于Python循环结构的描述中,错误的是( )A、Python通过for、while等保留字构建循环结构B、continue只结束本次循环C、break用来结束当前次语句,但不跳出当前的循环体D、遍历循环中的遍历结构可以是字符串、文件、组合数据类型和range()函数正确答案:C8.Python语句:f = open(),以下选项中对f的描述错误的是( )A、将f当作文件对象,f.read()可以读入文件全部信息B、表达式print(f)执行将报错C、*f是一个Python内部变量类型D、*f是文件句柄,用来在程序中表达文件正确答案:B9.给定字典d,以下选项中对d.get(x, y)的描述正确的是( )A、返回字典d中键为y的值,如果不存在,则返回yB、返回字典d中键为x的值,如果不存在,则返回yC、返回字典d中值为y的值,如果不存在,则返回xD、返回字典d中键值对为x:y的值正确答案:B10.对于一个列表aList和一个元组bTuple,以下函数调用错误的选项是( )?A、sorted(aList)B、bTuple.sort()C、aList.sort()D、sorted(bTuple)正确答案:B11.以下程序的输出结果是( ) for i in “the number changes”: ifi == ‘n’: break else: print( i, end= “”)A、the umber chagesB、theC、theumberchagesD、thenumberchanges正确答案:B12.len(“abc”)的长度是3,len(“老师好”)的长度是( )A、6B、1C、9D、3正确答案:D13.关于Python程序中与“缩进”有关的说法中,以下选项中正确的是( )A、缩进在程序中长度统一且强制使用B、缩进统一为4个空格C、缩进可以用在任何语句之后,表示语句间的包含关系D、缩进是非强制性的,仅为了提高代码可读性正确答案:A14.以下选项中可访问字符串s从右侧向左第三个字符的是( )A、s[:-3]B、s[-3]C、s[0:-3]D、s[3]正确答案:B15.下列表达式中返回为True的是 ( )A、(3,2) > (‘a’,‘b’)B、‘abc’ > ‘xyz’C、0x56 > 56D、3 > 2 > 2正确答案:C16.关于 Python 语言的注释,以下选项中描述错误的是( )A、Python 语言的多行注释以 ' ' '(三个单引号)开头和结尾B、Python 语言的单行注释以单引号 ' 开头C、Python 语言的单行注释以#开头D、Python 语言有两种注释方式:单行注释和多行注释正确答案:B17.random.uniform(a,b)的作用是( )A、生成一个[a, b]之间的随机整数B、生成一个(a, b)之间的随机数C、生成一个均值为a,方差为b的正态分布D、生成一个[a, b]之间的随机小数正确答案:D18.以下哪个不属于面向对象的特征( )A、封装B、继承C、多态D、复合正确答案:D19.以下不合法的表达式是A、x-6>5B、e>5 and 4==fC、3=aD、x in [1,2,3,4,5]正确答案:C20.Python 3.x语句 print(1, 2, 3, sep=':' ) 的输出结果是A、123B、1 2 3C、1:2:3D、1,2,3正确答案:C21.语句x=input()执行时,如果从键盘输入12并按回车键,则x的值是A、‘12’B、12.0C、(12)D、12正确答案:A22.以下选项中能够实现Python循环结构的是( )A、whileB、ifC、loopD、do…for正确答案:A23.下列选项中,幂运算的符号为( )A、**B、%C、*D、++正确答案:A24."下面代码的执行结果是( )>>> x = "Happy Birthday to you!" >>> x * 3"A、系统报错B、Happy Birthday to you!C、Happy Birthday to you!Happy Birthday to you!Happy Birthday to you!’D、Happy Birthday to you! Happy Birthday to you! Happy Birthday to you!正确答案:C25.与关系表达式x==0等价的表达式是A、x=0B、x!=1C、xD、not x正确答案:D26.下列表达式的值为True的是A、2!=5 or 0B、5+4j>2-3jC、3>2>2D、1 and 5==0正确答案:A27.Python 3.x 版本的保留字总数是( )A、27B、33C、29D、16正确答案:B28.以下关于函数参数传递的描述,错误的是:A、函数的实参位置可变,需要形参定义和实参调用时都要给出名称B、Python 支持可变数量的参数,实参用”*参数名”表示C、调用函数时,可变数量参数被当做元组类型传递到函数中D、定义函数的时候,可选参数必须写在非可选参数的后面正确答案:B29.以下选项中,符合Python语言变量命名规则的是( )A、TemplistB、(VR)C、!1D、5_1正确答案:A30.以下选项中,不是具体的Python序列类型的是( )A、元组类型B、数组类型D、列表类型C、字符串类型正确答案:B31.Python表达式中,可以控制运算有限顺序的是A、尖括号<>B、大括号{}C、方括号[]D、圆括号()正确答案:D32.选出对下列语句不符合语法要求的表达式: for var in ____________ : print varA、(1,2,3)B、range(0,10)C、{1,2,3,4,5}D、”Hello”正确答案:C33.以下选项中不是文件操作函数或方法的是( )A、readlinesB、readC、loadD、writelines正确答案:C34."当键盘输入”3”的时候,以下程序的输出结果是( ) r = input("请输入半径:")Ar = 3.1415 * r *r print("{:.0f}".format(ar))"A、28B、28.27C、29D、Type Error正确答案:D35.以下程序的输出结果是: n=5 while n>2: print(n) n=n-1A、5 4 3;B、5C、5 4 3 2 1D、5 4 3正确答案:D36.以下选项中描述正确的是( )A、条件24<=28<25是合法的,且输出为FalseB、条件35<=45<75是合法的,且输出为FalseC、条件24<=28<25是不合法的D、条件24<=28<25是合法的,且输出为True正确答案:A37.以下选项中可用作Python标识符的是( )A、3B9909B、classC、___D、it’s正确答案:C38.以下不能创建一个字典的语句是( )A、dict = {(4,5,6):‘dictionary’}B、dict= {4:6}C、dict = {}D、dict = {[4,5,6]:‘dictionary’}正确答案:D39.关于Python的元组类型,以下选项中描述错误的是( )A、元组一旦创建就不能被修改B、一个元组可以作为另一个元组的元素,可以采用多级索引获取信息C、Python中元组采用逗号和圆括号(可选)来表示D、元组中元素不可以是不同类型正确答案:D40.Python定义私有变量的方法为( )。

python简答题及答案

python简答题及答案

python简答题及答案python测试题及答案,python简答题及答案一、选择题(每题2分,共20分)1.下面哪个语句在Python中是非法的?(二)a、x=y=z=1 B、x=(y=z 1)c、x、y=y,x D、x=y?x=x y2.关于Python内存管理,下列说法错误的是(B)a、变量不用提前声明B、变量不用先创建赋值直接使用。

c,变量不需要指定类型D,可以用del释放资源3.3 .打印100-25 * 3% 4输出应该是什么?(二)A.1B.97C.25D.04.以下哪一项不是Python (B)的合法标识符a、int32 B、40XL C、self D 、__name__5.下列哪个陈述是错误的(A)除了字典类型,所有标准对象都可以用于布尔测试。

b,空字符串的布尔值为假c,空列表对象的布尔值为假D.任何数值为0的数字对象的布尔值都是假的。

6.下列表达式中正确的值是(C)5 4j 2-3j B、3221==1和2!=1?d、not(1==1和0!=1)7.Python不支持的数据类型有(A)a、char B、int C、float D、list8.下列不能创建词典的说法是(C)a、字典1={} B、字典2={ 3 : 5 }c、dict3=dict([2,5],[ 3,4 ])d、dict4=dict(([1,2],[3,4])9.下列不能创建集合的说法是(C)a、s1=set () B、s2=set ("abcd ")c、s3=(1,2,3,4) D、s4=frozenset((3,2,1))10.下列Python语句是正确的(D)a、min=x if x y else y B、max=x y and?x : yc、if (x y) print x D、while True :通过二填空(每空一分,共10分)设l=[a , b , c , d , e , f , g],则L[3]的值为_ d _ _?l [:5]的值是_ [d , e] _ l [33605]是_ [a ,b , c , d , e] _ l [:]是_ [在Python中,可变数据类型是_ _ list _ _和_ _ dict _ _,不可变数据类型是_ _ int _ _和_ _ str _ _三、简答题(每题5分,共45分)1声明变量有什么注意事项?答:1.由字母、数字、下划线组成,不能以数字开头;2.不能用关键词。

大学python考试题目及答案

大学python考试题目及答案

大学python考试题目及答案一、选择题(每题2分,共20分)1. Python中,以下哪个关键字用于定义一个函数?A. defB. functionC. funcD. define答案:A2. 在Python中,以下哪个数据结构用于存储一系列元素?A. ListB. SetC. DictionaryD. Tuple答案:A3. 以下哪个选项是Python中的逻辑运算符?A. andB. orC. notD. All of the above答案:D4. Python中,以下哪个函数用于计算字符串的长度?A. len()B. length()C. size()D. count()答案:A5. 在Python中,以下哪个关键字用于创建一个循环?A. whileB. loopC. repeatD. iterate答案:A6. 以下哪个选项是Python中用于异常处理的关键字?A. tryB. catchC. errorD. except答案:A7. Python中,以下哪个函数用于将字符串转换为大写?A. upper()B. capitalize()C. title()D. swapcase()答案:A8. 在Python中,以下哪个关键字用于定义一个类?A. classB. typeC. defineD. struct答案:A9. 以下哪个选项是Python中的布尔值?A. TrueB. falseC. TrueD. None答案:A10. Python中,以下哪个函数用于生成一个随机数?A. random()B. rand()C. generate()D. randomize()答案:A二、填空题(每题2分,共20分)1. 在Python中,使用______关键字可以定义一个空集合。

答案:set2. Python中的列表推导式是一种从其他可迭代对象创建______的简洁方式。

答案:list3. 使用______函数可以将字符串中的所有字符转换为小写。

Python练习题(附答案)

Python练习题(附答案)一、单选题(共57题,每题1分,共57分)1.Python中,用于获取用户输入的命令为( )A、inputB、readC、getD、for正确答案:A2.关于Python的分支结构,以下选项中描述错误的是( )A、分支结构使用if保留字B、Python中if-elif-else语句描述多分支结构C、分支结构可以向已经执行过的语句部分跳转D、Python中if-else语句用来形成二分支结构正确答案:C3."给出如下代码TempStr ="Hello World" 可以输出“World”子串的是( )"A、print(TempStr[–5:0])B、print(TempStr[–5:])C、print(TempStr[–5: –1])D、print(TempStr[–4: –1])正确答案:B4.以下不能创建一个字典的语句是 ( )A、dict3 = {[1,2,(2,3)]: “uestc”}B、dict2 = { 3 : 5 }C、dict1 = {}D、dict4 = {(1,2,3): “uestc”}正确答案:A5.关于lambda函数,以下选项中描述错误的是( )A、lambda函数也称为匿名函数B、lambda函数将函数名作为函数结果返回C、定义了一种特殊的函数D、lambda不是Python的保留字正确答案:D6.下列说法中正确的是( )。

A、continue能结束循环,而break只能结束本次循环B、break能结束循环,而continue只能结束本次循环C、break用在for语句中,而continue用在while语句中D、break用在while语句中,而continue用在for语句中正确答案:B7.关于函数的返回值,以下选项中描述错误的是( )A、函数可以有return,也可以没有B、return可以传递0个返回值,也可以传递任意多个返回值C、函数可以返回0个或多个结果D、函数必须有返回值正确答案:D8.下列不合法的Python变量名是( )A、Python2B、Hello_WorldC、N.xD、sum正确答案:C9.下列表达式的值为True的是( )A、not(1==1 and 0!=1)B、1==1 and 2!=1C、3>2>2D、(2**=3)<(2*=3)正确答案:B10.关于Python循环结构,以下选项中描述错误的是( )A、遍历循环中的遍历结构可以是字符串、文件、组合数据类型和range()函数等B、continue用来结束当前当次语句,但不跳出当前的循环体C、Python通过for、while等保留字构建循环结构D、continue结束整个循环过程,不再判断循环的执行条件正确答案:D11.在Python函数中,用于获取用户输入的是( )A、get()B、input()C、Eval()D、print()正确答案:B12.面代码的执行结果是( ) print(pow(3,0.5)*pow(3,0.5)==3)A、pow(3,0.5)*pow(3,0.5)==3B、FalseC、3D、True正确答案:B13.优先级最高的运算符为( )。

Python模拟习题(附答案)

Python模拟习题(附答案)一、单选题(共57题,每题1分,共57分)1.下列不合法的Python变量名是( )A、Hello$WorldB、N_xC、Python2D、sum正确答案:A2.以下选项不属于 Python 整数类型的是( )A、十进制B、二进制C、十二进制D、八进制正确答案:C3.以下选项中,不是Python IDE的是( ) -A、PyCharm -B、Jupyter Notebook-C、Spyder-D、R studio正确答案:D4.使用( )关键字来创建python自定义函数。

A、functionB、funcC、procedureD、def正确答案:D5.以下语句的运行结果是>>>Python = “ Python”>>> print (“ study” + Python)A、语法错误B、“study“PythonC、study PythonD、studyPython正确答案:C6.下列( )语句在Python中是非法的A、x = (y = z + 1)B、x, y = y, xC、x += yD、x = y = z = 1正确答案:A7.关于列表数据结构,下面描述正确的是( )A、不支持 in 运算符B、必须按顺序插入元素C、可以不按顺序查找元素D、所有元素类型必须相同正确答案:C8.以下选项中,对CSV格式的描述正确的是( )A、CSV文件以英文特殊符号分隔元素B、CSV文件以英文逗号分隔元素C、CSV文件以英文空格分隔元素D、CSV文件以英文分号分隔元素正确答案:B9.关于Python的无限循环,以下选项中描述错误的是( )A、无限循环通过while保留字构建B、无限循环也称为条件循环C、无限循环一直保持循环操作,直到循环条件不满足才结束D、无限循环需要提前确定循环次数正确答案:D10."下面代码实现的功能描述为( )Def fact(n): if n==0: return 1 else:Return n*fact(n-1)Num =eval(input("请输入一个整数:")) print(fact(abs(int(num))))"A、接受用户输入的整数N,输出N的阶乘值B、接受用户输入的整数N,判断N是否是素数并输出结论C、接受用户输入的整数N,判断N是否是水仙花数D、接受用户输入的整数N,判断N是否是完数并输出结论正确答案:A11.下列函数中,用于返回元组中元素最小值的是( )A、minB、maxD、len正确答案:A12.关于函数的关键字参数使用限制,以下选项中描述错误的是( )A、关键字参数必须位于位置参数之前B、关键字参数顺序无限制C、不得重复提供实际参数D、关键字参数必须位于位置参数之后正确答案:A13.给出下面代码: k=10000 while k>1: print(k) k=k/2 上述程序的运行次数是( )A、1000B、14C、13D、15正确答案:B14.字符串是一个字符序列,例如,字符串s,从右侧向左第2个字符用( )索引?A、s[0:-2]B、s[2]C、s[:-2]D、s[-2]正确答案:D15.下列运算符的使用错误的是( )A、-10 % -3B、3 * ‘abc’C、[1, 2, 3] + [4, 5, 6]D、1 + ‘a’正确答案:D16.下面代码的执行结果是( )>>> def area(r, pi = 3.14159): return pi * r * r>>> area(3.14, 4)A、39.4384B、出错C、50.24正确答案:A17."下面代码的输出结果是( ) for a in ‘mirror’: print(a, end="") if a == ‘r’: break"A、miB、mirrorC、mirD、Mirror正确答案:C18.使用( )关键字声明匿名函数A、funcB、functionC、defD、lambda正确答案:D19.下面代码的输出结果是>>> hex(255)A、‘0bff’B、‘0xff’C、'0offD、'0eff正确答案:B20.已知x=2,语句x*=x+1执行后,x的值是( )A、6B、3C、2D、4正确答案:A21.已知x=10,y=20,z=30;以下语句执行后x,y,z的值是()。

(完整版)Python测试题

Python测试题一、填空题1.Python使用符号# 标示注释;以缩进对齐划分语句块。

2、Python序列类型包括字符串、列表、元组三种;字典是Python中唯一的映射类型。

3、Python中的可变数据类型有列表和字典,不可变数据类型有字符串、数字、元组。

4、Python的数字类型分为整数、长整数、浮点、复数等子类型。

5、Python提供了两个对象身份比较操作符is 和is not 来测试两个变量是否指向同一个对象,也可以通过内建函数type() 来测试对象的类型。

6、设s=‘abcdefg’,则s[3]值是‘d’,s[3:5]值是‘de’,s[:5]值是‘abcdf’,s[3:]值是‘defg’,s[ : :2]值是‘aceg’,s[::-1]值是‘gfedcba’,s[-2:-5]值是‘’。

二、选择题1.下列哪个语句在Python中是非法的?()A、x = y = z = 1B、x = (y = z + 1)C、x, y = y, xD、x += y2.关于Python内存管理,下列说法错误的是()A、变量不必事先声明B、变量无须先创建和赋值而直接使用C、变量无须指定类型D、可以使用del释放资源3、下面哪个不是Python合法的标识符()A、int32B、40XLC、selfD、__name__4、下列哪种说法是错误的()A、除字典类型外,所有标准对象均可以用于布尔测试B、空字符串的布尔值是FalseC、空列表对象的布尔值是FalseD、值为0的任何数字对象的布尔值是False5、下列表达式的值为True的是()A、5+4j > 2-3jB、3>2>2C、(3,2)< (‘a’,’b’)D、’abc’ > ‘xyz’6、Python不支持的数据类型有()A、charB、intC、floatD、list7、关于Python中的复数,下列说法错误的是()A、表示复数的语法是real + image jB、实部和虚部都是浮点数C、虚部必须后缀j,且必须是小写D、方法conjugate返回复数的共轭复数8、关于字符串下列说法错误的是()A、字符应该视为长度为1的字符串B、字符串以\0标志字符串的结束C、既可以用单引号,也可以用双引号创建字符串D、在三引号字符串中可以包含换行回车等特殊字符9、以下不能创建一个字典的语句是()A、dict1 = {}B、dict2 = { 3 : 5 }C、dict3 = {[1,2,3]: “uestc”}D、dict4 = {(1,2,3): “uestc”}10、下列Python语句正确的是()A、min = x if x < y else yB、max = x > y ? x : yC、if (x > y) print xD、while True : pass三、编程题.1、Python如何定义一个函数,并试写一个函数,给定n,返回n以内的斐波那契数列。

python考试题目精品练习

python考试题目精品练习一、选择题1.在python中,运行下列程序,正确的结果是()x=1while x>0:x=x+1print( x)A.1 B.1 2 3 4 5 6 7 8 9…………………C.无输出D.1002.如下Python程序段x = 0while x < 50:x = (x + 2) * (x + 3)运行后,x的值为()A.0 B.72 C.50 D.168 3.Python语言属于()A.自然语言B.汇编语言C.机器语言D.高级语言4.Python中,赋值语句,“c=c-b”等价于()A.b-=cB.c-b=cC.c-=bD.c==c-b5.已知a = 6,b = -4,则Python表达式 a / 2 + b % 2 * 3 的值为()A.3 B.3.0 C.5 D.5.0 6.下列哪个语句在Python中是非法的?()A.x = y = z = 1 B.x = (y = z + 1)C.x, y = y, x D.x += y7.下列选项中,合法的Python变量名是()A.print B.speed C. D.a#2 8.关于python程序设计语言,下列说法不正确的是( )。

A.python源文件以***.py为扩展名B.python的默认交互提示符是:>>>C.python只能在文件模式中编写代码D.python具有丰富和强大的模块9.在python中,运行以下程序,结果应为()a=5b=7b+=3a=b*20a+=2a=a%bprint(a,b)A.5 7 B.20 10 C.22 7 D.2 10 10.Python中,保留字写法正确的是()A.PRINT()B.Print()C.print()D.Int()11.在python中,想输出一行文字,要用到的函数是()。

A.input()B.int()C.print()D.float()12.为了提高程序的可读性,可以在该语句后面添加注释语句,Python程序中用作注释的标识符是()A.:B.# C., D.!13.在Python语言中,用来定义函数的关键字是()。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.what does the following code do?(B)def a(b, c, d): passA.defines a list and initializes itB.defines a function, which does nothingC.defines a function, which passes its parameters throughD.defines an empty class2.what gets printed? Assuming python version 2.x(A) print type(1/2)A.<type 'int'>B.<type 'number'>C.<type 'float'>D.<type 'double'>E.<type 'tuple'>3. what is the output of the following code?(E)print type([1,2])A.<type 'tuple'>B.<type 'int'>C.<type 'set'>D.<type 'complex'>E.<type 'list'>4. what gets printed?(C)def f(): passprint type(f())A.<type 'function'>B.<type 'tuple'>C.<type 'NoneType'>D.<type 'str'>E.<type 'type'>5. what should the below code print?(A)print type(1J)A.<type 'complex'>B.<type 'unicode'>C.<type 'int'>D.<type 'float'>E.<type 'dict'>6. what is the output of the following code?(D)print type(lambda:None)A.<type 'NoneType'>B.<type 'tuple'>C.<type 'type'>D.<type 'function'>E.<type 'bool'>7. what is the output of the below program?(D)a = [1,2,3,None,(),[],]print len(a)A.syntax errorB.4C.5D.6E.78.what gets printed? Assuming python version 3.x(C) print (type(1/2))A.<type 'int'>B.<type 'number'>C.<type 'float'>D.<type 'double'>E.<type 'tuple'>9. What gets printed?(C)d = lambda p: p * 2t = lambda p: p * 3x = 2x = d(x)x = t(x)x = d(x)print xA.7B.12C.24D.36E.4810. What gets printed?(A) x = 4.5y = 2print x//yA.2.0B.2.25C.9.0D.20.25E.2111. What gets printed?(C) nums = set([1,1,2,3,3,3,4]) print len(nums)A.1B.2C.4D.5E.712. What gets printed?(A) x = Truey = Falsez = Falseif x or y and z:print "yes"else:print "no"A.yesB.noC.fails to compile13. What gets printed?(C) x = Truey = Falsez = Falseif not x or y:print 1elif not x or not y and z:print 2elif not x or y or not y and x:print 3else:print 4A.1B.2C.3D.414. If PYTHONPATH is set in the environment, which directories are searched for modules?(D)A) PYTHONPATH directoryB) current directoryC) home directoryD) installation dependent default pathA.A onlyB.A and DC.A, B, and CD.A, B, and DE.A, B, C, and D15. In python 2.6 or earlier, the code will print error type 1 if accessSecureSystem raises an exception of either AccessError type or SecurityError type(B)try:accessSecureSystem()except AccessError, SecurityError:print "error type 1"continueWork()A.trueB.false16. The following code will successfully print the days and then the months(B) daysOfWeek = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']months = ['Jan', \'Feb', \'Mar', \'Apr', \'May', \'Jun', \'Jul', \'Aug', \'Sep', \'Oct', \'Nov', \'Dec']print "DAYS: %s, MONTHS %s" %(daysOfWeek, months)A.trueB.false17. Assuming python 2.6 what gets printed?(A)f = Nonefor i in range (5):with open("data.txt", "w") as f:if i > 2:breakprint f.closedA.TrueB.FalseC.None18. What gets printed?(C)counter = 1def doLotsOfStuff():global counterfor i in (1, 2, 3):counter += 1doLotsOfStuff()print counterA.1B.3C.4D.7E.none of the above19. What gets printed?(C)print r"\nwoow"A.new line then the string: woowB.the text exactly like this: r"\nwoow"C.the text like exactly like this: \nwoowD.the letter r and then newline then the text: woowE.the letter r then the text like this: nwoow20.What gets printed?(B)print "hello" 'world'A.on one line the text: hello worldB.on one line the text: helloworldC.hello on one line and world on the next lineD.syntax error, this python program will not run21.What gets printed?(E)print "\x48\x49!"A.\x48\x49!B.4849C.4849!D. 48 49!E.HI!22. What gets printed?(D)print 0xA + 0xaA.0xA + 0xaB.0xA 0xaC.14D.20E.0x2023. What gets printed?(E)class parent:def __init__(self, param):self.v1 = paramclass child(parent):def __init__(self, param):self.v2 = paramobj = child(11)print "%d %d" % (obj.v1, obj.v2)A.None NoneB.None 11C.11 NoneD.11 11E.Error is generated by program24. What gets printed?(E)kvps = {"user","bill", "password","hillary"} print kvps['password']erB.billC.passwordD.hillaryE.Nothing. Python syntax error25. What gets printed?(B)66% on 1871 times askedclass Account:def __init__(self, id):self.id = idid = 666acc = Account(123)print acc.idA.NoneB.123C.666D.SyntaxError, this program will not run26. What gets printed?(C)name = "snow storm"print "%s" % name[6:8]A.stB.stoC.toD.torE.Syntax Error27. What gets printed?(D)name = "snow storm"name[5] = 'X'print nameA.snow stormB.snowXstormC.snow XtormD.ERROR, this code will not run28. Which numbers are printed?(C)for i in range(2):print ifor i in range(4,6):print iA.2, 4, 6B.0, 1, 2, 4, 5, 6C.0, 1, 4, 5D.0, 1, 4, 5, 6, 7, 8, 9E.1, 2, 4, 5, 629. What sequence of numbers is printed?(B) values = [1, 2, 1, 3]nums = set(values)def checkit(num):if num in nums:return Trueelse:return Falsefor i in filter(checkit, values):print iA.1 2 3B.1 2 1 3C.1 2 1 3 1 2 1 3D.1 1 1 1 2 2 3 3E.Syntax Error30. What sequence of numbers is printed?(E) values = [2, 3, 2, 4]def my_transformation(num):return num ** 2for i in map(my_transformation, values): print iA.2 3 2 4B.4 6 4 8C.1 1.5 1 2D.1 1 1 2E.4 9 4 1631. What numbers get printed(C)import pickleclass account:def __init__(self, id, balance):self.id = idself.balance = balancedef deposit(self, amount):self.balance += amountdef withdraw(self, amount):self.balance -= amountmyac = account('123', 100)myac.deposit(800)myac.withdraw(500)fd = open( "archive", "w" )pickle.dump( myac, fd)fd.close()myac.deposit(200)print myac.balancefd = open( "archive", "r" )myac = pickle.load( fd )fd.close()print myac.balanceA.500 300B.500 500C.600 400D.600 600E.300 50032. What gets printed by the code snippet below?(B) import mathprint math.floor(5.5)A.5B.5.0C.5.5D.6E.6.033. What gets printed by the code below?(E) class Person:def __init__(self, id):self.id = idobama = Person(100)obama.__dict__['age'] = 49print obama.age + len(obama.__dict__)A.1B.2C.49D.50E.5134. What gets printed?(E)x = "foo "y = 2print x + yA.fooB.foo fooC.foo 2D.2E.An exception is thrown35. What gets printed?(E)def simpleFunction():"This is a cool simple function that returns 1"return 1print simpleFunction.__doc__[10:14]A.simpleFunctionB.simpleC.funcD.funtionE.cool36. What does the code below do?(C)sys.path.append('/root/mods')A.Changes the location that the python executable is run fromB.Changes the current working directoryC.Adds a new directory to seach for python modules that are importedD.Removes all directories for modsE.Changes the location where sub-processes are searched for after they are launched37. What gets printed?(C)import resum = 0pattern = 'back'if re.match(pattern, 'backup.txt'):sum += 1if re.match(pattern, 'text.back'):sum += 2if re.search(pattern, 'backup.txt'):sum += 4if re.search(pattern, 'text.back'):sum += 8print sumA.3B.7C.13D.14E.1538. Which of the following print statements will print all the names in the list on a seperate line(A)names = ['Ramesh', 'Rajesh', 'Roger', 'Ivan', 'Nico']A.print "\n".join(names)B.print names.join("\n")C.print names.concatenate("\n")D.print names.append("\n")E.print names.join("%s\n", names)39. True or false? Code indentation must be 4 spaces when creating a code block?(B) if error:# four spaces of indent are used to create the blockprint "%s" % msgA.TrueB.False40. Assuming the filename for the code below is /usr/lib/python/person.pyand the program is run as:python /usr/lib/python/person.pyWhat gets printed?(D)class Person:def __init__(self):passdef getAge(self):print __name__p = Person()p.getAge()A.PersonB.getAger.lib.python.personD.__main__E.An exception is thrown41. What gets printed(B)foo = {}print type(foo)A.setB.dictD.tupleE.object42. What gets printed?(C)foo = (3, 4, 5)print type(foo)A.intB.listC.tupleD.dictE.set43. What gets printed?(D)country_counter = {}def addone(country):if country in country_counter:country_counter[country] += 1 else:country_counter[country] = 1 addone('China')addone('Japan')addone('china')print len(country_counter)A.0B.1C.2D.3E.444. What gets printed?(D)confusion = {}confusion[1] = 1confusion['1'] = 2confusion[1] += 1for k in confusion:sum += confusion[k]print sumA.1B.2C.3D.4E.545. What gets printed?(C)confusion = {} confusion[1] = 1 confusion['1'] = 2 confusion[1.0] = 4sum = 0for k in confusion:sum += confusion[k]print sumA.2B.4C.6D.7E.An exception is thrown46.What gets printed?(E)boxes = {}jars = {}crates = {}boxes['cereal'] = 1 boxes['candy'] = 2jars['honey'] = 4crates['boxes'] = boxes crates['jars'] = jarsprint len(crates[boxes])B.2C.4D.7E.An exception is thrown47. What gets printed?(E)numberGames = {} numberGames[(1,2,4)] = 8 numberGames[(4,2,1)] = 10 numberGames[(1,2)] = 12sum = 0for k in numberGames:sum += numberGames[k]print len(numberGames) + sumA.8B.12C.24D.30E.3348. What gets printed?(A)foo = {1:'1', 2:'2', 3:'3'}foo = {}print len(foo)A.0B.1C.2D.3E.An exception is thrown49. What gets printed?(B)foo = {1:'1', 2:'2', 3:'3'}del foo[1]foo[1] = '10'del foo[2]print len(foo)B.2C.3D.4E.An exception is thrown50. What gets printed?(E)names = ['Amir', 'Barry', 'Chales', 'Dao'] print names[-1][-1]A.AB.rC.AmirD.DaoE.o51. What gets printed?(B)names1 = ['Amir', 'Barry', 'Chales', 'Dao'] names2 = names1names3 = names1[:]names2[0] = 'Alice'names3[1] = 'Bob'sum = 0for ls in (names1, names2, names3): if ls[0] == 'Alice':sum += 1if ls[1] == 'Bob':sum += 10print sumA.11B.12C.21D.22E.3352. What gets printed?(E)names1 = ['Amir', 'Barry', 'Chales', 'Dao']loc = names1.index("Edward")print locA.-1B.0C.4D.EdwardE.An exception is thrown53. What gets printed?(B)names1 = ['Amir', 'Barry', 'Chales', 'Dao']if 'amir' in names1:print 1else:print 2A.1B.2C.An exception is thrown54. What gets printed?(C)names1 = ['Amir', 'Barry', 'Chales', 'Dao'] names2 = [name.lower() for name in names1] print names2[2][0]A.iB.aC.cD.CE.An exception is thrown55. What gets printed?(B)numbers = [1, 2, 3, 4]numbers.append([5,6,7,8])print len(numbers)A.4B.5C.8D.12E.An exception is thrown56. Which of the following data structures can be used with the "in" operator to check if an item is in the data structure?(E)A.listB.setC.dictionaryD.None of the aboveE.All of the above57. Wat gets printed?(D)list1 = [1, 2, 3, 4]list2 = [5, 6, 7, 8]print len(list1 + list2)A.2B.4C.5D.8E.An exception is thrown58. What gets printed?(C)def addItem(listParam):listParam += [1]mylist = [1, 2, 3, 4]addItem(mylist)print len(mylist)A.1B.4C.5D.8E.An exception is thrown59. What gets printed?(E)my_tuple = (1, 2, 3, 4)my_tuple.append( (5, 6, 7) )print len(my_tuple)A.1B.2C.5D.7E.An exception is thrown60. What gets printed?(B)a = 1b = 2a,b = b,aprint "%d %d" % (a,b)A.1 2B.2 1C.An exception is thrownD.This program has undefined behavior61. What gets printed?(A)def print_header(str):print "+++%s+++" % strprint_header.category = 1print_header.text = "some info"print_header("%d %s" % \(print_header.category, print_header.text))A.+++1 some info+++B.+++%s+++C.1D.some info62. What gets printed?(C)def dostuff(param1, *param2):print type(param2)dostuff('apples', 'bananas', 'cherry', 'dates')A.strB.intC.tupleD.listE.dict63. What gets printed?(E)def dostuff(param1, **param2):print type(param2)dostuff('capitals', Arizona='Phoenix',California='Sacramento', Texas='Austin')A.inB.strC.tupleD.listE.dict64. What gets printed?(B)def myfunc(x, y, z, a):print x + ynums = [1, 2, 3, 4]myfunc(*nums)A.1B.3C.6D.10E.An exception is thrown65. How do you create a package so that the following reference will work?(C) p = mytools.myparser.MyParser()A.Declare the myparser package in mytools.pyB.Create an __init__.py in the home dirC.Inside the mytools dir create a __init__.pyD.Create a myparser.py directory inside the mytools directoryE.This can not be done66. What gets printed?(E)class A:def __init__(self, a, b, c):self.x = a + b + ca = A(1,2,3)b = getattr(a, 'x')setattr(a, 'x', b+1)print a.xA.1B.2C.3D.6E.767. What gets printed?(E)class NumFactory:def __init__(self, n):self.val = ndef timesTwo(self):self.val *= 2def plusTwo(self):self.val += 2f = NumFactory(2)for m in dir(f):mthd = getattr(f,m)if callable(mthd):mthd()print f.valA.2B.4C.6E.An exception is thrown68. What gets printed?(A)one = chr(104)two = chr(105)print "%s%s" % (one, two)A.hiB.hC.Inside the mytools dir create a __init__.py and myparser.pyD.104105E.10469. What gets printed?(A)x = 0y = 1a = cmp(x,y)if a < x:print "a"elif a == x:print "b"else:print "c"A.aB.bC.c70. What gets printed?(C)x = 1y = "2"z = 3sum = 0for i in (x,y,z):if isinstance(i, int):sum += iprint sumB.3C.4D.6E.An exception is thrown71. What gets printed (with python version 2.X) assuming the user enters the following at the prompt?(D)#: fooa = input("#: ")print aA.fB.fooC.#: fooD.An exception is thrown72. What gets printed?(C)x = sum(range(5))print xA.4B.5C.10D.15E.An exception is thrown73. If the user types '0' at the prompt what gets printed?(B)def getinput():print "0: start"print "1: stop"print "2: reset"x = raw_input("selection: ")try:num = int(x)if num > 2 or num < 0:return Nonereturn numexcept:return Nonenum = getinput()if not num:print "invalid"else:print "valid"A.validB.invalidC.An exception is thrown74. What gets printed?(D) kvps = { '1' : 1, '2' : 2 } theCopy = kvpskvps['1'] = 5sum = kvps['1'] + theCopy['1'] print sumA.1B.2C.7D.10E.An exception is thrown75. What gets printed?(C) kvps = { '1' : 1, '2' : 2 } theCopy = kvps.copy()kvps['1'] = 5sum = kvps['1'] + theCopy['1'] print sumA.1B.2C.6D.10E.An exception is thrown76. What gets printed(D)aList = [1,2]bList = [3,4]kvps = { '1' : aList, '2' : bList } theCopy = kvps.copy()kvps['1'][0] = 5sum = kvps['1'][0] + theCopy['1'][0] print sumA.1B.2C.6D.10E.An exception is thrown77. What gets printed?(C)import copyaList = [1,2]bList = [3,4]kvps = { '1' : aList, '2' : bList } theCopy = copy.deepcopy(kvps) kvps['1'][0] = 5sum = kvps['1'][0] + theCopy['1'][0] print sumA.1B.2C.6D.10E.An exception is thrown78. What gets printed?(C)kvps = { '1' : 1, '2' : 2 }theCopy = dict(kvps)kvps['1'] = 5sum = kvps['1'] + theCopy['1']print sumA.1B.2C.6D.10E.An exception is thrown79. What gets printed?(B)kvps = { '1' : 1, '2' : 2 , '3' : 3, '4' : 4, '5' : 5}newData = { '1' : 10, '3' : 30 }kvps.update(newData)x = sum(kvps.values())print xA.15B.51C.150D.An exception is thrown80. What gets printed (with python version 3.X) assuming the user enters the following at the prompt?(B)#: fooa = input("#: ")print aA.fB.fooC.Not a numberD.An exception is thrown。

相关文档
最新文档