4-3
for i inrange(1,21):
print(i, end=' ')
output:
1 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20
4-5
nums = range(1,1000000)
print(min(nums))
print(max(nums))
print(sum(nums))
output:
1
999999
499999500000
4-6 4-10
nums = [i for i inrange(1,21,2)]
for num in nums:
print(num,end=' ')
print("\n")
print("The firstthree items in the list are:")
for num in nums[:3]:
print(num,end=' ')
print("\n")
print("Threeitems from the middle of the list are:")
for num in nums[3:6]:
print(num,end=' ')
print("\n")
print("The lastthree items in the list are:")
for num in nums[-3:]:
print(num,end=' ')
output:
1 3 5 7 9 11 13 15 17 19
The first three items in the list are:
1 3 5
Three items from the middle of the list are:
7 9 11
The last three items in the list are:
15 17 19
4-7
nums = [i for i inrange(3,21,3)]
for num in nums:
print(num,end=' ')
output:
3 6 9 12 15 18
4-9
nums = []
for i inrange(1,10):
nums.append(i**3)
for num in nums:
print(num,end=' ')
print("\n")
nums = [i**3for i inrange(1,10)]
for num in nums:
print(num,end=' ')
output:
1 8 27 64 125 216 343 512 729
1 8 27 64 125 216 343 512 729
4-13
foods = ('pizza','rice','fruit','vegetable','meet')
for food in foods:
print(food,end=' ')
print('\n')
foods = ('noodles','water','fruit','vegetable','meet')
for food in foods:
print(food,end=' ')
foods[1]='rice'
output:
pizza rice fruit vegetable meet
noodles water fruit vegetable meet
---------------------------------------------------------------------------
TypeError Traceback(most recent call last)
<ipython-input-6-dc9caf9e544f> in <module>()
6for food in foods:
7 print(food,end=' ')
----> 8foods[1]='rice'
TypeError: 'tuple' object does not support itemassignment