Module 5 Flashcards
item = 25 if item > 10: magnitude = 'Between 10 and 20' elif item > 20: magnitude = 'Greater than 20' else: magnitude = '10 or less' magnitude
output ?
Between 10 and 20
item = 25 if item > 20: magnitude = 'Greater than 20' elif item > 10: magnitude = 'Between 10 and 20' else: magnitude = '10 or less' magnitude
output ?
‘Greater than 20’
for i in range(5):
print(i)
output
0
1
2
3
4
for i in range(50, 101, 10):
print(i)
50
60
70
80
90
100
for i in range(0.5,1.0,0.1):
print(i)
TypeError: ‘float’ object cannot be interpreted as an integer.
Detailed traceback:
File “<string>", line 1, in <module></module></string>
for i in range(4):
print(i)
0
1
2
3
cereals = {‘Special K’: 4, ‘Lucky Charms’: 7, ‘Cheerios’: 2, ‘Wheaties’: 3}
for cereal, stock in cereals.items():
print( cereal + “ has “ + str(stock) + “ available”)
Special K has 4 available
Lucky Charms has 7 available
Cheerios has 2 available
Wheaties has 3 available
numbers = [2, 3, 5]
word_length = {number : number ** 2 for number in numbers}
word_length
{2: 4, 3: 9, 5: 25}
item_list = [25, 13, 21, 8, 17, 11, 4]
for item in item_list:
if item > 20:
magnitude = ‘greater than 20’
elif item > 10:
magnitude = ‘between 10 and 20’
else:
magnitude = ‘10 or less’
break
print(magnitude)
greater than 20
between 10 and 20
greater than 20
number_over_20 += item
is the same as ?
number_over_20 = number_over_20 + item