Can You Compute?
  • HOME
    • Class of 2017
    • COVER LESSON FB
    • Yr 10: Program of Study 2016
    • Yr 11:Coming soon
    • Student Showcase
  • KS3
    • Term 5 Scratch
    • Term 4 Websites
    • Term 3 Spreadsheets
    • Term 2: Binary & Algorithms
    • Term 1: Basics and Hardware
  • Yr 9
    • Year End Examination
    • L13-15: School Brochure
    • L12: Building a Computer
    • L11: Buying a House
    • L10: Logic Gates
    • L9: Presentations
    • L8: Binary
    • L7: CS Alan Turing
    • L6: Spreadsheets
    • L5: JS in an Hour!
    • L4: Intro to Coding
    • L3: IT Review and Mail Merge
    • L2: IT Letter Writing
    • L1: CS Compression
  • J276/01
    • Unit 1 Overview
    • EPIC FAILS
    • 1.1 System Architecture >
      • L1: The CPU
      • L2: The CPU in action
      • L3: CPU Recap
      • L4: Intro to LMC
      • L5: Problem solving with LMC
      • L6: LMC Subtraction
      • L7: LMC Looping
      • L8: LMC Multiplication
      • L9: Embedded Systems
      • L10: End of Term Exam
    • 1.2 Memory >
      • L1: RAM, ROM & MEMORY
      • L2: What do you know?
    • 1.3 Storage >
      • L1: Storage part 1
      • L2: Storage part 2
      • L3: Storage recap
    • 1.4 Wired / Wireless Networks >
      • L1: Introduction
      • L2: P2P & CS
      • L3: Hardware
      • L4: The Internet
      • L5: Web Server TO DO
      • L5: End of Unit Test
    • 1.5 Network topologies, protocols and layers >
      • L1: Topologies
      • L2: Protocols
      • L3: Packets and Layers
      • L4: Packets Games
      • L5: Routing Game
      • L6: End of Unit Test
    • 1.6 System Security
    • 1.7 System Software >
      • L1: The Operating System
      • L2: Utility Software
      • L3: GUI and Games
      • L4: Unit Test
  • J276/02
    • Unit 2 Overview
    • 2.1 Algorithms >
      • L1: Intro to Algorithms
      • L2: The Knight's Tour
      • L3: Bresenham
      • L4: Searching Algorithms
      • L5: Bubble Sort
      • L6: Insertion Sort
      • L7: Merge
      • L8: Sorting Recap
      • L9: Creating Algorithms
      • L10: Pseudocode
      • L11: Interpreting Algorithms
      • L12: End of Unit Test
    • 2.2 Programming Techniques >
      • Coding Intro
      • L1: Programming Concepts
      • L2: Sequence, Selection & Iteration
      • L3: SSI Practice
      • L4: String Manipulation
      • L5: Secret Codes
      • L6: File Handling
      • L7: Use of records to store data
      • L8: SQL
      • L9: SQL Python
      • L10 ,11, 12: Arrays
      • L13: Functions
      • L14: Data Types
      • L15: Casting
      • L16 -17: Test and Code
    • 2.3 Robust Programs >
      • L1 : Defensive Design
      • L2 : Testing Programs
    • 2.4 Computational Logic >
      • L1: Comp. Logic 1
      • L2: Comp. Logic 2
      • L3: Operators (Python)
    • 2.5 Translators & Language >
      • L1: Types of Languages
      • L2: Translators
    • 2.6 Data Representation >
      • L1: Units
      • L2: Numbers Binary
      • L3: Binary Addition
      • L4: Hexadecimal
      • L5: Recap
      • L6: Characters
      • L7: Images
      • L8: SenseHAT images
      • L9: Sound & Sampling
      • L10: Code Sound
      • L11: Compression Part 1
      • L12: Compression Part 2 >
        • Compression
      • What do you know?
  • J276/03 NEA
    • Overview
    • Part One
    • Part Two
    • Analysis
    • Success Criteria
    • Design - Algorithms
    • Pseudocode
    • Testing
  • Py
    • Coding Concepts
    • Writing your first Program
    • Python Pages
    • Python Playground
    • Strings
    • Python Operators
    • Functions
    • Conditions
    • Try and Except
    • Break and Continue
  • Py +
    • Looping
    • Why does my code not work?
    • Errors
    • Validation
    • Lists
    • Lists again
    • Looking for data in String
    • Writing to Files
    • Read, Write & Sorting Data
  • CODING
    • Magic Python
    • Alan Turing
    • Brian the Turtle
    • Grade Calculator
    • Nested IF Hotels
    • Vending Machine
    • A Looping Menu
    • For
    • Christmas Coding
    • JS in an Hour!
    • CSS + HTML
    • Medical Claims
    • Hour of Code 2015
  • Contact
    • Dictionaries - coming soon
    • L2:

python lists

Learners will be able to:
  1. Know how to create a list using Python
  2. Add data / information to a list
  3. Manipulate lists using a range of operations

In this lesson you will learn how to use Lists to store, edit and manipulate the data that is stored in them.  Adding song lyrics to start a party!

Part 1: Making a List:
Creating a list is very simple, create a variable to store the list and then use [ and ] to complete the list, it is really that simple.
  1. Look at the code below and run it, how many lists are there?
  2. Notice that when using strings in a List use, quotations.
  3. Print out the money list
  4. Create a new list called "nice things to say", add 5 nice things to say, for example you look nice today.
Part 2: Looping in a List:
You can use loops to loop around a list and then manipulate the contents or use another operation.  
  1. Look at the code below and run it, there are two friends in the list and the loop iterates twice, once for each friends.
  2. Add the names of your friends to the list on line 1.
  3. Adapt the code so that it says "Dave is my friend", replace Dave with your friends names.
  4. Use a similar structure that counts down from ten to zero and then says "Blast Off", 10, 9, 8, 7,6 etc...Blast Off!
Part 3: Print a Particular Entry:
Lists entries are stored in a numbered position, remember that the first position value is zero. You can print a particular position value by using the code print name_of_list[3].   This will print out the entry at the 4th position.

  1. In the code window below create a new list and add six entries.
  2. Print out the 3rd and the 5th entries
  3. Show another students your list and ask them for a word from the list, use the code line to print out that word
Part 4: Adding New Entries to an Existing List:
You can add new entries to a list, so that the list grows!  There are two methods append and extend.  Look at the code below for a very important list.  To add a new word use the code line name_of_list.append("entry")

  1. Run the code, the party is not started.
  2. Append the following lines to get the party started, C, A, Young Man, Ways to have a good time
  3. Instead of using append change it to extend, what happens?
  4. Can you think of when you may use this?
  5. Create a program to use the extend function. (Maybe separating a large number?)
  6. You can also add an entry at a particular point you want using name_of_list.insert(i, x)
  7. Add the phrase "Stop the Party!!" at the 1st position
  8. Can you research and find another method?
Part 5: Reverse that List:
Using the code window above reverse the list so that it prints out in reverse order.  This uses the code line name_of_list.reverse() then you can print the list.  
  
  1. Print the list in the original order and then print it it reverse.
  2. Lists are also mutable which means that you can change or replace the entry, for example replace the Y with a Z.
  3. Replace the Y above using the code the_YMCA_party_list[0] = Z 
  4. Now replace the M with the phrase "Have a good time"

Part 6: Find the length of a List:
How long is a list?  You could count the entries manually but this is a little chewie!  So instead use the len() function which will measure and return the length if the list. 
  1. Run the code window below
  2. What number does it give you?
  3. Add extra entries to make the total printed 15
  4. You can also check if a particular value is in a list using print 35 in a_list 
  5. Check if the following values are in the list, 34, 12, 6, 23
Part 7: Try some Other List Tools:
Try some of these other List tools
  1. list.append(x)
  2. list.remove(x)
  3. list.pop([i])
  4. list.sort(cmp=None, key=None, reverse=False)

Further reading here: 

  • HOME
    • Class of 2017
    • COVER LESSON FB
    • Yr 10: Program of Study 2016
    • Yr 11:Coming soon
    • Student Showcase
  • KS3
    • Term 5 Scratch
    • Term 4 Websites
    • Term 3 Spreadsheets
    • Term 2: Binary & Algorithms
    • Term 1: Basics and Hardware
  • Yr 9
    • Year End Examination
    • L13-15: School Brochure
    • L12: Building a Computer
    • L11: Buying a House
    • L10: Logic Gates
    • L9: Presentations
    • L8: Binary
    • L7: CS Alan Turing
    • L6: Spreadsheets
    • L5: JS in an Hour!
    • L4: Intro to Coding
    • L3: IT Review and Mail Merge
    • L2: IT Letter Writing
    • L1: CS Compression
  • J276/01
    • Unit 1 Overview
    • EPIC FAILS
    • 1.1 System Architecture >
      • L1: The CPU
      • L2: The CPU in action
      • L3: CPU Recap
      • L4: Intro to LMC
      • L5: Problem solving with LMC
      • L6: LMC Subtraction
      • L7: LMC Looping
      • L8: LMC Multiplication
      • L9: Embedded Systems
      • L10: End of Term Exam
    • 1.2 Memory >
      • L1: RAM, ROM & MEMORY
      • L2: What do you know?
    • 1.3 Storage >
      • L1: Storage part 1
      • L2: Storage part 2
      • L3: Storage recap
    • 1.4 Wired / Wireless Networks >
      • L1: Introduction
      • L2: P2P & CS
      • L3: Hardware
      • L4: The Internet
      • L5: Web Server TO DO
      • L5: End of Unit Test
    • 1.5 Network topologies, protocols and layers >
      • L1: Topologies
      • L2: Protocols
      • L3: Packets and Layers
      • L4: Packets Games
      • L5: Routing Game
      • L6: End of Unit Test
    • 1.6 System Security
    • 1.7 System Software >
      • L1: The Operating System
      • L2: Utility Software
      • L3: GUI and Games
      • L4: Unit Test
  • J276/02
    • Unit 2 Overview
    • 2.1 Algorithms >
      • L1: Intro to Algorithms
      • L2: The Knight's Tour
      • L3: Bresenham
      • L4: Searching Algorithms
      • L5: Bubble Sort
      • L6: Insertion Sort
      • L7: Merge
      • L8: Sorting Recap
      • L9: Creating Algorithms
      • L10: Pseudocode
      • L11: Interpreting Algorithms
      • L12: End of Unit Test
    • 2.2 Programming Techniques >
      • Coding Intro
      • L1: Programming Concepts
      • L2: Sequence, Selection & Iteration
      • L3: SSI Practice
      • L4: String Manipulation
      • L5: Secret Codes
      • L6: File Handling
      • L7: Use of records to store data
      • L8: SQL
      • L9: SQL Python
      • L10 ,11, 12: Arrays
      • L13: Functions
      • L14: Data Types
      • L15: Casting
      • L16 -17: Test and Code
    • 2.3 Robust Programs >
      • L1 : Defensive Design
      • L2 : Testing Programs
    • 2.4 Computational Logic >
      • L1: Comp. Logic 1
      • L2: Comp. Logic 2
      • L3: Operators (Python)
    • 2.5 Translators & Language >
      • L1: Types of Languages
      • L2: Translators
    • 2.6 Data Representation >
      • L1: Units
      • L2: Numbers Binary
      • L3: Binary Addition
      • L4: Hexadecimal
      • L5: Recap
      • L6: Characters
      • L7: Images
      • L8: SenseHAT images
      • L9: Sound & Sampling
      • L10: Code Sound
      • L11: Compression Part 1
      • L12: Compression Part 2 >
        • Compression
      • What do you know?
  • J276/03 NEA
    • Overview
    • Part One
    • Part Two
    • Analysis
    • Success Criteria
    • Design - Algorithms
    • Pseudocode
    • Testing
  • Py
    • Coding Concepts
    • Writing your first Program
    • Python Pages
    • Python Playground
    • Strings
    • Python Operators
    • Functions
    • Conditions
    • Try and Except
    • Break and Continue
  • Py +
    • Looping
    • Why does my code not work?
    • Errors
    • Validation
    • Lists
    • Lists again
    • Looking for data in String
    • Writing to Files
    • Read, Write & Sorting Data
  • CODING
    • Magic Python
    • Alan Turing
    • Brian the Turtle
    • Grade Calculator
    • Nested IF Hotels
    • Vending Machine
    • A Looping Menu
    • For
    • Christmas Coding
    • JS in an Hour!
    • CSS + HTML
    • Medical Claims
    • Hour of Code 2015
  • Contact
    • Dictionaries - coming soon
    • L2:
✕