Lists vs Strings CMSC 201. Overview Python strings and lists are similar but different. Similar: syntax, access mechanisms, operators Different: Strings.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
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.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
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.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
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 CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Strings CS303E: Elements of Computers and Programming.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Lists CS303E: Elements of Computers and Programming.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
A: A: double “4” A: “34” 4.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
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.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
String and Lists Dr. José M. Reyes Álamo.
Using Molecular Biology to Teach Computer Science
Python unit_4 review Tue/Wed, Dec 1-2
CSc 120 Introduction to Computer Programing II Adapted from slides by
CMSC201 Computer Science I for Majors Lecture 14 – Tuples
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings in Python Creating a string.
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
Chapter 7: Strings and Characters
Lists in Python.
CHAPTER THREE Sequences.
An aggregation mechanism
8 – Lists and tuples John R. Woodward.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Lists in Python Creating lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Part 1 Taken from notes by Dr. Neil Moore
Basic String Operations
Michael Ernst CSE 140 University of Washington
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
And now for something completely different . . .
Introduction to Strings
Python Review
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CSE 231 Lab 6.
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More Basics of Python Common types of data we will work with
Presentation transcript:

Lists vs Strings CMSC 201

Overview Python strings and lists are similar but different. Similar: syntax, access mechanisms, operators Different: Strings are immutable, lists are mutable Strings are homogeneous, lists are heterogeneous myString[1] is a string, myList[1] might not be a list

Similar myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myString = "abcdefghijklmnopqrstuvwxyz" Use the len( ) function to get the size. len(myList) == 10 len(myString) == 26

Similar Empty list and empty string: myList = [] myString = ""

Similar myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myString = "abcdefghijklmnopqrstuvwxyz" Use the [] to get components. myList[3] gives 3 myString[3] gives "d" myList[-1] gives 9 myString[-2] gives "y"

Similar myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myString = "abcdefghijklmnopqrstuvwxyz" Use the [:] to get sub-lists and substrings. myList[3:7] gives [3, 4, 5, 6] myString[3:7] gives "defg" This is the slicing operator.

Similar myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myString = "abcdefghijklmnopqrstuvwxyz" More slicing tricks. myList[:7] gives [0,1, 2, 3, 4, 5, 6] myString[:7] gives "abcdefg" myList[-3:] gives [7, 8, 9] myString[-3:] gives "xyz"

Similar L1 = [0, 1, 2, 3 ] L2 = [ 4, 5] str1 = "abcdefghij" str2 = "klmno" Use + operator for concatenation. L1 + L2 gives [0,1, 2, 3, 4, 5] str1 + str2 gives "abcdefghijklmno"

Different: Lists are mutable myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myList[4] = 44 changes myList to become: [0, 1, 2, 3, 44, 5, 6, 7, 8, 9]

Different: Lists are mutable myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myList[4:7] = [11, 22] changes myList to become: myList = [0, 1, 2, 3, 11, 22, 7, 8, 9]

Diff: Strings are immutable myString = "abcdefghijklmnopqrstuvwxyz" myString[4] = "X" is an error

Strings are homogeneous myString = "abcdefghijklmnopqrstuvwxyz" each myString[i] has the same type, a string.

Lists can be heterogeneous myList= [ "car", 5, True, 76.2 ] is perfectly legal. myList[0] is a string myList[1] is an int myList[2] is a boolean myList[3] is a float

Lists can be heterogeneous myList= ["car", 5, [True, "blue"], 76.2] is perfectly legal. myList[0] is a string myList[1] is an int myList[2] is a list myList[3] is a float

Different myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myString = "abcdefghijklmnopqrstuvwxyz" Then, myList[7] is an integer myString[7] is a string "h" myString and myString[7] are both strings.