Lists
A Simple List
Besides numbers and text, variables can contain a collection of items called a list
- For example
- a collection (list) of colors: colors = ['green', 'blue', 'red', 'yellow']
- Once we create a list, we can use it in a for loop as follows:
1 2 3 4 5 |
|
- The full code can be found here: https://trinket.io/python/94de1feac8
What Else Can We Do With a List?
Let's start with a list: colors = ['green', 'blue', 'red', 'yellow']
Operation | Result |
---|---|
colors.append('green') | colors = ['green', 'blue', 'red', 'yellow“, 'green'] |
colors.insert(2, 'green') | colors = ['green', 'blue', 'green', 'red', 'yellow“, 'green'] |
colors.remove('green') | colors = ['blue', 'green', 'red', 'yellow“, 'green'] |
color = colors.pop(2) | color = 'red', colors = ['blue', 'green', 'yellow', 'green'] |
color = colors.pop() | color = 'green', colors = ['blue', 'green', 'yellow'] |
colors.sort() | colors = ['blue', 'green', 'yellow'] |
colors.reverse() | colors = ['yellow', 'green', 'blue'] |
colors.clear() | colors = [] |
Taking It a Step Further
- Find the number of elements in a list:
1 2 3 |
|
Challenge
- Get a random number of colors from the user
- hint: color = input('Enter a color: ')
- Store the color in a list of colors
- Draw the polygons, one in each color, in a similar manner to the four squares.
- Let the number of sides of each polygon be equal to the number of colors that the user entered
- hint: # of sides = len(colors)
- Calculate the angle needed to draw a polygon
- hint: angle = 360 / # of sides
- Loop through all of the colors in the list you created
- hint:
1 2 3 4 5 6
for color in colors: # set the color # begin fill # draw polygon # end fill # hide the turtle when done.
- hint:
- Let the number of sides of each polygon be equal to the number of colors that the user entered
Need some help? Take a look here: https://trinket.io/python/06ab63c75f
What happens if the user only enters two colors? Can you add a check?