Tuples.

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:
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
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.
Python Programming Chapter 9: Tuples Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
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.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
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.
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
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists Victor Norman CS104. Reading Quiz Q1: What is printed by the following statements? alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False] print(3.14.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Built-in Data Structures in Python An Introduction.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Last Week if statement print statement input builtin function strings and methods for loop.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Let’s get started!.
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.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. 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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
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.
Python Let’s get started!.
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
Tuples and Lists.
Section 6: Sequences Chapter 4 and 5.
Repetition: the for loop
CHAPTER 7 STRINGS.
Python Primer 2: Functions and Control Flow
CHAPTER THREE Sequences.
Data types Numeric types Sequence types float int bool list str
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Lists in Python Outputting lists.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Basic String Operations
Introduction to Strings
Reference semantics, variables and names
(more) Python.
Topics Basic String Operations String Slicing
Introduction to Strings
Python Review
“Everything Else”.
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Topics Basic String Operations String Slicing
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Tuple.
Presentation transcript:

Tuples

Tuple Similar to a list except it is immutable. Syntactically, it is comma-seperated list of values. tuple = 'a', 'b', 'c', 'd', 'e‘ To create a tuple with a single element, we have to include the comma: t1 = ('a',) type(t1) <type 'tuple'> Without the comma, Python treats ('a') as a string in parentheses: t2 = ('a') type(t2) ??

The operations on tuples are the same as the operations on lists The index operator selects an element from a tuple. tuple = ('a', 'b', 'c', 'd', 'e') tuple[0] 'a‘ The slice operator selects a range of elements. tuple[1:3] ('b', 'c‘) But if we try to modify one of the elements of the tuple, we get an error. tuple[0] = 'A' TypeError: object doesn't support item assignment

Example we can't modify the elements of a tuple, we can replace it with a different tuple: tuple = ('A',) + tuple[1:] tuple ('A', 'b', 'c', 'd', 'e')

Tuple Assignmnt To swap the values, using conventional assignment statements, we have to use the assignment statement with a temporary variable. temp = a a = b b = temp Python provides a form of tuple assignment that solves this problem neatly: a, b = b, a The number of variables on the left and the number of values on the right have to be the same.

Tuples as Return Values Functions can return tuples as return values. For example: def swap(x, y): return y, x Then we can assign the return value to a tuple with two variables: a, b = swap(a, b)

def swap(x, y): # incorrect version x, y = y, x If we call this function like this: swap(a, b) a and x are aliases for the same value. Changing x inside swap makes x refer to a different value, but it has no effect on a in main . Similarly, changing y has no effect on b.

Random Numbers The random module contains a function called random that returns a floating point number between 0.0 and 1.0. Each time you call random, you get the next number in a long series. from random import* for i in range(10): x=random() print x

List of Random Numbers Output: def randomList(n): randomList(8) for i in range(n): t[i] = random.random() return t Output: randomList(8) 0.15156642489 0.498048560109 0.810894847068 0.360371157682 0.275119183077 0.328578797631 0.759199803101 0.800367163582

Counting Divide the problem into sub problems and look for sub problems that fit a computational pattern Traverse a list of numbers and count the number of times a value falls in a given range. Eg: def inBucket(t, low, high): count = 0 for num in t: if low < num < high: count = count + 1 return count This development plan is known as pattern matching.

Many Buckets With two buckets, range will be: low = inBucket(a, 0.0, 0.5) high = inBucket(a, 0.5, 1) But with four buckets it is: bucket1 = inBucket(a, 0.0, 0.25) bucket2 = inBucket(a, 0.25, 0.5) bucket3 = inBucket(a, 0.5, 0.75) bucket4 = inBucket(a, 0.75, 1.0)

If the number of buckets is numBuckets, then the width of each bucket is 1.0 / numBuckets. buckets = [0] * numBuckets bucketWidth = 1.0 / numBuckets for i in range(numBuckets): low = i * bucketWidth high = low + bucketWidth buckets[i] = inBucket(t, low, high) print buckets

def inBucket(t, low, high): count = 0 for num in t: def randomList(n): t = [0] * n for i in range(n): t[i] = random.random() return t def inBucket(t, low, high): count = 0 for num in t: if low < num < high: count = count + 1 return count numBuckets = 8 buckets = [0] * numBuckets bucketWidth = 1.0 / numBuckets for i in range(numBuckets): low = i * bucketWidth high = low + bucketWidth buckets[i] = inBucket(t, low, high) print buckets