Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set

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 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
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.
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.
Lists Introduction to Computing Science and Programming I.
Guide to Programming with Python
Introduction to Python
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
7 - Programming 7J, K, L, M, N, O – Handling Data.
String and Lists Dr. José M. Reyes Álamo.
Intro to CS Nov 21, 2016.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
COMPSCI 107 Computer Science Fundamentals
Python unit_4 review Tue/Wed, Dec 1-2
ITM 352 Data types, Variables
Python Variable Types.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Containers and Lists CIS 40 – Introduction to Programming in Python
loops for loops iterate over a given sequence.
Foundations of Programming: Arrays
From Think Python How to Think Like a Computer Scientist
Section 6: Sequences Chapter 4 and 5.
Ruth Anderson CSE 140 University of Washington
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Arrays, For loop While loop Do while loop
And now for something completely different . . .
Bryan Burlingame 03 October 2018
Lists in Python.
CHAPTER THREE Sequences.
Guide to Programming with Python
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
6. Lists Let's Learn Python and Pygame
Building Java Programs
Coding Concepts (Data Structures)
String and Lists Dr. José M. Reyes Álamo.
Ruth Anderson UW CSE 160 Winter 2017
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Building Java Programs
Michael Ernst CSE 140 University of Washington
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Ruth Anderson CSE 160 University of Washington
Data Structures & Algorithms
And now for something completely different . . .
Python Review
Ruth Anderson UW CSE 160 Spring 2018
Data Types and Maths Programming Guides.
Class code for pythonroom.com cchsp2cs
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Dictionary.
Selamat Datang di “Programming Essentials in Python”
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set We’ll do more with dictionaries later

Lab notes Interactive run – hard to tell when program’s output begins and ends – what can you do? Review Battleship How would you change to handle real game?

Sorting Often we want to sort data in a list, but our list does not contain atoms like single numbers. Need to tell Python how to compare elements Analogous to comparators in Java. Steps: Create function, taking 2 arbitrary elements from your list. Return positive / negative / zero. Call: list.sort(comparatorFunction) See example handout (compare.py)

Tuple Similar to list: use ( ) instead of [ ] Good for identifying a point in some space, like an ordered pair, triple Often an anonymous object Immutable – not meant to be updated, just throw away We just have count & index functions Syntax is straightforward: multiple assignment Can re-use variables later (a, b, c) = (10, 8, 2)

Dictionary A nice array Index can be anything that Python can easily evaluate, such as a single int, float or string. Typical procedure: Initialize as { } Add element by assignment, e.g. d[“USA”] = 308 Can traverse dictionary elegantly for i in d: print i, d[ i ]

Notes In a dictionary, like a set, the order of the data is irrelevant. The “key” is already an index. Example: { “Finland”: 5, “India”: 1150, “USA”, 308, “France” : 61 } Don’t rely on Finland being at the “beginning” of the dictionary. The value 5 is obtained by d[“Finland”], not d[0] ! Python can quickly find your data in a dictionary 

Illustration “Kevin” in d  returns True “James” in d  returns False d[“Tina”] = 3 d[“Kevin”] = 2 “Kevin” in d  returns True “James” in d  returns False d [“James”] gives a KeyError, so when in doubt, check to see that key actually exists!

Applications Good for sparse array: only store the values you actually use. Here, the value at each key can be a list: f [1920] = [90, 230] f [1920][0] = 90 1920 1951 1960 1986 2000 2010 90 150 650 6656 18768 37728 230 700 750 3176 5144 9572

Applications (2) Excellent for maintaining data E.g. Stock portfolio: reading a list of buy/sell transactions. At any point in time we may want to know total holdings Can remove an element from a dictionary using del: del portfolio[“IBM”] del enemy[“Soviet Union”]

Set Essentially, a set is a list in which the elements do not repeat. (See chapter 7) E.g. Useful when you need a lot of boolean values Can convert a list to a set by using set( ). s = set([1, 3, 5, 7, 9]) If you call set( ) on a string, you get a set of its characters! Operations in not in & | - ^ Note that you can’t use ~