loopcond1 — try it
For ages below, which version of over21 will run without producing an error?
ages = [ 18, 24, 22, 17, 20, 21, 19, 18, 20, 25 ]
over21_1 = [ (age > 21) for age in ages ]
over21_2 = [ age for age in ages if (age > 21)]
over21_1
over21_2
Both
Neither - Answers both
loopcond2 — try it
For ages below, which version of over21 will produce a list of numbers?
ages = [ 18, 24, 22, 17, 20, 21, 19, 18, 20, 25 ]
over21_1 = [ (age > 21) for age in ages ]
over21_2 = [ age for age in ages if (age > 21)]
over21_1
over21_2
Both
Neither - Answers over21_2
loopcond3 — try it
For ages below, which version of over21 will produce a list as long as ages?
ages = [ 18, 24, 22, 17, 20, 21, 19, 18, 20, 25 ]
over21_1 = [ (age > 21) for age in ages ]
over21_2 = [ age for age in ages if (age > 21)]
over21_1
over21_2
Both
Neither - Answers over21_1
loopcond4 — try it
Select the option that will produce a list equal to outlist from list inlist
inlist = [ 'pinch', 'punch', 'paunch', 'inch', 'cinch', 'wrench' ]
outlist = [ 'pinch', 'inch', 'cinch']
1 [ aword[-5:] == 'inch' for aword in inlist ]
2 [ aword[-4:] == 'inch' for aword in inlist ]
3 [ inchword for inchword in inlist if inchword[-5:] == 'inch' ]
4 [ inchword for inchword in inlist if inchword[-4:] == 'inch' ]
5 [ inch[-5:] for inch in inlist if inch[-5:] == 'nch' ]
6 [ inch[-4:] for inch in inlist if inch[-4:] == 'nch' ]
7 None of the above - Answers 4
loopcond5 — try it
Select the option that will not produce a list equal to outlist from list inlist
inlist = [ 'pinch', 'punch', 'paunch', 'inch', 'cinch', 'wrench' ]
outlist = [ 'pinch', 'punch', 'paunch', 'inch', 'cinch', 'wrench' ]
1 [ element for element in inlist if element[-5:-1] == 'inch' ]
2 [ words for words in inlist ]
3 [ val[:] for val in inlist ] - Answers 1
loopcond6 — try it
Select the option that will produce a list equal to outlist from list inlist
inlist = [ 'aardvark', 'apogee', 'ape', 'open', 'oar', 'ocean' ]
outlist = [ 'aardvark', 'oar' ]
, 1 [ item[2] == 'a' for item in inlist]
2 [ item for item in inlist if item[0] == 'a' ]
3 [ item for item in inlist if item[1] == 'a' ]
4 [ item for item in inlist if item[2] == 'a' ]
5 None of the above - Answers 3
loopcond7 — try it
Select the option that shows the value of list cantvote
ages = [ 5, 16, 18, 22, 26, 22, 19, 30, 17, 13, 20, 10 ]
cantvote = [ age for age in ages if age < 18 ]
candrivecantvote = [ age for age in ages if age >= 16 ]
print( candrivecantvote )
1 [ 5, 16, 18, 22, 26, 22, 19, 30, 17, 13, 20, 10 ]
2 [ 5, 16, 18, 19, 17, 13, 10 ]
3 [ 5, 16, 18, 17, 13, 10 ]
4 None of the above - Answers 4
loopcond8 — try it
Select the option that shows the value of list candrivecantvote
ages = [ 5, 16, 18, 22, 26, 22, 19, 30, 17, 13, 20, 10 ]
cantvote = [ age for age in ages if age < 18 ]
candrivecantvote = [ age for age in ages if age >= 16 ]
print( candrivecantvote )
[ 5, 16, 18, 22, 26, 22, 19, 30, 17, 13, 20, 10 ]
[ 5, 16, 18, 19, 17, 13, 10 ]
[ 5, 16, 18, 17, 13, 10 ]
[ 16, 17, 13 ]
[ 16, 17 ]
[ 16 ]
None of the above - Answers none of the above
iflong1 — try it
Select the option that shows the number of lines of printed output that will be produced by the code
below.
weather = 'sunny'
if weather != 'sunny':
print( 'Might need raincoat' )
1
2
3
4
5
0
Error - Answers 0
iflong2 — try it
Select the option that shows the number of lines of printed output that will be produced by the code
below.
if True
print( 'This gets printed' )
1
2
3
4
5
0
Error - Answers error