CSE 231 Lab 6.

Slides:



Advertisements
Similar presentations
Python for Informatics: Exploring Information
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Course A201: Introduction to Programming 10/28/2010.
Container Types in Python
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.
Lists Introduction to Computing Science and Programming I.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Guide to Programming with Python
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
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.
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.
Built-in Data Structures in Python An Introduction.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
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.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
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.
Lists vs Strings CMSC 201. Overview Python strings and lists are similar but different. Similar: syntax, access mechanisms, operators Different: Strings.
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.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
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.
Intro to CS Nov 21, 2016.
Tuples and Lists.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
CSc 120 Introduction to Computer Programing II
Python Lists Chapter 8 Python for Everybody
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Part 2 Taken from notes by Dr. Neil Moore
Lists in Python.
Bryan Burlingame Halloween 2018
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
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
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Python Review
Ruth Anderson UW CSE 160 Spring 2018
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 8.
CSE 231 Lab 7.
CSE 231 Lab 9.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CSE 231 Lab 6

Topics to cover Creating Lists & tuples Lists methods In-place vs not in-place Patterns List comprehension A Python list is a sequence of objects. It is a sequence like a string. It shares characteristics such as indexing & slicing, iteration (for) and membership (in) as well as functions such as len().

LISTS and tuples A sequence of objects. It is a sequence like a string. It shares characteristics such as indexing & slicing, iteration (for) and membership (in) as well as functions such as len(). Mutable vs Immutable Strings, ints, floats, tuples are all immutable Lists are Mutable Tuples are immutable lists

Lists my_list = ["Arthur", "King", "of", "the", "Britons"]

Lists – Creating Lists my_list = [] # start off with nothing in the list my_list = ['I', 'am', 'Full', 'of', 'stuff'] my_list = list(‘CSE231 Rocks') # using list() ['C', 'S', 'E', '2', '3', '1', ' ', 'R', 'o', 'c', 'k', 's'] mixed_list = [“string”, 1, 42.0, True] Matrix = [[1, 1], [1,1]]

List methods: In-place my_list.append('A') # takes element arg. & puts to end of list my_list.pop(3) # takes index arg. & takes it out of list If no argument is given, it pops out the last element What does this return? my_list.sort() # sorts entire list & modifies it in place. What does the function sorted(my_list) do? my_list.insert(index, value) # Insert value into position index my_list.remove(value) # removes the first instance of value # crashes if the value doesn't exist

Lists – What is in-place? some_list = [] some_list.append(1) print(some_list) >>> [1] # Why didn’t I need to do some_list = some_list.append(1) ? # Methods (like append) on lists happen IN-PLACE meaning there is no return!

Lists – What is in-place? >>> some_list = [3, 1, 7, 2, 4] >>> some_list = some_list.sort() >>> print(some_list) None # uh oh. What happened to the data??? # You reassigned some_list when sort is done in-place! # The return of a function that does not return something is what?

Lists – What is in-place? >>> some_list = [3, 1, 7, 2, 4] >>> some_list.sort() >>> print(some_list) [1, 2, 3, 4, 7] # All good again!

List methods: NOT-IN-PLACE ‘-’.join(my_list) # joins entire list with a delimiter ‘-’ >>my_list = [‘I’, ’am’, ‘Full’, ‘of’, ‘stuff’] >>my_string= ‘-’.join(my_list) >>print(my_string) I-am-Full-of-stuff

Split Method >>my_str='I am in my 231 lab‘ split and also strip methods are your best friends with data parsing By default it separates strings by whitespaces into a list Very helpful when given lines of data. >>my_str='I am in my 231 lab‘ >>my_str.split() ['I', 'am', 'in', 'my', '231', 'lab'] >>my_str.split('m') ['I a', ' in ', 'y 231 lab']

Patterns Counter: x = 0 # initialize at zero x += 1 # increment the counter Strings: s = ‘’ # initialize with empty string s += ch # concatenate characters Lists: L = [] # initialize with empty list L.append(value) # append values to the list

List Comprehensions L=[] For i in range(3) L.append(i**3) L = [i**3 for i in range(3)] Other examples >> L = [i**3 for i in range(10) if i%2 == 0] >>L = [int(x) for x in line_list] line = “3 12 5 82 1” # original data read from a file line_list = line.split() #split to get a list of strings, SHOULD CONVERT TO INT NEXT