OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Tuples. Tuples 1 A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Guide to Programming with Python
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 12: A few other things.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Built-in Data Structures in Python An Introduction.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Lists. The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lists CS303E: Elements of Computers and Programming.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
OCR GCSE Computing © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 1: Introduction.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 4: Writing programs.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Magic 8 ball. "Design and write a python program that emulates a Magic Eight Ball. Your program should continually prompt the user to enter a question.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
Chapter 4 Strings & Tuples
CSc 120 Introduction to Computer Programing II
Lists 2 Day /19/14 LING 3820 & 6820 Natural Language Processing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bryan Burlingame Halloween 2018
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
8 – Lists and tuples John R. Woodward.
Data Structures – 1D Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Tuples.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
15-110: Principles of Computing
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 7.
For loop Using lists.
Course A201: Introduction to Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionary.
Python List.
Tuple.
Introduction to Computer Science
Presentation transcript:

OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists

OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 9: Tuples and lists Tuples and lists are Python objects that can store sequences of data. They are like arrays in other languages in some ways but Arrays contain data items that are all the same data type; Tuples and lists can contain different data types. The main difference between tuples and lists is that Tuples are immutable; Lists are changeable – you can alter their contents during a program. This is how to make a tuple: movie=('Skyfall','PG','26/10/2012',143) This has produced: Index0123 ContentsSkyfallPG26/10/

OCR Computing GCSE © Hodder Education 2013 Slide 3 Python 9: Tuples and lists Indexing a tuple In this example: movie=('Skyfall','PG','26/10/2012',143) movie[0] is ‘Skyfall ‘ movie[1] is ‘PG’ etc. Index0123 ContentsSkyfallPG26/10/

OCR Computing GCSE © Hodder Education 2013 Slide 4 Python 9: Tuples and lists We can access elements in the same way as any Python sequence: For example, len(movie) gives us how many elements there are in the tuple.

OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 9: Tuples and lists To show that tuples are immutable, try changing the contents of one element with lines such as movie[3]='new item' print(movie[3]) You will get an error like this: movie[3]='new item' TypeError: 'tuple' object does not support item assignment

OCR Computing GCSE © Hodder Education 2013 Slide 6 Python 9: Tuples and lists Lists are set up in a similar way but use square brackets: movie=['Skyfall','PG','26/10/2012',143] To access one element print(movie[0]) To output a complete list for item in movie: print(item)

OCR Computing GCSE © Hodder Education 2013 Slide 7 Python 9: Tuples and lists More list techniques Make a slice start=int(input('Enter the start position of the slice ')) end=int(input('Enter the end position of the slice ')) print('\nThe slice is ',movie[start:end]) Create a new list booking=['New Theatre','1/1/2013','seat D23','price £5'] Concatenate lists movie+=booking Change an element in a list (you can’t do this in a tuple) movie[index]=new_value

OCR Computing GCSE © Hodder Education 2013 Slide 8 Python 9: Tuples and lists List methods Lists come with lots of methods, just like so many Python objects. Removing items from a list Here is our list – named ‘movie’: 'Skyfall','PG','26/10/2012',143,'New Theatre','1/1/2013','seat D23','price £5' The method is invoked like this: movie.remove(item to be removed) So we could say: movie.remove(‘New Theatre’) This method does not create a gap. The list shrinks when an item is removed.

OCR Computing GCSE © Hodder Education 2013 Slide 9 Python 9: Tuples and lists Sorting a list This is easy to achieve using the sort method. You can sort in either direction. Here is the output: