Professional Documents
Culture Documents
7 Python Chapter VII
7 Python Chapter VII
[['few", ‘words']}, this is a list with one clement, a list. () True. As Oth element of L[2: 3} list isa list [“few”, “words”] which hasa member “few" init. (¢) True. £{2] returns the list [‘few’, ‘words'], “few () L[2] = (Few, words’) an clement of this list. 12] U1] + U2) = [are’, ‘2, “fev, ‘words! 5. Lisanon-emply list of ints, Print the smallest and largest integer in L. Write the code without using a lop. Solution. length = len(L) Lesort() print ("smallest :*, print (“largest » L[@]) Lpength-a})7 LIST MANIPULATION ite the most appropriate Tist method to perform the following task. (a) Delete a given element from the tist, () Delete 3ril element from the list. (@) Addit an element in the end of the Hist, (d) Add an element in the beginning of the list. (2) Adi elements of list in the end of atist gatution. (@) Femove() (b) pop() (c) append) (d) insert() (¢) extend( ) 4, Given alist of integers, Ly write code to add the integers and display the sum. solution. pos = @ # start of list sum = @ # initial sum while pos < len (L) : # loop through entire list sum = sum + L(pos] # add current item to sun pos = pos +1 Hf move to next item in ilst print (sum ) # the answer , Given a list of integers, L, write code to calculnte and display the sum of all the odd numbers in the list. Solution. pos =@ # start of List sun=0 # initially no sun while pos < len (L) : # loop through List if L[pos] ¥2==1: # this is an odd nunber sum = sum + L{pos] # so add it pos = pos +1 next item in List print (sum ) # the answer 9, Examine the following code : Y nunlist = eval(input( "Enter list:")) pos = 0 odds = evens = @ Jength = len (num list) while pos < length : if numlist(pos] %2 == 0: evens = evens +1 else : odds = odds +1 Pos = pos +1 if odds > evens : print ("Balanced oddity") (2) What is this program calculating? (b) What does the program for thelist (1, 5,2, 3, 6, 6,9]? (©) What does the program print for the list |2, 5, 2, 3, 6, 6, 9]? How can we fix this ? Solution. (2) The given code is checking whether a list of numbers is ‘mostly odd’ — ie, whether there ate more odd numbers than even numbers in the list. (b) Balanced oddity (€) The code prints nothing. This if odds > evens : print ("Balanced oddity") else: print (“Unbalanced oddity") because there is only one print statement, the fix is :COMPUTER SCIENCE Wit} if they have al least one member py for le sake ofthe exercise, write jy using, sans “Overlapped” 10. Given tay fists, write # program Hat ri nero bul otherwise prints “Sqnarated™, You may use the tion. . oe = eval input("énter 14st] :°)) Lista = eval (input ("Enter ist2 :")) lent « len(1ista) Jen2 » len(1istB) for a in range(lenl) : ele = listafa) ifele in listB : princ(“Overlapped") break else : , print("Separated” ; LL Write a program te find the second largest number of a list af munbers. ‘loop ends normally, not because of break © Solution, Ast = eval (input("Enter list :")) length = len(1st) bineese = secondbiggest = 1st[@] # though logically not fair for 4 in range(1, length): Af Ist[i] > biggest: secondbiggest = biggest biggest = 1st[i} elif Ist[i] > secondbiggest: secondbiggest = Ist[i] print(“Largest nunber of the list :", biggest) print("Second Largest number of the list :" secondbiggest ) 12. Write program that inputs ats of numbers aut shifts all the zeros to right and all uon-zero mumbers toe, of the list. Solution, Ast = eval(input(“Enter list :")) Jength = len(1st) end = Jength-1 print("Original list : ", 1st) iro while (4 <= end): ele = Ist[i] ifele=0: for j in range(i, end): Ast[i] = Ist[jsa) else: Ast[end] = @ end = 1 4¥ Istip! i 4 # if after shitting, a Consecutive @ shifted to current place Print("zero shifted : ", Ast)