Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
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.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
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.
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.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
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.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
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.
Lecture 2 Python Basics.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CS-104 Final Exam Review Victor Norman.
Ruth Anderson University of Washington CSE 160 Spring 2015
Containers.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Agenda Control Flow Statements Purpose test statement
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 in Python.
Bryan Burlingame Halloween 2018
CISC101 Reminders Slides have changed from those posted last night…
Ruth Anderson University of Washington CSE 160 Winter 2017
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
Programming for Geographical Information Analysis: Core Skills
CS 1111 Introduction to Programming Fall 2018
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Python Lists and Sequences
CHAPTER 3: String And Numeric Data In Python
CS 1111 Introduction to Programming Spring 2019
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Data Structures & Algorithms
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Python Review
Common Lisp II.
For loop Using lists.
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Introduction to Computer Science
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription (again, zero based indexing): >>> a[0] 1 >>> a[len(a) - 1] 3

Assignment a = (1,2,3) a[0] = 10 # Breaks the program. a = [1,2,3] a[0] = 10 # Fine a = [10,20,30] # Assigns a new list, # rather than altering the old one. Essentially the same kind of operation as : b = 100 b = 200

For mutables, if you change a variable's content, it changes for all attached labels. You can still disconnect it and connect it to something new. >>> a = [10] # List containing 10. >>> b = a >>> b[0] 10 >>> b[0] = 20 >>> a[0] # Changing b changes a, as they refer to the same 20 # object. >>> b = [30] # Disconnect b from [10] and attach it to a new list. Essentially a new "b". >>> a[0] 30 The what of variables

Slices: extended indexing Extended indexing is a way of referencing values in a sequence. These are sometimes called a 'slice' (essentially slice objects are generated, containing the indices generated by a range). a[i:j] returns all the elements between i and j, including a[i] but not a[j] a[i:j:k] returns the same, but stepping k numbers each time. Slices must have at least [:] (slice everything), but the rest is optional. If j > len(a), the last position is used. If i is None or omitted, zero is used. If i > j, the slice is empty. a[:2] # First two values. a[-2:] # Last two values. “For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2.”

Assigning slices While you can use slices as indices with immutable sequences, they can be used with mutable sequences like lists for assignment as well. >>> a = [0,1,2,3,4] >>> b = [10,20,30] >>> a[1:3] = b >>> a [0,10,20,30,3,4] # Note we replace 2 values with 3. >>> b = [10,20] >>> a[1:5] = b [0,10,20] # We replace 4 values with 2.

List related functions All the same functions as immutable sequences, plus these "in place" functions (i.e. functions that change the original, rather than returning a copy): del(a[:]) Delete a slice. a.clear() Empty the list. a.extend(b) Extends a with the contents of b (i.e. a += b) a.insert(i, x) Inserts x into a at the index position i. a.pop(i) Returns the item at i and removes it from a. a.remove(x) Remove the first item from a equal to x a.reverse() Reverses the items of a. https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

2D lists There is nothing to stop sequences containing other sequences. You can therefore built 2D (or more) sequences. a = [[1,2,3],[10,20,30],[100,200,300]] They can be regular (i.e. have all second dimension sequences the same length) or irregular. Reference like this: >>> a[0] [1,2,3] >>> a[0][0] 1

NB: 2D sequences Note that tuples can contain lists. This seems odd, as lists are mutable, but it is only the object references/links in the tuple that are immutable, not the objects themselves. Tuples of numbers and strings are immutable because numbers and strings are immutable. >>> a = ([1,2],[10,20]) >>> a[0][0] = 100 >>> a ([100,2],[10,20]) >>> a[0] = [1000,2000] TypeError: 'tuple' object does not support item assignment

NB: 2D sequences Finally, you can combine two lists or tuples into a list of tuples like this: >>> a = [1,2,3,4,5] >>> b = [10,20,30,40,50] >>> c = zip(a,b) >>> d = list(c) >>> d [(1,10),(2,20),(3,30),(4,40),(5,50)] As we'll see in the module on control flow, this allows us to process two sets of number simultaneously. List(seq) https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

Command line arguments When you run a Python program at the command line, you can pass information into it. This is not unusual. When you click on a Word file in Windows, what is happening in the background is Word is being activated and passed the filename to open. Indeed, when you run a Python program, you are doing this: python helloworld.py you're saying "run the python program, and the thing after its name is the file for it to execute." The elements passed into a program like this are called "command line arguments". They are used to quickly say how a program should work without re-writing the code. One place lists are used is in getting data into a program.

Command line arguments We can actually get our code to record command line arguments. python ourfile.py arg1 arg2 arg3 Inside the program, these are made available in a list called sys.argv argv[0] is actually the script name (and, depending on OS, path) If run using the -c option of the interpreter, argv[0] == "-c". After argv[0] come the other arguments as strings.

Example #args.py import sys print ("Hello " + sys.argv[1]) > python args.py Dave > Hello Dave