先决条件: 列表 和 多元组 注: 所有这些程序的输出都在Python3上进行了测试
1) 以下程序的输出是什么?
python
L1 = [] L1.append([ 1 , [ 2 , 3 ], 4 ]) L1.extend([ 7 , 8 , 9 ]) print (L1[ 0 ][ 1 ][ 1 ] + L1[ 2 ]) |
a) 类型错误:只能将列表(而不是“int”)连接到列表 b) 十二 c) 11 d) 38
Ans: (c) Explanation: In the print(), indexing is used. L1[0] denotes [1, [2, 3], 4], L1[0][1] denotes [2, 3], L1[0][1][1] = 3 and L1[2] = 8. Thus, the two integers are added, 3 + 8 = 11 and output comes as 11.
2) 以下程序的输出是什么?
python
L1 = [ 1 , 1.33 , 'GFG' , 0 , 'NO' , None , 'G' , True ] val1, val2 = 0 , '' for x in L1: if ( type (x) = = int or type (x) = = float ): val1 + = x elif ( type (x) = = str ): val2 + = x else : break print (val1, val2) |
a) 2 GFGNO b) 2.33 GFGNOG c) 2.33 GFGNONEONGTRUE d) 2.33 GFGNO
Ans: (d) Explanation: val1 will only have integer and floating values val1 = 1 + 1.33 + 0 = 2.33 and val2 will have string values val2 ='GFG' + 'NO' = 'GFGNO'. String 'G' will not be part of val2 as the for loop will break at None, thus 'G' will not be added to val2.
3) 以下程序的输出是什么?
Python3
L1 = [ 1 , 2 , 3 , 4 ] L2 = L1 L3 = L1.copy() L4 = L3 L1[ 0 ] = [ 5 ] print (L1, L2, L3, L4) |
a) [5,2,3,4][5,2,3,4][1,2,3,4][1,2,3,4] b) [5],2,3,4][[5],2,3,4][[5],2,3,4][1,2,3,4] c) [5,2,3,4][5,2,3,4][5,2,3,4][1,2,3,4] d) [5,2,3,4][[5,2,3,4][1,2,3,4][1,2,3,4]
Ans: (d) Explanation: L2 is the reference pointing to the same object as L1, while L3 and L4 are single recursive Copy(Shallow Copy) of List L1. L1[0] = [5], implies that at index 0, list [5] will be present and not integer value 5.
4) 以下程序的输出是什么?
python
import sys L1 = tuple () print (sys.getsizeof(L1), end = " " ) L1 = ( 1 , 2 ) print (sys.getsizeof(L1), end = " " ) L1 = ( 1 , 3 , ( 4 , 5 )) print (sys.getsizeof(L1), end = " " ) L1 = ( 1 , 2 , 3 , 4 , 5 , [ 3 , 4 ], 'p' , '8' , 9.777 , ( 1 , 3 )) print (sys.getsizeof(L1)) |
a) 023100 b) 32 34 35 42 c) 486472128 d) 48 144 192 480
Ans: (c) Explanation: An Empty Tuple has 48 Bytes as Overhead size and each additional element requires 8 Bytes. (1, 2) Size: 48 + 2 * 8 = 64 (1, 3, (4, 5)) Size: 48 + 3 * 8 = 72 (1, 2, 3, 4, 5, [3, 4], 'p', '8', 9.777, (1, 3)) Size: 48 + 10 * 8 = 128
5) 以下程序的输出是什么?
python
T1 = ( 1 ) T2 = ( 3 , 4 ) T1 + = 5 print (T1) print (T1 + T2) |
a) 打字错误 b) (1,5,3,4) c) 1.打字错误 d) 6.打字错误
Ans: (d) Explanation: T1 is an integer while T2 is tuple. Thus T1 will become 1 + 5 = 6. But an integer and tuple cannot be added, it will throw TypeError.
本文由 皮尤斯门战 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。