Practice Activity --
Working with Counting Loops -- Part 2
Objective:
Instructions:
This is the second part of a 2-part practice activity. Be sure to complete Part 1 before doing these exercises.
Write a Python program to solve each of the following in the order shown (in each pattern, the intention is that each number be printed on a separate line; no commas are required):
Feel free to discuss any of these in the Discussion Forum. Upload your completed work as a single .py file with each of the exercises above completed in the order listed.
.I have implemented the required code in python.
And also provided the output of the respective code.
Source code:
Loopspart-2.py
# while loop to print -3, -2, -1, 0, 1, 2, 3, 4
i = -3
while i < 5:
print(i)
i += 1
# for loop to print -3, -2, -1, 0, 1, 2, 3, 4
x = range(-3, 5)
for n in x:
print(n)
# while loop to print 20, 15, 10, 5, 0, -5, -10
i = 4
while i > -3:
print(i*5)
i -= 1
# for loop to print 20, 15, 10, 5, 0, -5, -10
start = 20
stop = -15
step = -5
for number in range(start, stop, step):
print(number)
# while loop to print 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36
i = -6
while i <= 6:
print(i**2)
i += 1
# while loop to print 10, 20, 40, 80, 160, 320, 640
i=1
while i <=64 :
print(i*10)
i += i
Output:
-3 -2 -1 0 1 2 3 4 -3 -2 -1 0 1 2 3 4 20 15 10 5 0 -5 -10 20 15 10 5 0 -5 -10 36 25 16 9 4 1 0 1 4 9 16 25 36 10 20 40 80 160 320 640
Get Answers For Free
Most questions answered within 1 hours.