Chapter 4 Strings & Tuples

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Guide to Programming with Python
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
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.
Guide to Programming with Python
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
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.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
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.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
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.
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.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
Python - Lists.
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Intro to CS Sept 22/23, 2015.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Quiz 2 this week.
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
8 – Lists and tuples John R. Woodward.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By Himanshi dixit 11th ’B’
Class code for pythonroom.com cchsp2cs
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Tuple.
Introduction to Computer Science
Presentation transcript:

Chapter 4 Strings & Tuples Part II

Creating Tuples Tuples are a type of sequence that can contain elements of any type (unlike a string which can only contain characters) Tuple elements don’t have to all be the same type. Ex. Could be both strings and numbers combined Ex. Could be a sequence of graphic images or sound files. Whatever you can assign to a variable, you can group together and store as a sequence in a tuple.

Creating Tuples Continued To create a tuple, you surround a sequence of values, separated by commas, with parentheses. Ex. Inventory = (“sword”, “armor”, shield”, “healing potion”) If you don’t put anything inside the parenthesis it is an empty tuple Ex. Inventory = ( ) You can write a tuple on one line or span it across multiple lines, as long as you end each line with a comma.

Tuples Continued Tuples can be treated as conditions just like any other value. Ex. If not inventory: print(“You are empty handed”) As a condition, an empty tuple is False and a tuple with at least one element is True.

Printing Tuples Though a tuple can contain many elements, you can print the entire tuple just like you would any single value. Ex. grocerylist = (“bread”, “milk”, “eggs”) print(grocerylist) This will print (‘bread’, ‘milk’, ‘eggs’) You can also loop through a tuple so that every element is printed individually Ex. for item in grocerylist: print(item) This will print: bread milk eggs

Using Tuples Note: Other programming languages offers structures similiar to tuples, however in those languages you can’t mix elements of different types in the sequences. For example, you couldn’t mix strings and numbers. You can do anything with a tuple that you can with a string Get length Print each element with a for loop Use the in operator to test if an element is a tuple Index, slice, and concatenate

Using the len() function Ex. print(“You have”, len(grocerylist), “items on your list.” This will print “You have 3 items on your list” Using the in Operator Ex. If “eggs” in grocerylist: print(“You can make an omelet this week”) Slicing Tuples Every element in the tuple corresponds to a position Tuples can be sliced just like other strings Ex. print(grocerylist[0,2]) would print “bread” and “milk”

Tuple Immutability Just like strings, tuples are immutable (you can’t change them Even though you can’t change them you can build on them by concatentating Ex. forgottenitems =(“PB”, “Jelly”) grocerylist +=forgottenitems Your grocery list would now include: bread, milk, eggs, PB, Jelly