And now for something completely different . . .

Slides:



Advertisements
Similar presentations
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Lists in Python.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Built-in Data Structures in Python An Introduction.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
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.
Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
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
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
Introduction to python programming
String and Lists Dr. José M. Reyes Álamo.
Data types: Complex types (List)
Module 5 Working with Data
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Student Book An Introduction
JavaScript: Functions.
Ruth Anderson CSE 140 University of Washington
Arrays in C.
Introduction to Python
And now for something completely different . . .
Arrays, For loop While loop Do while loop
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Python
And now for something completely different . . .
Bryan Burlingame 03 October 2018
Lists in Python.
Creation, Traversal, Insertion and Removal
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1111 Introduction to Programming Fall 2018
Arrays .
String and Lists Dr. José M. Reyes Álamo.
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Reference semantics, variables and names
CS 1111 Introduction to Programming Spring 2019
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Lists Copying Lists Processing Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 4: Lists, Tuples and Dictionaries
Ruth Anderson CSE 160 University of Washington
Data Structures & Algorithms
Topics Sequences Introduction to Lists List Slicing
Ruth Anderson UW CSE 160 Spring 2018
For loop Using lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Python
Python List.
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

And now for something completely different . . . 28-Apr-19 AHD c 2010

Part 7 Lists 28-Apr-19 AHD c 2010

Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19 AHD c 2010

Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19 AHD c 2010

Introduction to Lists Most real-world Python programs contain lists. Lists allow you to collect objects, so that you can treat them as a group. Lists allow you to collect objects, so that you can treat them as a group. Lists have left-to-right positional ordering, with index capability. Lists have left-to-right positional ordering, with index capability. Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. 28-Apr-19 AHD c 2010

Introduction to Lists Most real-world Python programs contain lists. Lists allow you to collect objects, so that you can treat them as a group. Lists have left-to-right positional ordering, with index capability. Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. 28-Apr-19 AHD c 2010

Lists are arrays of object references Technically, Python lists contain zero or more references to other objects. Lists do not contain the actual objects, just a reference to (address of) them. (Aside for C and C++ programmers: Python lists are essentially arrays of pointers. They are implemented as C arrays inside the Python interpreter. Fetching an item from a Python list is about as fast as indexing a C array, i.e. fast.) 28-Apr-19 AHD c 2010

A variable (or simple variable) can take a single value. A list variable is a special kind of object that can take many values - and they don't need to be all of the same data type. 28-Apr-19 AHD c 2010

Say you want to store the exam results of 30 students Say you want to store the exam results of 30 students. You could create 30 variables: result1 = 67 result2 = 95 result3 = 58 and so on . . . 28-Apr-19 AHD c 2010

If you only had a few results to score, this method would be acceptable. But what if you had to store the results of each student in the college, not just one class? 28-Apr-19 AHD c 2010

Storing 2000 results in this way would be a very tedious and error-prone process. You would have to have 2000 different variable names and type 2000 assignment statements. 28-Apr-19 AHD c 2010

Fortunately, by using a list, we get around this problem . . . 28-Apr-19 AHD c 2010

What is a list? First consider a simple variable, the type we have used so far: result1 = 67 28-Apr-19 AHD c 2010

After this statement Python reserves an area of memory which is referred to by the name result1 which is big enough to hold the address of the integer object, for example the number 67. Imagine this area of memory as a mail box: result1 . 67 28-Apr-19 AHD c 2010

A List Variable Consider a list variable as a line of these boxes, each box is referred to by a number: 0 1 2 3 4 5 6 7 result 28-Apr-19 AHD c 2010

result[0], result[1] and so on... 0 1 2 3 4 5 6 7 result The line of boxes is known as a list. A list variable is given a name, just like the simple variable, (e.g. result), and each box is referred to by its number: result[0], result[1] and so on... 0 1 2 3 4 5 6 7 result 28-Apr-19 AHD c 2010

Important The number of the box is known as the subscript or index of the list element (box). 28-Apr-19 AHD c 2010

Note: In Python, the index of a list always starts at 0. Important Note: In Python, the index of a list always starts at 0. 28-Apr-19 AHD c 2010

Elements of a list may be of any data type The individual elements of a Python list do not need to be all of the same data type. A Python list can contain a mixture of strings, integers and float objects, indeed a mixture of any type of object. 28-Apr-19 AHD c 2010

Assigning values to lists Note: for simplicity, the diagram here shows the integer values in each element, but in reality, the elements of a list store references to the data values. result = [0,0,0,0,0,0,0,0] result[0] = 75 result[1] = 90 result[4] = 72 0 1 2 3 4 5 6 7 75 90 72 28-Apr-19 AHD c 2010

How big are the list elements? An element is one of the boxes making up the list. Each element holds the address of the object to which it refers. Hence, each element has size in bytes sufficient to hold an address. 28-Apr-19 AHD c 2010

A List Variable Consider an list variable as a line of adjacent memory locations. The elements are all the same size. 0 1 2 3 4 5 6 7 result 28-Apr-19 AHD c 2010

Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19 AHD c 2010

List operations The best way to understand lists is to see them in action... The first example program creates a list of integers, and accesses each item in the list (each element) by its index. result = [0,0,0,0,0,0,0,0] print result result[0] = 75 result[1] = 90 result[4] = 72 print result[0] print result[1] 07-01.py 28-Apr-19 AHD c 2010

An empty list This example program creates an empty list and shows the result of a print on an empty list. list1 = [] print list1 07-02.py 28-Apr-19 AHD c 2010

Appending to an empty list This example program creates an empty list and shows the result of a print on an empty list, appends an item, and prints again. list1 = [] print list1 list1.append(67) print list1[0] list1.append("spam") print list1[1] 07-03.py 28-Apr-19 AHD c 2010

A list of lists This example program creates a list of lists and prints out the list and its elements. list1 = [1,2,3] print list1 list2 = [4,5,6] print list2 list3=[list1,list2] print list3 print list3[0] print list3[1] 07-04.py 28-Apr-19 AHD c 2010

Accessing the last element in a list This example program creates a list and prints out the last element by using index number -1. list1 = [1,2,3,6,7,8,9,10] print list1 print list1[0] print list1[1] print list1[-1] print list1[-2] 07-05.py 28-Apr-19 AHD c 2010

Deleting items from a list This example program creates a list and deletes selected elements of the list. list1 = [1,2,3,4,5,6,7,8,9,10] print list1 del list1[0] del list1[-1] 07-06.py 28-Apr-19 AHD c 2010

Repeating (multiplying) lists using * print list1 print list1 * 3 list1 = list1 * 2 07-07.py The output: [1, 2, 3] [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3] 28-Apr-19 AHD c 2010

Lists can be joined together using the + symbol Joining lists together using the + symbol is a process known as concatenation. list1 = [1,2,3] print list1 list2 = [4,5,6] print list2 list1 = list1 + list2 list1 = list1 + list1 [1, 2, 3] [4, 5, 6] [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] 07-08.py 28-Apr-19 AHD c 2010

Indexing lists using the [] operator Any element of a list can be accessed by indexing. list1 = ["Anne", "Dawson", 666] print list1[0], list1[2] prints Anne 666 07-09.py 28-Apr-19 AHD c 2010

Slicing lists using the [] operator Any sublist of a list can be obtained by using the [] operator. list1 = [2,4,6,8,10,12,14,16,18,20] print list1[0:1],list1[5:7] prints [2] [12, 14] 07-10.py 28-Apr-19 AHD c 2010

Finding the length of a list using len The length of any list can be determined using the len method. list1 = ["Anne","was",'here','testing',1,2,3] list2 = [1,2,3,4] list3 = [] print len(list1), print len(list2), print len(list3) prints 7 4 0 07-11.py 28-Apr-19 AHD c 2010

List iteration The following example shows that you can iterate over lists in loops using for statements. list = [1,2,3,"Spam",4,5] for i in list: print i, 07-12.py The output: >>> 1 2 3 Spam 4 5 28-Apr-19 AHD c 2010

List membership The following example shows that you can test lists for membership with the in expression operator: list = [1,2,3,"Spam",4,5] print "Spam" in list 07-13.py The output: >>> True 28-Apr-19 AHD c 2010

List Methods Python has a number of built-in list methods which enable lists to be sorted, reversed, appended, etc. For a full list of available methods, refer to Python's web page: http://docs.python.org/lib/typesseq-mutable.html 07-14.py 28-Apr-19 AHD c 2010

List Textbook References http://www.swaroopch.com/notes/Python_en:Data_Structures#List 28-Apr-19 AHD c 2010

This presentation uses the following program files: http://www.annedawson.net/PythonPrograms.txt 07-01.py 07-02.py 07-03.py 07-04.py 07-05.py 07-06.py 07-07.py 07-08.py 07-09.py 07-10.py 07-11.py 07-12.py 07-13.py 07-14.py 28-Apr-19 AHD c 2010

End of Python_Lists.ppt 28-Apr-19 AHD c 2010

Last updated: Friday 29th May 2009, 14:38 PT, AHD 28-Apr-19 AHD c 2010