Lectures‎ > ‎

Week08

Introduction to Lists

  • A list is an ordered group of values
  • Each value in the list is identified by an index
  • The values in that make up a list are called elements
  • Lists are like strings, which are order sets of characters, except each element in a list can have any type.
  • Lists and strings are called sequences
    • There are other data types that can be sequences as well
  • LIsts are denoted by square brackets [ and ]


List Values

Creating a list:

list1 = [1, 2, 3, 4]

list2 = ['red", "white", "blue"]



Nested Lists

Lists can contain other lists:

list3 = ["rectangle", [1,2], [10,15]]



The Empty List

  • A list with no elements is call empty
  • Represented by []
  • The empty list is equivalent to False in an expression

if []:
    print 'empty'
else:
    print 'not empty'


>>> type([])
<type 'list'>
>>> bool([])
False
>>>



Accessing Elements

  • Works the same was as strings
>>> list4 = ['a', 'b', 'c']
>>> list4[0]
'a'
>>> list4[1]
'b'
>>> list4[2]
'c'
>>> list4[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> 



Iterating Over Lists

  • Just like with strings, we can use the for statement to iterate over a list

>>> list5 = [90.0, 70.0, 81.0, 65.0, 99.0]
>>> for value in list5:
...     print value
... 
90.0
70.0
81.0
65.0
99.0
>>> 


Implementing Average

  • Show how to implement the average of a list
  • Use for
  • Use while
  • Use sum


List Operations

  • Concatenation with +
  • Repetition with *
  • List slices with [start:end]


The range Function

  • range(n) creates a list of elements 0 through n-1
  • range(start, end) creates a list of elements start through end - 1
  • range(start, end, step) creates a list of elements start through end - 1, stepping by step
  • Negative step values

Lists are Mutable

  • You can change the value of an element
  • You can delete elements

Tuples vs Lists

  • A tuple is like a list but a tuple is not mutable
  • Use parens instead of brackets
  • E.g. (1, 2, 3)

Introduction to Pygame


Basics

  • Coordinates
  • Event loop
  • User input

Pygame Code Template

# rect.py - Draw a simple rectangle

import pygame
 
# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
green    = (   0, 255,   0)
red      = ( 255,   0,   0)
 
pygame.init()
  
# Set the height and width of the screen
size = [500,500]
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("Simple Rectangle")
 
# Loop until the user clicks the close button.
done = False
 
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
 
# Main event loop
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
            done = True
 
    # Set the screen background
    screen.fill(black)
 
    # Draw the rectangle
    pygame.draw.rect(screen,white,[100,100,300,300])
  
    # Limit to 20 frames per second
    clock.tick(20)
 
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
     
# Exit cleanly
pygame.quit()

ċ
avg.py
(1k)
Greg Benson,
Oct 10, 2013, 6:51 PM
ċ
strobe.py
(1k)
Greg Benson,
Oct 10, 2013, 6:49 PM
Comments