Section 6: Sequences Chapter 4 and 5.

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

Container Types in Python
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
2/7/2008. >>> Overview * boolean * while * random * tuples.
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.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
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.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
January 24, 2006And Now For Something Completely Different 1 “And Now For Something Completely Different” A Quick Tutorial of the Python Programming Language.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
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.
If statements while loop for loop
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
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.
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 Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.
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.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
String and Lists Dr. José M. Reyes Álamo.
Scientific Programming in Python -- Cheat Sheet
Section06: Sequences Chapter 4 and 5.
Python unit_4 review Tue/Wed, Dec 1-2
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Sequences and Indexing
When to use Tuples instead of Lists
CSc 120 Introduction to Computer Programing II
CS 115 Lecture 8 Structured Programming; for loops
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
CISC101 Reminders Quiz 2 this week.
Bryan Burlingame 03 October 2018
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
Datatypes in Python (Part II)
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Topics Sequences Introduction to Lists List Slicing
Dictionaries Dictionary: object that stores a collection of data
(more) Python.
CS1110 Today: collections.
For loops Taken from notes by Dr. Neil Moore
Topics Sequences Lists Copying Lists Processing Lists
By- Anshul Kumar Class: XI “B”
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Topics Sequences Introduction to Lists List Slicing
Python Review
Sequences Sequences are collections of data values that are ordered by position A string is a sequence of characters A list is a sequence of any Python.
CSE 231 Lab 7.
Dictionary.
Python List.
Tuple.
Presentation transcript:

Section 6: Sequences Chapter 4 and 5

General Description "Normal" variables Sequence variables: x = 19 The name "x" is associated with a single value Sequence variables: T = (5, 17, 19, -1) A name which is associated with a group of values.

Types of Python sequences Immutable (create once, can’t edit) strings: a collection of glyphs tuples: a collection of python objects range objects: a collection of integer (index #’s) Mutable (can add / remove / etc) lists: like tuples, but slower dictionaries: a mapping from key to value

Creating sequences with existing data s = “abc” # string t = (1, 4.2, “def”) # tuple L = [1, 4.2, “def”] # list D = {“joe”: 1, “bob”: 4.2, “sue”: “def”} # dictionary Key word: delimiter

Creating sequences with existing data As before s = “abc” # string t = (1, 4.2, “def”) # tuple L = [1, 4.2, “def”] # list D = {“joe”: 1, “bob”: 4.2, “sue”: “def”} # dictionary New operations len(s) # 3 len(t) # 3 len(L) # 3 len(D) # 3 “a” in s # True 4.2 in L # True “bob” in D # True 4.2 in D # False – see why? s + “yyy” # abcyyy t + (9, 10) # (1, 4.2, “def”, 9, 10) # no dictionaries!

Indexing Sequences (except Dictionaries) Index number identifies a position. s = "Oranges" Examples: >>> s[0] # 'O' >>> s[2] # 'a' >>> s[-1] # 's' >>> s[-2] # 'e' The indexed value can be used anywhere a "normal" variable / constant would -7 -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 O r a n g e s

Indexing Dictionaries D = {“joe”: 1, “bob”: 4.2, “sue”: “def”} # dictionary print(D[“joe”]) # 1 print(D[“sue”]) # def print(D[“xyz”]) # Error! print(D) # { “bob”: 4.2, “sue”: “def”, “joe”: 1} Note: with dictionaries, there is no ordering!

Slicing Sequences Returns a section of a sequence Doesn't work for Dictionaries. >>> s = "Apples-Oranges" >>> s[1:4] # "ppl" >>> s[5:9] # "s-Or" >>> s[-6:-1] # "range" >>> s[-5:-7] # "" >>> s[10:] # "nges" >>> s[:6] # "Apples" >>> s[1:10:2] # "plsOa" >>> s[12:8:-1] # "egna" >>> s[-1::-1] # "segnarO-selppA" (backward) -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 7 8 9 10 11 12 13 A p l e s - O r a n g

counting and indexing L = [“ab”, “c”, “def”, “gh”] L.count(“ab”) # 1 L.count(“x”) # 0 L.index(“c”) # 1 L.index(“x”) # Error! # Note: none of these work on dictionaries.

list-only methods L = [“a”, “b”, “c”] L.append(“d”) print(L) # [a, b, c, d] L.insert(1, “x”) print(L) # [a, x, b, c, d] L.remove(“a”) # The first one… print(L) # [x, b, c, d] del L[-1] print(L) # [x, b, c] L[0] = “y” print(L) # [y, b, c]

Micro-labs Frist one: Second one: Have the user enter a string. Convert it to lower-case (s = s.lower()) Determine if it’s a palindrome Second one: Find and print all prime numbers from 2 to n Make a list of all numbers Remove a number if it’s evenly divisible by anything that comes before.

a new random function random.choice(sequence) Returns a random element of sequence. s = "ABCDEFG" print(random.choice(s)) Good quiz question: do it manually.

for loops A type of repetition, but tied to a sequence. Syntax: for var in seq: # for-loop block The number of items in seq controls the number of iterations. var is a variable, created / updated by python A reference to the "current" item in the sequence. Equivalent to: i = 0 while i < len(seq): var = seq[i] i += 1 # for-loop block

for loop example s = "ABCDE" for c in s: print(c) Outputs: A B C D E # Doing the same thing with a while loop i = 0 while i < len(s): c = s[i] print(c) i += 1

range objects range([start,] end [, step]) Treated as a sequence (sort of) Useful when used with for loops Example: s = "ABCDE" for i in range(len(s)): # prints 0,1,2,3,4 print(i) for i in range(2,10,3): # prints 2,5,8

Pulling back the pygame curtain Discuss how this “magic” used sequences: Accessing RGB of color tuples The key and mouse pressed (and pos) tuples key_pressed lists rectangles …

2D Tuples / Lists A common name for a tuple with tuple (sub-)elements Example: T = ((15,8,7), (3,2,9), (1,-2,10)) T[0] # (15,8,7) T[0][1] # 8 T[0:2] # ((15,8,7), (3,2,9)) T[0][1:] # (8,7) T[1] + T[2] # (3,2,9,1,-2,10) T[1] + T[1] # (3,2,9,3,2,9) T[1] * 3 # (3,2,9,3,2,9,3,2,9) T[1][1] + T[2][-1] # 12

Examples! [scrolling star field] [simple particle-effects] [simple tile-worlds]