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.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Course A201: Introduction to Programming 10/28/2010.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
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.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Programming for Linguists An Introduction to Python 24/11/2011.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
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.
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University
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.
Built-in Data Structures in Python An Introduction.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
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.
Lists. The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
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.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
More Strings CS303E: Elements of Computers and Programming.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
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
Chapter 10 Loops: while and for CSC1310 Fall 2009.
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.
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.
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.
INTRO2CS Tirgul 4 1. What will we see today?  Strings  Lists  Tuples  Mutable and Immutable  Iterating over sequences  Nested Loops  Shallow and.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
From Think Python How to Think Like a Computer Scientist
Strings Part 1 Taken from notes by Dr. Neil Moore
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists Part 1 Taken from notes by Dr. Neil Moore
Introduction to Strings
Chapter 7: Strings and Characters
Bryan Burlingame 03 October 2018
Lists in Python.
Creation, Traversal, Insertion and Removal
CHAPTER THREE Sequences.
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
CEV208 Computer Programming
Lists A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are.
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Basic String Operations
Strings and the slice operator
15-110: Principles of Computing
Topics Basic String Operations String Slicing
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
Python Review
Topics Basic String Operations String Slicing
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Python List.
Introduction to Computer Science
Presentation transcript:

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 List operations Lists slices

Strings

4 A string is a sequence A string is a sequence of characters. You can access the characters one at a time with the bracket operator: This statement selects character number 1 from fruit and assigns it to letter. The expression in brackets is called an index. The index indicates which character in the sequence you want (hence the name). But you might not get what you expect. Why?

5 Indexes For computer scientists, the index is an offset from the beginning of the string, and the offset of the first letter is zero. You can use any expression, including variables and operators, as an index, but this value has to be an integer. Otherwise you get an error:

6 Strings have length len is a built-in function that returns the number of characters in a string

7 Strings have methods A method is another word for a function. For example, the method upper takes a string and returns a new string with all uppercase letters

8 Many methods

9 Strings have special operators The word in is a Boolean operator that takes two strings and returns True if the first string is as a substring of the second one You can concatenate strings using the + sign You can repeatedly copy a string using the * sign

10 Strings Are Comparable The relational operators work on strings. To see if two strings are equal Other relational operations are useful for putting words in alphabetical order: Uppercase letters come before all the lowercase letters, so:

11 The Traversal Pattern

12 Traversal Example What is the output of this program?

13 A segment of a string is called a slice Selecting a slice is similar to selecting a character: Careful! It printed from 0 to 4-1

14 Strings are immutable Immutable means you can’t change an existing string. The best you can do is create a new string that is a variation on the original

Lists

16 A list is a sequence Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in a list are called elements or items. There are several ways to create a new list; the simplest is to enclose the elements in square brackets [ ] The elements do not have to be of the same type: A list within another list is nested

17 More About Lists A list that contains no elements is called an empty list. You can create one with empty brackets []. You can assign list values to variables:

18 Lists are mutable For accessing the elements of a list we also use the bracket operator. The expression inside the brackets specifies the index. Unlike strings, lists are mutable, which means that you can change the value of an element by using the bracket operator:

19 The in operator works with lists too

20 A for loop is good for traversing a list If you want to write or update the elements, you need the indices. A common way to do that is to combine the functions range and len : A for loop over an empty list never executes the body: Nested list still counts as a single element. (0)(1)(2) (3)

21 The + operator concatenates lists List Operators The * operator copies a list a given number of times

22 Built-in methods that operate on lists append adds a new element to the end of a list extend takes a list as an argument and appends all of the elements sort arranges the elements of the list from low to high List methods are all void; they modify the list and return None. If you write t = t.sort(), you will be disappointed with the result.

23 Deleting elements If you know the index of the element you want, you can use pop If you don’t need the removed value, you can use the del operator If you know the element you want to remove (but not the index), you can use remove

24 The slice operator also works on lists If you omit both, the slice is a copy of the whole list. If you omit the first index, the slice starts at the beginning. If you omit the second, the slice goes to the end.

25 An assignment statement can update multiple elements

26 Summary String: –A string is a sequence of characters. You can access the characters one at a time with the bracket operator. String operations: –Python provides built-in string operations (i.e., len ), methods (i.e., upper() ), and operators (i.e., in, +, * ) Traversing strings: –The best way to travers a string is using a for loop. String slices: –Selecting a slice is similar to selecting a character (i.e., s[0:6] )

27 Summary List: –A list is a sequence of values. The values in a list are called elements or items, they can be of any type. Traversing lists: –The in operator works with lists too. A for loop is good for traversing a list. But, if you want to write or update the elements, you need the indices. List operations: –The + concatenates lists. The * operator repeats a list a given number of times. del is used for deleting an element(s). Built-in methods (i.e., append, extend, sort, pop, remove, etc.). List slices: –If you omit the first index, the slice starts at the beginning. –If you omit the second, the slice goes to the end. –if you omit both, the slice is a copy of the whole list

28 OpenLab and Blackboard Check OpenLab for any new lab. Check Blackboard for any new quiz.