Notebook. March 30, 2019

Similar documents
CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Dictionaries. Upsorn Praphamontripong. CS 1111 Introduction to Programming Spring 2018

MUTABLE LISTS AND DICTIONARIES 4

Python Tutorial. CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Collections. Lists, Tuples, Sets, Dictionaries

Data Structures. Lists, Tuples, Sets, Dictionaries

ENGR 102 Engineering Lab I - Computation

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Python for Non-programmers

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Python Intro GIS Week 1. Jake K. Carr

The Practice of Computing Using PYTHON

Iterators & Generators

Python Review IPRE

The Pyth Language. Administrivia

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

MEIN 50010: Python Data Structures

Topic 7: Lists, Dictionaries and Strings

CMSC201 Computer Science I for Majors

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions

CS 234 Python Review Part 2

Python Review IPRE

University of Texas at Arlington, TX, USA

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

List of squares. Program to generate a list containing squares of n integers starting from 0. list. Example. n = 12

Lecture Agenda. Objects. But First... Immutable Types and Nesting. Immutable Types and Nesting

1 Strings (Review) CS151: Problem Solving and Programming

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Lists CS 1111 Introduction to Programming Fall 2018

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Lecture #11: Immutable and Mutable Data. Last modified: Sun Feb 19 17:03: CS61A: Lecture #11 1

Types, lists & functions

Text Input and Conditionals

Using Scala in CS241

Slicing. Open pizza_slicer.py

Data Structures. Dictionaries - stores a series of unsorted key/value pairs that are indexed using the keys and return the value.

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Advanced Algorithms and Computational Models (module A)

[301] Objects/References. Tyler Caraza-Harter

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS Advanced Unix Tools & Scripting

: Intro Programming for Scientists and Engineers Final Exam

Introduction to Python

Lists How lists are like strings

STATS Data Analysis using Python. Lecture 8: Hadoop and the mrjob package Some slides adapted from C. Budak

Chapter 1. Data types. Data types. In this chapter you will: learn about data types. learn about tuples, lists and dictionaries

CMSC201 Computer Science I for Majors

CMSC 201 Spring 2019 Lab 06 Lists

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

TUPLES AND RECURSIVE LISTS 5

CMSC201 Computer Science I for Majors

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CMSC 201 Spring 2017 Lab 05 Lists

XNA Tutorials Utah State University Association for Computing Machinery XNA Special Interest Group RB Whitaker 21 December 2007

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

First cut, scope. Defining scope. Find the namespace. Passing argument to parameter. A function s namespace 6/13/2017. chapter 8.

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

Python: common syntax

Lecture 10: Lists and Sequences

CSc 120 Introduction to Computer Programing II Adapted from slides by Dr. Saumya Debray

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

Swift. Introducing swift. Thomas Woodfin

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Functions that return lists

Python - Variable Types. John R. Woodward

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Episode 3 Lists and Strings

CS Summer 2013

Part III Appendices 165

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

Compound Data Types 2

The Dynamic Typing Interlude

CIS192 Python Programming

Python Evaluation Rules

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Overview of List Syntax

All programs can be represented in terms of sequence, selection and iteration.

Compound Data Types 1

Data Acquisition and Processing

CSCE 110 Programming I

More Data Structures. What is a Dictionary? Dictionaries. Python Dictionary. Key Value Pairs 10/21/2010

Sequence Types FEB

Part 1 - Your First algorithm

At full speed with Python

LECTURE 3 Python Basics Part 2

Implementing an Algorithm for Boomerang Fraction Sequences in Python

For this chapter, switch languages in DrRacket to Advanced Student Language.

Intro. Scheme Basics. scm> 5 5. scm>

Programming in Python

Transcription:

Notebook March 30, 2019 1 Complex Data Types Some kinds of data can store other kinds of data. 1.1 Lists We ve actually seen the most common complex data type a few times before, I just haven t pointed it out to you. We make a list by enclosing the elements of a list in square brackets. In [1]: list_of_numbers = [1, 2, 3] list_of_strings = ["cat", "dog", "bird"] we can have lists of lists. In [2]: for sublist in [list_of_numbers, list_of_strings]: for item in sublist: print(item) 1 2 3 cat dog bird As you ve seen, you can iterate over lists with a loop. Indeed, lists are the most common data structure in Python for such tasks. One extremely important fact about lists in Python is that they are mutable. By this, I mean that you can have a list, assigned to a name, and you can change its contents without changing its name or the relationship between that list and the variable. Indeed, lists have a bunch of methods that are intended to mutate them. Let s see some, eh? In [3]: example = list_of_strings.append("fish") counterexample = "cat".upper() You might expect the.append() method to work sort of like the.upper() method for strings, where it returns a new instance of the data type. But, as you ll see, it doesn t: 1

In [4]: print(example) None In [5]: print(counterexample) CAT The reason that the append() method doesn t return anything is because it mutates the original list: now the original list has a new word in it! In [6]: print(list_of_strings) ['cat', 'dog', 'bird', 'fish'] In [7]: list_of_strings.sort() print(list_of_strings) ['bird', 'cat', 'dog', 'fish'] Mutation is useful, but it s also perilous. In Jupyter Notebooks, in particular, it s easy to mess things up. For example, suppose I had realized I d made a mistake in some complex code cell where I d run a bunch of mutating functions, and so I tweaked something and ran it again. Then, way down the line, like 50 cells later, I might find that I had, say, the word fish twice in my list rather than just once! This can be a very difficult problem to debug. (The easiest solution is usually to go up to the top of the screen and click Kernel and then Restart & Run All Another trap with mutable lists is that you can actually change them in functions without using the global keyword. Why? Well, because scope is about controlling which variable name refers to what, not what s inside them! In [8]: def add_cow_to_list(animals): animals.append("cow") return animals is_this_a_second_list = add_cow_to_list(list_of_strings) In [9]: print(is_this_a_second_list) ['bird', 'cat', 'dog', 'fish', 'cow'] In [10]: print(list_of_strings) ['bird', 'cat', 'dog', 'fish', 'cow'] 2

The original list got changed inside the function! But it gets even crazier. In [11]: is_this_a_second_list.append("mouse") print(list_of_strings) ['bird', 'cat', 'dog', 'fish', 'cow', 'mouse'] That s right. We didn t actually make a second list when we returned the list out of the function. We actually just assigned the same list to a second variable! This is a general principle of Python lists: when you assign them to a new variable, it doesn t make a copy, it just adds another variable pointing to the same list; when you change one, you change the other. This can be really annoying behavior, it s actually one of my least favorite things about Python. It means it s easy to screw up your lists accidentally. And it s a big way that beginners get tripped up. A good rule of thumb for avoiding problems is to never mutate the original list in either a function or a loop. Instead, create a new blank list (which you can create with []) and then add things to that. For example: In [12]: def safe_add_horse(animals): newlist = [] for x in animals: newlist.append(x) newlist.append("horse") return newlist In [13]: neigh = safe_add_horse(list_of_strings) In [14]: print(list_of_strings) ['bird', 'cat', 'dog', 'fish', 'cow', 'mouse'] In [15]: print(neigh) ['bird', 'cat', 'dog', 'fish', 'cow', 'mouse', 'horse'] There are more sophisticated ways of dealing with these problems, which we can talk about down the road. Like strings, we can slice (subset) lists. In [16]: list_of_strings[1] Out[16]: 'cat' In [17]: list_of_strings[-1] Out[17]: 'mouse' 3

(Negative numbers index strings from the end, but start at 1.) In [18]: list_of_strings[-1:-5:-1] Out[18]: ['mouse', 'cow', 'fish', 'dog'] One nice trick about slicing lists is that slices return new lists, so they can help you get around mutability problems. A secret trick to copy a list is to use an empty slice with all the elements represented by blank spots between the colons. In [19]: newlist = list_of_strings[::] newlist.append("bear") print(newlist) print(list_of_strings) ['bird', 'cat', 'dog', 'fish', 'cow', 'mouse', 'bear'] ['bird', 'cat', 'dog', 'fish', 'cow', 'mouse'] But be careful. This just makes what s known as a shallow copy: it doesn t copy lists within lists, so you can still accidentally mutate those. In [20]: biglist = [list_of_numbers, list_of_strings] new_big_list = biglist[::] new_big_list[0].append(4) print(biglist) print(new_big_list) [[1, 2, 3, 4], ['bird', 'cat', 'dog', 'fish', 'cow', 'mouse']] [[1, 2, 3, 4], ['bird', 'cat', 'dog', 'fish', 'cow', 'mouse']] 1.2 Tuples Tuples are surrounded by parentheses, and are like lists, except they re immutable what that means is that you can t change them. There isn t an append or a sort or anything like that. However, that doesn t mean they re useless, because you can concatenate them with a plus sign (you can do that with lists too) and assign them to new variables. In [21]: mytupe = (1, 2) myothertupe = (3, 4) mytupe + myothertupe Out[21]: (1, 2, 3, 4) Also, even though something s immutable, it doesn t mean that you can t reassign the name, it just means you can t muck around with the contents. So this works: In [22]: mytupe = mytupe + myothertupe print(mytupe) 4

(1, 2, 3, 4) But you can t do things like assigning to slices you could do that with lists, but not with tuples. (You also can t do it with strings.) In [23]: list_of_numbers[0] = 99 print(list_of_numbers) [99, 2, 3, 4] In [24]: mytupe[0] = 99 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-24-7dc7a24e9236> in <module> ----> 1 mytupe[0] = 99 TypeError: 'tuple' object does not support item assignment 1.3 Dictionaries The other main complex data type is the dictionary, or dict. That s how Python stores associations between names and other data in a list-like form. For example, think of a telephone directory, that associates names with numbers. You could easily represent that with a Python dict. In [25]: phonebook = {"Joe Schmoe": 5551212, "Jill Schmill": 911, "Jack Schmack": 8675309} The syntax should be self-explanatory: curly brackets around the edges, then pairs of a name, or key, followed by a colon, followed by a value for that key, where all the pairs are separated by commas. Phonebooks, unlike lists and tuples, don t have an order to them. Instead, you access them by their keys. In [26]: print(phonebook["jack Schmack"]) 8675309 Dictionaries are also mutable, and you can add a new key or change the value attached to a key just by assigning to it. In [27]: phonebook["paul Gowder"] = 3843202 5

In [28]: print(phonebook) {'Joe Schmoe': 5551212, 'Jill Schmill': 911, 'Jack Schmack': 8675309, 'Paul Gowder': 3843202} You can iterate over a list s keys, values, or items (as key-value tuples) with, respectively, the keys(), values(), and items() methods. In [30]: for item in phonebook.items(): print(item) ('Joe Schmoe', 5551212) ('Jill Schmill', 911) ('Jack Schmack', 8675309) ('Paul Gowder', 3843202) The results of those methods act like lists, but they aren t really lists they have a special property called laziness which means that they don t actually exist in memory until you use them. In [31]: lazykeys = phonebook.keys() In [32]: print(lazykeys) dict_keys(['joe Schmoe', 'Jill Schmill', 'Jack Schmack', 'Paul Gowder']) In [33]: print(lazykeys[0]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-33-01ad6b36bc56> in <module> ----> 1 print(lazykeys[0]) TypeError: 'dict_keys' object does not support indexing It doesn t support indexing because it s not a real list. But you can make it one by slapping a call to a list() constructor function around it: In [34]: eagerkeys = list(lazykeys) In [35]: print(eagerkeys) ['Joe Schmoe', 'Jill Schmill', 'Jack Schmack', 'Paul Gowder'] In [36]: eagerkeys[0] Out[36]: 'Joe Schmoe' 6