Download presentation
Presentation is loading. Please wait.
Published byJustin Gilbert Modified over 9 years ago
1
Peter Andreae Python Workshop for Teachers #2, 3
2
© Peter Andreae Lists Lists are a collection of items in order Like arrays, but better can be extended or shortened can be acted on as a whole eg, can be stored in variable, passed to function, printed out, … can act on individual elements, indexed by position (0,1,2….) Written with square brackets, comma separated, eg names = ["peter", "james", "justine"] ages = [ ] Access using index in [ ]: print(names[2] ) names[i] = "unknown" Append to end: names.append("jillian")
3
© Peter Andreae Example Programs concordance: lists of words for item in list len(list), append, in, primes lists of numbers access and change elements function with parameters print with sep & and off-by-1 errors optimisation
4
© Peter Andreae To Do: stats: read a line of numbers into a list then find min, max, average list all numbers less than half the average. wordList read a list of words from user print out shortest and longest word any word starting with a vowel wordShifter: read a list of words repeatedly, ask user for index of a word to move to the left swap the word with its neighbour print out the line
5
© Peter Andreae Graphical output and GUI Need tkinter library to do any graphical input/output or GUI Simple graphical output: set up window and canvas call drawing commands on the canvas create_rectangle(left, top, right, bot,….) call canvas.update() to make things visible. Easy to add buttons and mouse create button and specify function to call when button is pressed. Button(window, text="Grow", command=grow).pack() bind mouse event to function called when mouse event happens canvas.bind(" ", command=plant) def plant(event) : ….. event.x …. event.y
6
© Peter Andreae Example Programs Flower : simple drawing Garden : list of coordinates functions with parameters buttons Tuples: collection of values "stuck together" in ( …) can treat the collections as a single value can access the elements using [ ] flower = ( 100, 150, 50) flower[2]
7
© Peter Andreae To Do: Write programs that Draw a table with four chairs Draw a collection of tables, given coordinates in two arrays tablesX = [100, 300, 500] tablesY = [150, 250, 150] Allow the user to place or move tables with the mouse
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.