Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Computer Science 112 Fundamentals of Programming II List Iterators.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
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.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
Computer Science 111 Fundamentals of Programming I Sequences: Lists.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Programming for Linguists An Introduction to Python 24/11/2011.
Lists in Python.
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About 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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
Introduction to Computer Science – Chapter 5 CSc 2010 Spring 2011 Marco Valero.
Tuples Chapter 10 Python for Informatics: Exploring Information
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
BUILDING JAVA PROGRAMS CHAPTER 7: ARRAYS 1. 2 OUT-OF-BOUNDS INDEXES The indexes that are legal to access in an array are those in the range of 0 to 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.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
Main Points: - Working with strings. - Problem Solving.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
Fundamentals of Programming I Managing the Namespace
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
ARRAYS MIT-AITI Copyright 2001.
Perl Variables: Array Web Programming.
Computer Science 312 Haskell Lists 1.
Lists in Python.
Bryan Burlingame Halloween 2018
LING 388: Computers and Language
STL - Algorithms.
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Data Structures: Lists
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Python Data Structures: Lists
Python Data Structures: Lists
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Topics Basic String Operations String Slicing
CSE 231 Lab 6.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Computer Science 101 Python Lists

Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1, 4) # b now refers to [1, 2, 3] a == b # returns True a < [2, 3, 4] # returns True print a + b # displays [1, 2, 3, 1, 2, 3] print len(a) # displays 3

Indexing Similar to the behavior of strings so far a = [1, 2, 3] print a[2] # Displays 3 print a[len(a) - 1] # Displays 3 print a[-1] # Displays 3

Replacing an Element To replace an element at a given position, use the subscript operator with the appropriate index [ ] = a = [1, 2, 3] a[1] = 5 # The list is now [1, 5, 3] print a[1] # Displays [1, 2, 3] 0 1 2

Splitting split builds a list of tokens (words) from a string using the space as the default separator s = 'Python is way cool!' lyst = s.split() print lyst # Displays ['Python', 'is', 'way', 'cool!']

Joining join builds a string from a list of tokens (words) using the space as the default separator s = 'Python is way cool!' lyst = s.split() print lyst # Displays ['Python', 'is', 'way', 'cool!'] print ' '.join(lyst) # Displays Python is way cool!

Application: Sentence Length Short sentences are an index of good writing style. Word processing programs allow you to do word counts. sentence = raw_input('Enter a sentence: ') words = sentence.split() count = len(words) print 'There are', count, 'words in your sentence.'

Just the Tip of the Iceberg The list is a very powerful data structure There are many list processing methods A Python method is like a function, but uses a slightly different syntax

The append Method lyst = [1, 2, 3] lyst.append(4) print lyst # Displays [1, 2, 3, 4].append( ) # Puts the element at the end of the list # Actually modifies the list!!! Syntax for calling the append method:

Functions vs Methods lyst = [1, 2, 3] lyst.append(4) # A method call print len(lyst) # A function call. ( ) ( ) Syntax of method calls and function calls:

Some List Methods Example CallWhat It Does lyst.count(3) Returns the number of 3s in the list lyst.insert(5, 2) Inserts 5 at position 2, after shifting the elements at positions 2 through N - 1 to the right lyst.pop(0) Removes the element at the first position and then shifts the remaining elements to the left by one position lyst.remove(5) Removes the first instance of 5 in the list lyst.reverse() Reverses the elements lyst.sort() Sorts the elements in ascending order