<< Chapter < Page Chapter >> Page >
Figure 2 . Question 6.
def prod(a,b): return a*bdef sum(c,d): return c+dcomp = lambda w,x,y,z : sum(w,x) * prod(y,z) print(comp(3,4,5,6))=============================================================================== 210

Go to answer 6

Question 7

True or False? The code in the top panel of Figure 3 produces the output shown in the bottom panel of Figure 3 .

Figure 3 . Question 7.
highNumbers = list(filter((lambda x: x>5), range(10))) print(highNumbers)=============================================================================== [5, 6, 7, 8, 9]

Go to answer 7

Question 8

True or False? The code in the top panel of Figure 5 produces the output shown in the bottom panel of Figure 5 .

Figure 5 . Question 8.
import re str = "abcd$"if re.search(r'[ceg$]', str):print("Match") else:print("No Match") if re.search(r'[^abcd$]', str): print("Match")else: print("No Match")=============================================================================== MatchMatch

Go to answer 8

Question 9

True or False? The code in the top panel of Figure 7 produces the output shown in the bottom panel of Figure 7 .

Figure 7 . Question 9.
import re s = "pqrabcdefg-abcd-abc$"matches = re.findall(r'ab', s) for substr in matches:print(substr) searchObj = re.search(r'ab', s)if searchObj: print(str(searchObj.start()) + "-" + str(searchObj.end()))=============================================================================== abab ab3-5

Go to answer 9

Question 10

True or False? The code in the top panel of Figure 8 produces the output shown in the bottom panel of Figure 8 .

Figure 8 . Question 10.
myList = [0,1,2,3,4,5,6] print(myList)print("ok") print(*myList)=============================================================================== [0, 1, 2, 3, 4, 5, 6]ok [0, 1, 2, 3, 4, 5, 6]

Go to answer 10

Question 11

Note: You will need to understand lambda and filter to understand the code in most of the remaining questions.

True or False? The code in the top panel of Figure 10 produces the output shown in the bottom panel of Figure 10 .

Figure 10 . Question 11.
import re myList = ('Dick', 'TomDickHarry', 'Dick is my name')for testString in myList: print("Test string:",testString)lambdaFunction = lambda arg: re.search(r"Dick", arg) matchObject = lambdaFunction(testString)if(matchObject!=None): print(" Matching subString:",matchObject.group())=============================================================================== Test string: DickMatching subString: Dick Test string: TomDickHarryMatching subString: Dick Test string: Dick is my nameMatching subString: Dick

Go to answer 11

Question 12

True or False? The code in the top panel of Figure 11 produces the output shown in the bottom panel of Figure 11 .

Figure 11 . Question 12.
import re myList = ('Dick', 'TomDickHarry', 'Dick is my name')for testString in myList: print("Test string:",testString)lambdaFunction = lambda arg: re.match(r"Dick", arg) matchObject = lambdaFunction(testString)if(matchObject!=None): print(" Matching subString:",matchObject.group())=============================================================================== Test string: DickMatching subString: Dick Test string: TomDickHarryMatching subString: Dick Test string: Dick is my nameMatching subString: Dick

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask