Lists Python Next steps Python Next steps L
- Slides: 18
Lists Python: Next steps
Python: Next steps L 3 Lists Learning Objectives • • Be able to store and update values in a list Be able to append data to a list Be able to use a for() loop to step through a list Understand why using a list can be more efficient than usingle variables
Python: Next steps L 3 Lists Starter Why is this piece of code inefficient?
Python: Next steps L 3 Lists Variables • Variables are needed to store values • If you have a lot of values then storing them all individually makes the code very longwinded • It would be much easier to use a list of values (sometimes called an array)
Python: Next steps L 3 Lists Creating & reading lists Try this code: highscore = [125, 63, 35, 12] print(highscore) print(highscore[0]) print(highscore[1])
Python: Next steps L 3 Lists Creating & reading lists • When using lists we can use the index in square brackets to refer to one element in the list • high. Score[0] is the first element in the list (remember that computers count from 0 ) • Try printing out the other two values from the list
Python: Next steps L 3 Lists Updating lists Try this code: highscore = [125, 63, 35, 12] highscore[0] = 127 print(highscore)
Python: Next steps L 3 Lists Updating lists • Just as you can print an individual element using the index, you can also update an individual element • Since the new highscore is 127, try updating the rest of the array to store the 4 highest scores in order (127, 125, 63, 35) • Use highscore[n] = value
Python: Next steps L 3 Lists Appending lists Try this code: highscore = [125, 63, 35, 12] highscore[4] = 8 print(highscore) • Does the program run? • What does the error message mean?
Python: Next steps L 3 Lists Appending lists • You have created an array with 4 values: highscore[0], highscore[1], highscore[2] and highscore[3] • You can’t simply set a value for highscore[4] because there isn’t a highscore[4]
Python: Next steps L 3 Lists Appending lists Try this code: highscore = [125, 63, 35, 12] highscore. append(8) print(highscore)
Python: Next steps L 3 Lists Worksheet 5 Complete Worksheet 5: Superheroes
Python: Next steps L 3 Lists Using Loops Try this code: highscore = [125, 63, 35, 12] for counter in range(4): print(highscore[counter])
Python: Next steps L 3 Lists Using loops One big advantage of lists is that you can use a for loop to step through each value high. Score[0] high. Score[1] high. Score[2] high. Score[3] 125 63 35 12 counter = 0 print(high. Score[counter])
Python: Next steps L 3 Lists Using loops One big advantage of lists is that you can use a for loop to step through each value high. Score[0] high. Score[1] high. Score[2] high. Score[3] 125 63 35 12 counter = 1 print(high. Score[counter])
Python: Next steps L 3 Lists Using loops One big advantage of lists is that you can use a for loop to step through each value high. Score[0] high. Score[1] high. Score[2] high. Score[3] 125 63 35 12 counter = 2 print(high. Score[counter])
Python: Next steps L 3 Lists Using loops One big advantage of lists is that you can use a for loop to step through each value high. Score[0] high. Score[1] high. Score[2] high. Score[3] 125 63 35 12 counter = 3 print(high. Score[counter])
Python: Next steps L 3 Lists Worksheet 6: Super villains Complete the worksheet, which will give you practice using a for loop and a list together
- X.next = x.next.next
- Lists of tuples python
- Methods python list
- Concatinates
- Python next steps
- Python next steps
- Lisp lists
- "new bookmarking lists 2018"
- Parallel structure in lists
- Ucl library reading lists
- Clwords
- Qri word lists
- Java types of lists
- What is diigo
- What is semicolon
- Mesa verde ap world history
- Swot analysis customer service example
- Swst spelling lists level 5
- No nonsense spelling strategies