Tuples and Lists.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 7 Strings, Lists.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
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.
Built-in Data Structures in Python An Introduction.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
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.
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.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
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.
String and Lists Dr. José M. Reyes Álamo.
Generating Random Numbers
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Chapter 4 Strings & Tuples
Basic operators - strings
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Arrays, For loop While loop Do while loop
Introduction to Strings
Introduction to Strings
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Guide to Programming with Python
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
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 4: Lists, Tuples and Dictionaries
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Introduction to Strings
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By Himanshi dixit 11th ’B’
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Tuple.
Introduction to Computer Science
Presentation transcript:

Tuples and Lists

Tuples Tuples are a type of sequence, like strings. Unlike strings (which can only contain characters) – tuples can contain any type of element Tuples can store high scores of a game, or groups of employee names, etc. Tuple elements don’t have to all be the same type (can have a tuple with strings and numbers) Tuples can have graphic images, sound files, etc.)

Creating Tuples Surround a sequence of values, separated by commas, with parenthesis Unlike lists, separated by brackets [ ] Empty tuple: Inventory = ( ) Open Hero’s Inventory program

Treating a Tuple as a Condition We learned about conditional statements and values (if/elif/else) You can treat any value in python as a condition, including tuples As a condition, an empty tuple is False. A tuple with at least one element is True. Example: not inventory is True, so the computer will print the string “You are empty-handed.”)

Creating a Tuple with Elements Each element in a Tuple is separated by a comma. In the Hero Inventory Program, each string is an element in the tuple. Notice: the tuple spans on multiple lines. You can write a tuple on one line, or span it, as long as you end each line with a comma.

Printing a Tuple When you print a tuple it will display off of the elements, surrounded by parenthesis Code: Output:

Looping Through a Tuples Elements A for loop iterates over (marches through) the elements in inventory and print each one individually Code: Output: Reminder: you can use a for loop to go through the elements of any sequence!

Hero’s Inventory 2.0 In this program, his inventory is counted, tested, indexed, and sliced. First part of the program is the same as the last

Using the len() Function with Tuples len(inventory) will print the number of elements in the tuple, inventory Code: Output: Notice: in the tuple, the string “healing potion” is counted as a single element, even though its 2 words

Using the in Operator with Tuples You can use the in operator with tuples to test for element membership The in operator is usually used to create a condition. The condition “healing potion” in inventory tests if the entire string “healing potion” is an element of inventory. Code: Output:

Indexing Tuples Indexing tuples works like indexing strings You specify a position number, in brackets, to access a particular element. The following code lets the user choose the index number and then the computer displays the corresponding element: Output when index = 2:

Diagram of tuple inventory with index numbers: Each string is a single element in the tuple 1 2 3 “sword” “armor” “shield” “healing potion” - 4 - 3 - 2 -1

Slicing Tuples Works the same as slicing strings. Give a beginning and ending position Result: every element between those positions This program lets the user pick the beginning and ending Code:

Slicing Tuples Output using beginning position as -1 and ending position -4:

Diagram of tuple slicing: 1 2 4 “sword” “armor” “shield” “healing potion” - 4 - 3 - 2 -1

Understanding Immutability Like strings, tuples are immutable. That means you can’t change a tuple. Example: Type in the following code and run it. Although you can’t change tuples, like strings, you can create new tuples from existing ones.

Concatenating Tuples You can concatenate tuples the same way you concatenate strings. You simply join them together with +, the concatenation operator:

Takeaways: Tuple notation Elements of a tuple do not have to be values of the same time. Single tuple can contain integers, strings, floating point numbers, etc. You can get lengths of a tuple Print each element with a for loop Use the in operator to test if an element is in the tuple You can index, slice, and concatenate tuples