Uploaded by ThatOneKid2005

looping lists.py

advertisement
*Copied and pasted from Python Idle. It wouldn’t let me upload my file to Edgenuity.*
PYTHON EDITOR
# Looping Lists
from collections import deque
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
print("These are the elements in the colors list:\n", colors, "\n")
#Starter file comes with the code above this comment.
#Student inputs everything below this comment.
print("Using a for loop to print the colors list commands:")
for i in colors:
print(i)
print("Using a for loop with range(10)to print iterator values and a string:")
for i in range(10):
print(i, "Hello")
counter = 0
print("Using a while loop to print iterator values and a string:")
while counter < 5:
print(counter, "Loop while the counter is less than 5")
counter = counter + 1
print("The for loop removes and prints list elements in stack order:")
for i in range(len(colors)):
print(colors.pop())
print("The colors list after all elements are removed:\n",colors)
new_colors = ["red", "orange", "yellow", "green", "blue", "purple"]
print("The loop prints and removes elements in the new_colors list in queue order:")
for i in range(len(new_colors)):
print(new_colors.pop(0))
print("The new_colors list after all elements are removed:\n", new_colors)
more_colors = ["violet", "pink", "brown", "teal", "magenta"]
more_colors_deque = deque(more_colors)
print("Elements in the more_colors list:\n", more_colors)
print("Elements in the deque object:\n", more_colors_deque)
print("Using a for loop to print the deque elements as a queue:")
for i in range(len(more_colors_deque)):
print(more_colors_deque.popleft())
print("Printing the deque object after all elements are removed:\n", more_colors_deque)
PYTHON SHELL
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
======= RESTART: C:\Users\rickb\Downloads\Looping_Lists\looping_lists.py =======
These are the elements in the colors list:
['red', 'orange', 'yellow', 'green', 'blue', 'purple']
Using a for loop to print the colors list commands:
red
orange
yellow
green
blue
purple
Using a for loop with range(10)to print iterator values and a string:
0 Hello
1 Hello
2 Hello
3 Hello
4 Hello
5 Hello
6 Hello
7 Hello
8 Hello
9 Hello
Using a while loop to print iterator values and a string:
0 Loop while the counter is less than 5
1 Loop while the counter is less than 5
2 Loop while the counter is less than 5
3 Loop while the counter is less than 5
4 Loop while the counter is less than 5
The for loop removes and prints list elements in stack order:
purple
The colors list after all elements are removed:
['red', 'orange', 'yellow', 'green', 'blue']
blue
The colors list after all elements are removed:
['red', 'orange', 'yellow', 'green']
green
The colors list after all elements are removed:
['red', 'orange', 'yellow']
yellow
The colors list after all elements are removed:
['red', 'orange']
orange
The colors list after all elements are removed:
['red']
red
The colors list after all elements are removed:
[]
The loop prints and removes elements in the new_colors list in queue order:
red
orange
yellow
green
blue
purple
The new_colors list after all elements are removed:
[]
Elements in the more_colors list:
['violet', 'pink', 'brown', 'teal', 'magenta']
Elements in the deque object:
deque(['violet', 'pink', 'brown', 'teal', 'magenta'])
Using a for loop to print the deque elements as a queue:
violet
pink
brown
teal
magenta
Printing the deque object after all elements are removed:
deque([])
>>>
Download