# F openmyfiletxt w fwritefirst linensecond linen 15

• 19

This preview shows page 7 - 10 out of 19 pages.

f = open("myfile.txt", 'w')f.write("First line.\nSecond line.\n")15.True|False: The following program would write 500 random integernumbers to the output file “integers.txt”.import randomf = open(“integers.txt”, ‘w’)for count in range(200):number = random.randint(1, 500)f.write ( str(number) + ‘\n’)f.close( )16.s = " 391287 "list = s.split( )sum = 0for i in list:sum += int(i)print( sum )The output of the above code will be3916. True|False: In Python, a string representation of an integer number mustbe converted to a number (by using function int ) before it can be used inany computation.17.True|False: In Python, a string representation of an integer number mustbe converted to a number (by using function int ) before it can be used inany computation.18.list1 = [1, 2, 3, 4]list2 = list(range(1, 5))if list1 == list2 : print("same")else : print("diff")The output of the above code will besame(hint: a word)19.list1 = [0, 1, 2, 3, 4, 5, 6, 7]print( len(list1) ) #The output of the above code will be820.list1 = [ 1, 2, 3, 4, 5, 6, 7 ]list2 = list( range(1, 7) )if list1 == list2 :print("same")else :print("diff")The output of the above code will bediff
21.list1 = [ 0, 1, 2, 3, 4, 5, 6, 7 ]print ( list1 )The output of the above code will be622.list1 = [ 0, 1, 2, 3, 4, 5, 6, 7 ]list2 = list1 + list1print ( len(list2) )The output of the above code will be1623.True|False:The statement a += 1 is more efficient thanthe statement a = a + 1even though the final result is the same for these 2 statements.24.list1 = [ 0, 1, 2, 3, 4, 5, 6, 7 ]list2 = list1 + list1list2 += list1print ( len(list2) )The output of the above code will be2425.list1 = [ 0, 1, 2, 3, 4, 5, 6, 7 ]if ( 7 in list1 ) :print( "ok")else:print ("bad" )The output of the above code will beok26.True|False:( X in listX ) is True if X is in listX, False otherwise.27.number =8 * 8 + 12 * 2+ 5if number >= 89 :letter = 'A'elif number >= 79 :letter = 'B'elif number >= 69 :letter = 'C'else :letter = 'F'print ( letter ) # end of programThe output of the above code will beA28.True|False: Python's short-circuit evaluation of logic expressions willmake our programs run much faster .
29.list5 = [0, 1, 2, 3, 4, 5, 6, 7]list5 = 8print( list5 + list5 )The output of the above code will be1430.numbers = [2, 3, 4, 5]for index in range( len(numbers) ):numbers[index] = numbers[index] ** 2print ( numbers + numbers  )4131.list8 = [1, 2, 3, 4, 5]list8.insert(0, 6)print( list8 + list8 )The output of the above code will be532.list9 = [0, 1, 2, 3, 4, 5, 6]list9.append( 7 )print( list9 + list9 )The output of the above code will be1333. defodd ( x ) :if x % 2 == 1:return Trueelse: return False# main program starts hereif odd(5) and odd(8) :print("odd")else: print ("even")The output of the above program will beeven34. The output of the following code will beA90grades = {90:'A', 80:'B', 70:'C', 60:'D', 50:'F'}listV = list(grades.values( ))listK = list(grades.keys( ))print( listV + str( listK ) )35. The output of the following code will beBCgrades = {90:'A', 80:'B', 70:'C', 60:'D', 50:'F'}listG = list(grades.values( ))print( listG + listG )36.The output of the following code will beADgrades = {90:'A', 80:'B', 70:'C', 60:'D', 50:'F'}print ( grades + grades )37.The output of the following code will be150

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 19 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Binary numeral system
• • • 