CMSC201 Computer Science I for Majors Lecture 16 – Tuples

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
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.
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.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Built-in Data Structures in Python An Introduction.
CIT 590 Intro to Programming Lecture 5 – completing lists.
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.
Lists CS303E: Elements of Computers and Programming.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
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.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
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.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
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.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
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.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CMSC201 Computer Science I for Majors Lecture 14 – Tuples
Tuples and Lists.
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Announcements Project 4 due Wed., Nov 7
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
Bryan Burlingame Halloween 2018
CISC101 Reminders Slides have changed from those posted last night…
CS190/295 Programming in Python for Life Sciences: Lecture 6
8 – Lists and tuples John R. Woodward.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By- Anshul Kumar Class: XI “B”
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Introduction to Computer Science
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By Himanshi dixit 11th ’B’
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 16 – Tuples Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from the book author, and previous iterations of the course

Last Class We Covered “Good” code Code design Readability Adaptability Top-down design Modular development

Any Questions from Last Time?

Today’s Objectives Learn about the tuple data structure in Python Perform basic operations with tuples including: Creation Conversion Repetition Slicing Traversing Use tuples in functions (as return values)

Tuples

The Tuple Data Structure In Python, a tuple is an immutable sequence of values What does immutable mean? Tuples are immutable which means you cannot update or change the values of tuple elements http://www.ou.edu/memorylab/python/Lsn15_Tuples.ppt

The Tuple Data Structure Each value in the tuple is an element or item Elements can be any Python data type Tuples can mix data types Elements can be nested tuples year_born = ("Paris Hilton", 1981) tuple name first element: a string second element: an integer

Creating Tuples

Creating Tuples The empty tuple is written as two parentheses containing nothing tup1 = () To cast a list as a tuple, you use tuple() myList = [5, 15, 23] myTuple = tuple(myList) print(type(myTuple)) <class 'tuple'>

Creating Tuples numbers = (1, 2, 3, 4) print (numbers) (1, 2, 3, 4) cheeses = ('swiss', 'cheddar', 'ricotta', 'gouda') print (cheeses) ('swiss', 'cheddar', 'ricotta', 'gouda')

Tuples with one element require a comma Creating Tuples t1 = ('a') print (t1, type(t1)) a <class 'str'> Is this a tuple? t2 = ('a',) print (t2, type(t2)) ('a',) <class 'tuple'> Tuples with one element require a comma

Creating Tuples t3 = tuple('a') print (t3, type(t3)) empty = tuple() print (empty) ('a',) <class 'tuple'> ()

Creating Tuples aList = [1, 2, 3, 4] aTuple = tuple(aList) print (aTuple) aStr = 'parrot' aTuple2 = tuple(aStr) print (aTuple2) What does this output? (1, 2, 3, 4) ('p', 'a', 'r', 'r', 'o', 't')

Indexing and Slicing Tuples

Tuple Indexing Just like other sequences (strings), elements within a tuple are indexed cheeses = ('swiss', 'cheddar', 'ricotta', 'gouda') print (cheeses[0]) cheeses[0] = 'swiss' Tuples are immutable. What does this do? Nothing! (an error)

Slicing a Tuple Like other sequences, tuples can be sliced Slicing a tuple creates a new tuple. It does not change the original tuple. cheeses = ('swiss', 'cheddar', 'ricotta', 'gouda') print (cheeses[1:4]) ('cheddar', 'ricotta', 'gouda') What does this output?

Tuple Operations

Operations on Tuples Tuples support all the standard sequence operations, including: Membership tests (using the in keyword) Comparison (element-wise) Iteration (e.g., in a for loop) Concatenation and repetition The len() function The min() and max() functions

Membership Tests (in) In Python, the in keyword is used to test if a sequence (list, tuple, string etc.) contains a value. Returns True or False a = [1, 2, 3, 4, 5] print(5 in a) print(10 in a) True False What does this output?

Comparison In Python 3.3, we can use the comparison operator, ==, to do tuple comparison Returns True or False tuple1, tuple2 = (123, 'xyz'), (456, 'abc') tuple3 = (456, 'abc') print (tuple1==tuple2) print (tuple2==tuple3) What does this output? False True From: http://www.tutorialspoint.com/python/tuple_cmp.htm

Iteration teams = ((1, 'Ravens'),(2, 'Panthers'), (5, 'Eagles'),(7, 'Steelers')) for (index, name) in teams: print(index, name) Notice tuple of tuples What does this output? 1 Ravens 2 Panthers 5 Eagles 7 Steelers

Iteration t = [('a', 0), ('b', 1), ('c', 2)] for letter, number in t: print (number, letter) Notice list of tuples What does this output? 0 a 1 b 2 c

Concatenation (+) The + operator returns a new tuple that is a concatenation of two tuples a = (1, 2, 3) b = (4, 5, 6) c = a + b print (a, b, c) (1, 2, 3) (4, 5, 6) (1, 2, 3, 4, 5, 6) What does this output?

Repetition (*) The * operator returns a new tuple that repeats the tuple. a = (1, 2, 3) b = (4, 5, 6) print (a*2, b) (1, 2, 3, 1, 2, 3) (4, 5, 6) What does this output?

len() Functions The method len() returns the number of elements in the tuple. tuple0 = () print(len(tuple0)) tupleA = ("UMBC", "is", "the", "best") print(len(tupleA)) What does this output? 4

min() and max() Functions max(tuple) Returns item from the tuple with max value min(tuple) Returns item from the tuple with min value What does this output? myTuple = tuple('parrot') print (myTuple) print(min(myTuple)) print(max(myTuple)) ('p', 'a', 'r', 'r', 'o', 't') a t

Tuples and Functions (return)

Tuples and functions Python functions (as is true of most languages) can only return one value But… but… we’ve returned multiple values before! If multiple objects are packaged together into a tuple, then the function can return the objects as a single tuple Many Python functions return tuples

Example: min_max.py (3, 98) (' ', 'w') What does this output? # Returns the smallest and largest # elements of a sequence as a tuple def min_max(t): return min(t), max(t) seq = [12, 98, 23, 74, 3, 54] print (min_max(seq)) string = 'She turned me into a newt!' print (min_max(string)) myMin, myMax = min_max(string) What does this output? (3, 98) (' ', 'w')

Passing Tuples as Parameters

Passing Tuples as Parameters A parameter name that begins with * gathers all the arguments into a tuple This allows functions to take a variable number of parameters So we can call the function with one, or two, or twenty parameters! (An actual parameter is also called an argument) From: https://docs.python.org/3.3/faq/programming.html#faq-argument-vs-parameter

Actual Parameters (or Arguments) Example def printall(*args): print (args) printall(1, 2.0, 'three') Actual Parameters (or Arguments)

Example: pointless.py What does this output? def pointless(required, *args): print ('Required:', required) if args: print('Others: ', args) pointless(1) pointless(1, 2) pointless(1, 2.0, 'three') pointless(1, 2.0, 'three', [4]) What does this output? Required: 1 Others: (2,) Others: (2.0, 'three') Others: (2.0, 'three', [4])

Any Other Questions?

Announcements Homework 7 is due Due by Monday (April 4th) at 8:59:59 PM Project 1 will come out Monday night Next time: Dictionaries