Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container.

Slides:



Advertisements
Similar presentations
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
Lists Introduction to Computing Science and Programming I.
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.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Python for Informatics: Exploring Information
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
PYTHON LISTS. What are lists? An ordered set of elements A variable with 0 or more things inside of it Examples myList = [8, 6, 7, 5, 3, 0, 9] strList.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
Tuples Chapter 10 Python for Informatics: Exploring Information
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
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.
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
ITEC 109 Lecture 20 Arrays / Lists. Arrays Review For loops –Rationale –Syntax –Examples.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
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.
String and Lists Dr. José M. Reyes Álamo.
Introduction to Arrays
Lecture 3 Python Basics Part 2.
Module 5 Working with Data
Tuples and Lists.
Racket CSC270 Pepper major portions credited to
Tuples Chapter 10 Python for Everybody
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists – Indexing and Sorting
Bryan Burlingame 03 October 2018
Lists in Python.
Ruth Anderson University of Washington CSE 160 Spring 2018
Creation, Traversal, Insertion and Removal
An Introduction to Python
8 – Lists and tuples John R. Woodward.
Python for Informatics: Exploring Information
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Lists – Indexing and Sorting
15-110: Principles of Computing
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Lists Copying Lists Processing Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
2-Dimensional Lists (Matrices) in Python
Python Review
Lists – Indexing and Sorting
For loop Using lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Files A common programming task is to read or write information from/to a file.
Dictionary basics Consider typing the word “infinite” in the Google search:
Strings String basics String formatting.
Introduction to Computer Science
Presentation transcript:

Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container created by surrounding a sequence of variables or literals with brackets [ ]. Example: my_list = [10, 'abc',2.5] creates a new list variable my_list that contains the three items: 10, 'abc', and 2.5 A list item is called an element .

Lists basics A list is also a sequence (just like string), meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0. >>> myList=[1,2,3,"a","hello",8] >>> myList[2] 3 >>> my_list = [ ] creates an empty list 3 is at position 2 position 2

Lists basics A list is also a sequence (just like string), meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0. >>> names = ["Jane","Margo","Sally"] >>> names print all elements of the list names ['Jane', 'Margo', 'Sally'] >>> names.append("Sam") add another element to the list >>> names display the list ['Jane', 'Margo', 'Sally', 'Sam']

Lists basics A list is also a sequence (just like string), meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0. >>> myList2=[1,2,3,5] >>> myList2[2] = 7 >>> myList2 [1,2,7,5] >>> len(myList2) get the length of the list 4

Lists basics A list is also a sequence (just like string), meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0. >>> myList2.pop() remove the last element 5 >>> myList2.pop(1) remove the element with index 1 >>> myList2 [1,7] myList2: [1,2,7,5]

Lists basics A list is also a sequence (just like string), meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0. >>> myList3=[10,9,8,7,6,5,4,6,3,2,1] >>> myList3.remove(6) finds and removes first occurrence of value 6 in the list >>> myList3 [10,9,8,7,5,4,6,3,2,1] >>> myList3.index(8) get the position/index of value 8 2

Lists basics A list is also a sequence (just like string), meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0. >>> myList3.insert(3,12) >>> myList3 [10,9,8,12,7,5,4,6,3,2,1] myList3: [10,9,8,7,5,4,6,3,2,1]

Lists basics: in-class work Assume we have two lists: a = [1,2,3,4] and b = ['a','b','c'] Answer the following questions (assume you are working with Python Shell): 1) How to display the last element of the list a? 2) How to remove the last element from the list b? 3) The function call len(a) returns the length of the list a. Write the command to display the sum of the lengths of lists a and b (using len(), print() and +).

Lists basics: in-class work Still working with lists a = [1,2,3,4] and b = ['a','b','c'] Answer the following questions (assume you are working with Python Shell): 4) What will happen if you type the following statement: a+b ? 5) How can we “save” the result of (a+b) so that we could use it sometime later in a program?

This OER material was produced as a result of the CS04ALL CUNY OER project. This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 4.0 License.