1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 7 Strings, Lists.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Character and String definitions, algorithms, library functions Characters and Strings.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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.
Guide to Programming with Python
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
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.
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.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Strings CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Lists CS303E: Elements of Computers and Programming.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
1 Computer Graphics and Games MONT 105S, Spring 2009 Lecture 2 Simple Python Programs Working with numbers and strings.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
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.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
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.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Generating Random Numbers
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
String class.
CS 100: Roadmap to Computing
Lecture 10 Data Collections
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Lists in Python.
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
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
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Introduction to Strings
CS 100: Roadmap to Computing
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 7 Strings, Lists

2 Generating Random Numbers Python provides libraries of functions that perform useful tasks. One of these libraries allows us to generate random numbers. To use the library, we must first import it: import random#This should be at the beginning of #the program To generate a random integer between 0 and x-1: myNumber = random.randrange(x) To generate a random number between low and high - 1: myNumber = random.randrange(low, high) Examples: points = random.randrange(10) #random number between 0 and 9 target = random.randrange(2, 11)#random number between 2 and 10

3 Example using random # Program: points.py # Purpose: Generate random numbers of points import random# import library for random numbers count = 0 while count < 5: points = random.randrange(1, 11) print "You got", points, "points!" count = count + 1 #What is the output?

4 Collections Python provides several classes called collection that allow us to group data together. Sequential Collections: Strings--A sequential collection of characters Lists--A sequential collection of python objects Tuples--A specialized list whose items cannot be modified. Non-sequential Collections: Dictionaries-- Nonsequential collections of pairs of items. Each pair has a key and a value. The key can be used to look up the value.

5 Recall Strings A string is a sequential collection of characters. The value of a string is given within quotes: "dog" "alpha centauri" "$37.62" Each character in a string can be accessed by its position. >>> monster = "fire-breathing dragon" >>> monster[0] 'f' >>> monster[2] 'r'

6 Things we can do with Strings OperationOperatorExample Concatenation +"my " + "goodness" -> "my goodness" Repetition *"ha" * 3 -> "hahaha" Length lenlen("fire") -> 4 Substring [ : ]monster[1:4] -> "ire" # monster = "fire-breathing dragon" Membership in"ir" in monster -> True "w" in monster -> False

7 Other Methods with Strings Object: A member of a class Method: An action the object can take To have an object carry out a method, we use "dot notation" myObject.methodName( ) myString.count(item)#number of occurrences of item in #myString Example: >>> monster = "troll" >>> monster.count("l") 2 >>> monster.count("rol") 1

8 More String Methods myString.upper( )#returns a string with all upper case myString.lower( )#returns a string with all lower case myString.find(substring) #returns index of 1st occurrence of #substring, or -1 if not found Examples: >>> monster = "troll" >>> monster.upper( ) "TROLL" >>>monster "troll" >>> monster.find("ll") 3

9 Lists A list is a sequential collection of python objects. A list is written as a set of objects separated by commas,within square brackets: [1, 5, 7]["dog", "cat", "bird"][3.7, "spam", 46] [ ] We can access an element of a list by its position: Example: >>>inventory = ["Axe", "gold coins", "torch"] >>>inventory[2] "torch"

10 Things we can do with Lists OperationOperatorExample Concatenation +[4.5, "Mary"] + [6, 2] -> [4.5, "Mary", 6, 2] Repetition *["house"] * 3 -> ["house", "house", "house"] Length leninventory = ["Axe", "gold coins", "torch"] len(inventory) -> 3 Sublist [ : ]prices = [2.98, 5.67, 1.36, 14.92] prices[1:3] -> [5.67, 1.36] Membership in5.67 in prices -> True

11 Changing an item in a list A list is stored in memory as a set of sequential id's referencing objects. alist = [3, "dog", "purple", 5.6] We can use indexing to assign a new value to any item in the list: >>> alist[2] = 6.7 >>> alist [3, "dog", 6.7, 5.6] Note: You cannot change a character in a string this way. idididid 3"dog""purple"5.6 alist

12 More fun with lists alist.append(item)#adds item to the end of the list alist.insert(i, item)#inserts item at the ith position in list alist.pop(i)#removes and returns the ith item alist.sort( )#Sorts the list alist.reverse( )#Reverses the list alist.index(item)#Returns the index of the 1st #occurrence of item alist.count(item)#Returns the number of occurrences #of item alist.remove(item)#Removes 1st occurrence of item

13 Some List examples >>> inventory = ["Axe", "Gold", "Torch"] >>> inventory.append("Matches") >>> inventory ["Axe", "Gold", "Torch", "Matches"] >>> inventory.index("Gold") 1 >>> inventory.sort( ) >>> inventory ["Axe", "Gold", "Matches", "Torch"]