Guido van Rossum 9th LASER summer school, Sept. 2012

Size: px
Start display at page:

Download "Guido van Rossum 9th LASER summer school, Sept. 2012"

Transcription

1 Guido van Rossum 9th LASER summer school, Sept. 2012

2 It took many steps to get to Py3k generators Interesting example of a "random walk" Origins go as far back as it gets

3 ABC has datatypes text, list, table You can iterate over each of these FOR ch IN 'abc':... 'a', 'b', 'c' (order as specified in the string) FOR elem IN {'one'; 'two'; 'three'}:... 'one', 'three', 'two' (sorted) FOR val IN {1: 'one'; 3: 'three'; 2: 'two'}:... 'one', 'two', 'three' (values in key order) Note that the FOR-loop is polymorphic nearly the only thing strings, lists have in common

4 The for-loop works with strings, lists, tuples but not with dicts! These are unified into "sequences" Sequences support (among others): length: len(xs) indexing, slicing: xs[i], xs[i:j] concatenation, repetition: xs + ys, xs * n optionally: assignment to item or slice Type object has slots for each of these The for-loop calls len(xs) and xs[i] each time

5 Use case: a class wrapping e.g. a file so you can write e.g. for line in wrapped_file: print line Steve Majewski invented a hack: implement getitem () to get the "next" item implement len () to lie until exhausted Python change: get xs[i] for increasing i until it raises IndexError avoids one (expensive) call per iteration implementing len () became optional

6 PEP 234 (Ka-Ping Yee, GvR, 2001) Separate "iterator" from "iterable" Downgrade "sequence" to backward compatibility All sequences become iterables Some new iterables added (e.g. dict) ironically, Python "for x in <dict>:" gives the keys The iterator object holds the iteration state E.g. list iteration: list object, index One iterable may have many iterators Iterator operation: it.next(), returns next value

7 How does an iterator indicate it is done? We had many long discussions about this Possibilities: separate has_next() or is_done() method return (value, done) tuple return special sentinel value when done raise exception when done In the end we chose the exception Plus a special case in the C code NULL without exception == exhausted

8 Now, "for x in xs: <body>" translates to: it = xs. iter () while True: try: x = it.next() except StopIteration: break <body> But we want to be able to pass an iterator! e.g. for x in xs. iter (): <body> Solution: iter(it) returns it Every iterator must support it.next(), iter(it)

9 PEP 255 (Neil Schemenauer, Tim Peters, M.L. Hetland, 2001) def foobar(n): for i in range(n): for j in range(i, n): yield j foobar(5) <generator object foobar at 0x104d93f00> list(foobar(5)) [0, 1, 2, 3, 4, 1, 2, 3, 4, 2, 3, 4, 3, 4, 4] A generator is an iterator; not every iterator is a generator!

10 Umm... Parser recognizes 'yield' keyword (then forbids "return <expr>" syntax) Function gets flagged "is generator" (actually, it's the code object that gets flagged) When called, constructs generator object next(genobj) runs bytecode until next YIELD

11 PEP 342 (GvR, Phillip Eby, 2005) Change yield from statement to expression def f(): a = yield 'a' print(a) yield 'b' g = f() print(next(g)) print(g.send(1)) 'a' 1 'b'

12 Other APIs added: g.throw(exc) # Causes yield to raise exc g.close() # Complex protocol, GeneratorExit Allow yield inside try/finally Use cases: Coroutines exchanging values Async I/O using "trampoline" or scheduler Still not quite Knuth-style coroutines Can suspend only one frame Still can't use "return <expr>"

13 Python 2 protocol is it.next(), it. iter () PEP 234 explains why (with regret!): explicit calls expected considered prev(), current(), reset() as extensions Python 3 changed to it. next () PEP 3114 (Ka-Ping Yee, 2007) Also adds next(it) (We had iter(it) from the start) next(it) added in Python 2.6 to help transition

14 (Greg Ewing, 2009) Approximate semantics: Make "yield from <expr>" Equivalent to "for v in <expr>: yield v" Also allow "return <expr>" equivalent to "raise StopIteration(<expr>)" True semantics are more complex send() and throw() pass values down Use case: refactoring generators/coroutines Sadly, we have no experience using this yet

15 The itertools module has many useful ops: E.g. itertools.chain(it1, it2), itertools.islice(it, i, j) Can't we write these as overloaded operators? E.g. it1+it2, it[i:j] No! Iterators are a (nice, small) protocol There is no common base class We wouldn't want to burden each iterator implementation with such extra methods We could offer an optional base class but that would make use of the overloading unreliable

Iterators & Generators

Iterators & Generators Iterators & Generators Sequences A sequence is something that you can: Index into Get the length of What are some examples of sequences? Sequences We ve been working with sequences all semester! Examples:

More information

Guido van Rossum 9th LASER summer school, Sept. 2012

Guido van Rossum 9th LASER summer school, Sept. 2012 Guido van Rossum guido@python.org 9th LASER summer school, Sept. 2012 Async I/O is as old as computers But programmers prefer synchronous I/O Easier to understand Easier to write code for Fewer chances

More information

Guido van Rossum 9th LASER summer school, Sept. 2012

Guido van Rossum 9th LASER summer school, Sept. 2012 Guido van Rossum guido@python.org 9th LASER summer school, Sept. 2012 Static analysis for Python is hard There are no good current tools There are a number of lint-like tools Some IDEs have some refactoring

More information

Python 3000 and You. Guido van Rossum PyCon March 14, 2008

Python 3000 and You. Guido van Rossum PyCon March 14, 2008 Python 3000 and You Guido van Rossum PyCon March 14, 2008 Why Py3k Open source needs to move or die Matz (creator of Ruby) To fix early, sticky design mistakes e.g. classic classes, int division, print

More information

CS 331 Midterm Exam 1

CS 331 Midterm Exam 1 CS 331 Midterm Exam 1 Friday, October 6 th, 2016 Please bubble your answers in on the provided answer sheet. Also be sure to write and bubble in your student ID number (without the leading A ). 1. What

More information

CS 331/401 Summer 2018 Midterm Exam

CS 331/401 Summer 2018 Midterm Exam CS 331/401 Summer 2018 Midterm Exam Instructions: This exam is closed-book, closed-notes. Computers of any kind are not permitted. For numbered, multiple-choice questions, fill your answer in the corresponding

More information

Python 3000 and You. Guido van Rossum EuroPython July 7, 2008

Python 3000 and You. Guido van Rossum EuroPython July 7, 2008 Python 3000 and You Guido van Rossum EuroPython July 7, 2008 Why Py3k Open source needs to move or die Matz (creator of Ruby) To fix early, sticky design mistakes e.g. classic classes, int division, print

More information

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators Chapter 4: Linked Structures and Outline 1 Chapter 4: Linked Structures and Chapter 4: Linked Structures and Using the ListNode Class Ideas about Linked List Implementation We have a pretty good feeling

More information

CS61A Lecture 36. Soumya Basu UC Berkeley April 15, 2013

CS61A Lecture 36. Soumya Basu UC Berkeley April 15, 2013 CS61A Lecture 36 Soumya Basu UC Berkeley April 15, 2013 Announcements HW11 due Wednesday Scheme project, contest out Our Sequence Abstraction Recall our previous sequence interface: A sequence has a finite,

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College September 25, 2017 Outline Outline 1 Chapter 4: Linked Structures and Chapter 4: Linked Structures and Outline 1 Chapter 4:

More information

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python Python Sequence Types Sequence types str and bytes are sequence types Sequence types have several operations defined for them Indexing Python Sequence Types Each element in a sequence can be extracted

More information

Python itertools. Stéphane Vialette. October 22, LIGM, Université Paris-Est Marne-la-Vallée

Python itertools. Stéphane Vialette. October 22, LIGM, Université Paris-Est Marne-la-Vallée Python itertools Stéphane Vialette LIGM, Université Paris-Est Marne-la-Vallée October 22, 2009 Stéphane Vialette (LIGM, Université Paris-Est Marne-la-Vallée) Python itertools October 22, 2009 1 / 31 Outline

More information

Iterators and Generators

Iterators and Generators Iterators and Generators it ain't your gramps' loop any more 2005 Alex Martelli aleaxit@gmail.com Python Iteration Protocol the old way: "for x in y:..." used to mean: at each leg of the loop: y got indexed

More information

Table of Contents EVALUATION COPY

Table of Contents EVALUATION COPY Table of Contents Introduction... 1-2 A Brief History of Python... 1-3 Python Versions... 1-4 Installing Python... 1-5 Environment Variables... 1-6 Executing Python from the Command Line... 1-7 IDLE...

More information

Iterators and Generators

Iterators and Generators Iterators and Generators Alex Martelli AB Strakt 1 This Tutorial s Audience You have a good base knowledge of Python 2.* (say, 2.0 or 2.1) You may have no knowledge of iterators, generators, other 2.2

More information

Python: Functions and Generators. Giuseppe Attardi

Python: Functions and Generators. Giuseppe Attardi Python: Functions and Generators Giuseppe Attardi Functional Programming Slides by Felix Hernandez-Campos List Comprehensions >>> freshfruit = [' banana', ' loganberry ', 'passion fruit '] >>> [x.strip()

More information

Guido van Rossum 9th LASER summer school, Sept. 2012

Guido van Rossum 9th LASER summer school, Sept. 2012 Guido van Rossum guido@python.org 9th LASER summer school, Sept. 2012 Why? Because I can :-) No language is designed "from scratch" "Those who don't know history are destined to repeat it" --Edmund Burke

More information

A Brief Introduction to Python for those who know Java. (Last extensive revision: Daniel Moroz, fall 2015)

A Brief Introduction to Python for those who know Java. (Last extensive revision: Daniel Moroz, fall 2015) A Brief Introduction to Python for those who know Java (Last extensive revision: Daniel Moroz, fall 2015) Plan Day 1 Baby steps History, Python environments, Docs Absolute Fundamentals Objects, Types Math

More information

What's New in Python 2.2

What's New in Python 2.2 What's New in Python 2.2 LinuxWorld - New York City - January 2002 Guido van Rossum Director of PythonLabs at Zope Corporation guido@python.org guido@zope.com Overview Appetizers Nested Scopes Int/Long

More information

Introduction to Problem Solving and Programming in Python.

Introduction to Problem Solving and Programming in Python. Introduction to Problem Solving and Programming in Python http://cis-linux1.temple.edu/~tuf80213/courses/temple/cis1051/ Overview Python sequences Lists, Tuples, and Ranges Built-in operations Slicing

More information

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1 A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Programming Paradigms

Programming Paradigms Programming Paradigms Procedural Sequence of instructions that inform the computer what to do with the program's input Declarative Specification describes the problem to be solved, and language implementation

More information

ITERATORS AND GENERATORS 10

ITERATORS AND GENERATORS 10 ITERATORS AND GENERATORS COMPUTER SCIENCE 6A July 23, 25 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only good for

More information

Lecture 7: Python s Built-in. in Types and Basic Statements

Lecture 7: Python s Built-in. in Types and Basic Statements The University of North Carolina at Chapel Hill Spring 2002 Lecture 7: Python s Built-in in Types and Basic Statements Jan 25 1 Built-in in Data Structures: Lists A list is an ordered collection of objects

More information

ITERATORS AND STREAMS 9

ITERATORS AND STREAMS 9 ITERATORS AND STREAMS 9 COMPUTER SCIENCE 61A November 12, 2015 1 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only

More information

Lecture 17A: Finite and Infinite Iterators

Lecture 17A: Finite and Infinite Iterators Extended Introduction to Computer Science CS1001.py Lecture 17A: Finite and Infinite Iterators Instructors: Benny Chor, Amir Rubinstein Teaching Assistants: Michal Kleinbort, Amir Gilad Founding TA and

More information

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

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords Worksheet 1: Introductory Exercises Turtle Programming Calculations The Print Function Comments Syntax Semantics Strings Concatenation Quotation Marks Types Variables Restrictions on Variable Names Long

More information

Babu Madhav Institute of Information Technology, UTU 2015

Babu Madhav Institute of Information Technology, UTU 2015 Five years Integrated M.Sc.(IT)(Semester 5) Question Bank 060010502:Programming in Python Unit-1:Introduction To Python Q-1 Answer the following Questions in short. 1. Which operator is used for slicing?

More information

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers Lecture 27 Lecture 27: Regular Expressions and Python Identifiers Python Syntax Python syntax makes very few restrictions on the ways that we can name our variables, functions, and classes. Variables names

More information

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009 Python A Technical Introduction James Heliotis Rochester Institute of Technology December, 2009 Background & Overview Beginnings Developed by Guido Van Rossum, BDFL, in 1990 (Guido is a Monty Python fan.)

More information

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline Python Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar December 28, 2011 1 Outline Introduction Installation and Use Distinct Features Python Basics Functional Example Comparisons with

More information

Asynchronous I/O: A Case Study in Python

Asynchronous I/O: A Case Study in Python Asynchronous I/O: A Case Study in Python SALEIL BHAT A library for performing await -style asynchronous socket I/O was written in Python. It provides an event loop, as well as a set of asynchronous functions

More information

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

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han Lesson 4: Type Conversion, Mutability, Sequence Indexing Fundamentals of Text Processing for Linguists Na-Rae Han Objectives Python data types Mutable vs. immutable object types How variable assignment

More information

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2011 Python Python was developed

More information

Data structure and algorithm in Python

Data structure and algorithm in Python Data structure and algorithm in Python Array-Based Sequences Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Python s Sequence Types 2. Low-Level s Arrays 3.

More information

A Brief Introduction to Python for those who know Java Last extensive revision: Jie Gao, Fall 2018 Previous revisions: Daniel Moroz, Fall 2015

A Brief Introduction to Python for those who know Java Last extensive revision: Jie Gao, Fall 2018 Previous revisions: Daniel Moroz, Fall 2015 A Brief Introduction to Python for those who know Java Last extensive revision: Jie Gao, Fall 2018 Previous revisions: Daniel Moroz, Fall 2015 Meet the Mighty Python Plan Day 1 Baby steps History, Python

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Iterators, Generators, Exceptions & IO Raymond Yin University of Pennsylvania September 28, 2016 Raymond Yin (University of Pennsylvania) CIS 192 September 28, 2016 1 / 26 Outline

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Object Oriented Programming Harry Smith University of Pennsylvania February 15, 2016 Harry Smith (University of Pennsylvania) CIS 192 Lecture 5 February 15, 2016 1 / 26 Outline

More information

Python-2. None. Special constant that is a null value

Python-2. None. Special constant that is a null value Python-2 None value in Python Iterators and generators Using them with file input Exceptions Modules (e.g., Python libraries) Using file I/O Walking a directory structure w. os module Using regular expressions

More information

21 Years of Python. From Pet Project to Programming Language of the Year. Guido van Rossum May 2011

21 Years of Python. From Pet Project to Programming Language of the Year. Guido van Rossum May 2011 21 Years of Python From Pet Project to Programming Language of the Year Guido van Rossum guido@python.org May 2011 Disclaimer I am speaking on my own behalf. My statements reflect my views only and do

More information

mith College Computer Science Week 12 CSC111 Spring 2018 Dominique Thiébaut

mith College Computer Science Week 12 CSC111 Spring 2018 Dominique Thiébaut mith College Computer Science Week 12 CSC111 Spring 2018 Dominique Thiébaut dthiebaut@smith.edu http://www.science.smith.edu/dftwiki/index.php/ Nice_Wrapping_Paper... Polymorphism Dictionaries & Recursion

More information

Computer Science 1001.py. Lecture 19, part B: Characters and Text Representation: Ascii and Unicode

Computer Science 1001.py. Lecture 19, part B: Characters and Text Representation: Ascii and Unicode Computer Science 1001.py Lecture 19, part B: Characters and Text Representation: Ascii and Unicode Instructors: Benny Chor, Amir Rubinstein Teaching Assistants: Amir Gilad, Michal Kleinbort Founding Teaching

More information

Lecture no

Lecture no Advanced Algorithms and Computational Models (module A) Lecture no. 3 29-09-2014 Giacomo Fiumara giacomo.fiumara@unime.it 2014-2015 1 / 28 Expressions, Operators and Precedence Sequence Operators The following

More information

Advanced Python. generators, decorators, context managers. Zbigniew Jędrzejewski-Szmek. George Mason University

Advanced Python. generators, decorators, context managers. Zbigniew Jędrzejewski-Szmek. George Mason University Advanced Python generators, decorators, context managers Zbigniew Jędrzejewski-Szmek George Mason University Python Summer School, Zürich, September 05, 2013 Version Zürich-98-ge13be00 This work is licensed

More information

Python - a Dynamic Programming Language. Guido van Rossum May 31, 2007

Python - a Dynamic Programming Language. Guido van Rossum May 31, 2007 Python - a Dynamic Programming Language Guido van Rossum May 31, 2007 Outline What is Python? Origins, history and design philosophy Python today The future: Python 3000 Links, Q&A 3 Copyright 2007 Google

More information

Python Evaluation Rules

Python Evaluation Rules Python Evaluation Rules UW CSE 160 https://courses.cs.washington.edu/courses/cse160/15sp/ Michael Ernst and Isaac Reynolds mernst@cs.washington.edu April 1, 2015 Contents 1 Introduction 2 1.1 The Structure

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Iterators, Generators, IO, and Exceptions Harry Smith University of Pennsylvania February 15, 2018 Harry Smith (University of Pennsylvania) CIS 192 Lecture 5 February 15, 2018

More information

Advanced Python. Executive Summary, Session 1

Advanced Python. Executive Summary, Session 1 Advanced Python Executive Summary, Session 1 OBJECT: a unit of data of a particular type with characteristic functionality (i.e., methods and/or use with operators). Everything in Python is an object.

More information

Data Science with Python Course Catalog

Data Science with Python Course Catalog Enhance Your Contribution to the Business, Earn Industry-recognized Accreditations, and Develop Skills that Help You Advance in Your Career March 2018 www.iotintercon.com Table of Contents Syllabus Overview

More information

Porting Python 2 Code to Python 3 Release 2.7.6

Porting Python 2 Code to Python 3 Release 2.7.6 Porting Python 2 Code to Python 3 Release 2.7.6 Guido van Rossum Fred L. Drake, Jr., editor Contents November 10, 2013 Python Software Foundation Email: docs@python.org 1 Choosing a Strategy ii 1.1 Universal

More information

References and Mutable Data Structures

References and Mutable Data Structures References and Mutable Data Structures Principles of Programming Languages CSE 307 1 Syntax 2 Semantics 3 Version: 1.4 16:44:20 2012/11/29 Compiled at 09:37 on 2018/11/13 Programming Languages References

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Classes Dr. David Koop Tuple, List, Dictionary, or Set? [1,2,"abc"] 2 Tuple, List, Dictionary, or Set? {"a", 1, 2} 3 Tuple, List, Dictionary, or Set? {} 4 Tuple,

More information

Data structure and algorithm in Python

Data structure and algorithm in Python Data structure and algorithm in Python Linked Lists Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Singly Linked Lists 2. Circularly Linked Lists 3. Doubly Linked

More information

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc. Python BASICS Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc. Identikit First appeared in 1991 Designed by Guido van Rossum General purpose High level

More information

Lists, loops and decisions

Lists, loops and decisions Caltech/LEAD Summer 2012 Computer Science Lecture 4: July 11, 2012 Lists, loops and decisions Lists Today Looping with the for statement Making decisions with the if statement Lists A list is a sequence

More information

MUTABLE LISTS AND DICTIONARIES 4

MUTABLE LISTS AND DICTIONARIES 4 MUTABLE LISTS AND DICTIONARIES 4 COMPUTER SCIENCE 61A Sept. 24, 2012 1 Lists Lists are similar to tuples: the order of the data matters, their indices start at 0. The big difference is that lists are mutable

More information

First Programming Language in CS Education The Arguments for Scala

First Programming Language in CS Education The Arguments for Scala First Programming Language in CS Education The Arguments for Scala WORLDCOMP 2011 By Dr. Mark C. Lewis Trinity University Disclaimer I am writing a Scala textbook that is under contract with CRC Press.

More information

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013 61A LECTURE 22 TAIL CALLS, ITERATORS Steven Tang and Eric Tzeng July 30, 2013 Announcements Homework 12 due Thursday, not Monday. Take the time to get started on the project instead! Almost done with Scheme

More information

CIS192: Python Programming

CIS192: Python Programming CIS192: Python Programming Introduction Harry Smith University of Pennsylvania January 18, 2017 Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 January 18, 2017 1 / 34 Outline 1 Logistics Rooms

More information

A Tutorial Introduction. CS 3270 Chapter 1

A Tutorial Introduction. CS 3270 Chapter 1 A Tutorial Introduction CS 3270 Chapter 1 A Micro-bit of Python History Evolved from a government-funded project to teach programming to children (ABC language) The project tanked, but Guido van Rossum

More information

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists Module 04: Lists Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10 1 Consider the string method split >>> name = "Harry James Potter" >>> name.split() ['Harry',

More information

Sequences and iteration in Python

Sequences and iteration in Python GC3: Grid Computing Competence Center Sequences and iteration in Python GC3: Grid Computing Competence Center, University of Zurich Sep. 11 12, 2013 Sequences Python provides a few built-in sequence classes:

More information

CSE 130, Fall 2005: Final Examination

CSE 130, Fall 2005: Final Examination CSE 130, Fall 2005: Final Examination Name: ID: Instructions, etc. 1. Write your answers in the space provided. 2. Wherever it says explain, write no more than three lines as explanation. The rest will

More information

PyTrie Documentation. Release 0.3. George Sakkis

PyTrie Documentation. Release 0.3. George Sakkis PyTrie Documentation Release 0.3 George Sakkis Jun 05, 2017 Contents 1 Usage 3 2 Reference documentation 5 2.1 Classes.................................................. 5 2.2 Trie methods...............................................

More information

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses CS 6A Delayed Expressions Fall 07 Discussion 9: November 8, 07 Iterables and Iterators An iterable is any container that can be processed sequentially. Examples include lists, tuples, strings, and dictionaries.

More information

Python Crash-Course. C. Basso. Dipartimento di Informatica e Scienze dell Informazione Università di Genova. December 11, 2007

Python Crash-Course. C. Basso. Dipartimento di Informatica e Scienze dell Informazione Università di Genova. December 11, 2007 Python Crash-Course C. Basso Dipartimento di Informatica e Scienze dell Informazione Università di Genova December 11, 2007 Basso (DISI) Python Crash-Course December 11, 2007 1 / 26 What is Python? Python

More information

TEXT MINING INTRO TO PYTHON

TEXT MINING INTRO TO PYTHON TEXT MINING INTRO TO PYTHON Johan Falkenjack (based on slides by Mattias Villani) NLPLAB Dept. of Computer and Information Science Linköping University JOHAN FALKENJACK (NLPLAB, LIU) TEXT MINING 1 / 23

More information

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

Python I. Some material adapted from Upenn cmpe391 slides and other sources Python I Some material adapted from Upenn cmpe391 slides and other sources Overview Names & Assignment Data types Sequences types: Lists, Tuples, and Strings Mutability Understanding Reference Semantics

More information

Modern Python Patterns and Idioms

Modern Python Patterns and Idioms Modern Python Patterns and Idioms http://www.aleax.it/pyconit15_mppi_en.pdf 2015 Google -- aleax@google.com Patterns vs Idioms (1) Patterns: a very general term Architecture Design Development Deployment...

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Generators Exceptions and IO Eric Kutschera University of Pennsylvania February 13, 2015 Eric Kutschera (University of Pennsylvania) CIS 192 February 13, 2015 1 / 24 Outline 1

More information

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python Outline 1 On Python language 2 3 4 Marcin Młotkowski Object oriented programming 1 / 52 On Python language The beginnings of Pythons 90 CWI Amsterdam, Guido van Rossum Marcin Młotkowski Object oriented

More information

CSC326 Python Sequences i. CSC326 Python Sequences

CSC326 Python Sequences i. CSC326 Python Sequences i CSC326 Python Sequences ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME 1.0 2011-09 JZ iii Contents 1 Agenda 1 2 while Statement 1 3 Sequence Overview 2 4 String 2 5 Lists 4 6 Dictionary 5 7 Tuples

More information

Introduction to Python programming, II

Introduction to Python programming, II GC3: Grid Computing Competence Center Introduction to Python programming, II (with a hint of MapReduce) Riccardo Murri Grid Computing Competence Center, University of Zurich Oct. 10, 2012 Today s class

More information

Patterns The Essence of Functional Programming

Patterns The Essence of Functional Programming Patterns The Essence of Functional Programming Up to now we have defined functions in a very traditional way: function name + variable name parameters Read Chap 7 In functional programming we can exploit

More information

Static Analysis of Dynamically Typed Languages made Easy

Static Analysis of Dynamically Typed Languages made Easy Static Analysis of Dynamically Typed Languages made Easy Yin Wang School of Informatics and Computing Indiana University Overview Work done as two internships at Google (2009 summer and 2010 summer) Motivation:

More information

Python Review IPRE

Python Review IPRE Python Review 2 Jay Summet 2005-12-31 IPRE Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing Elements Cloning Slices Mutable Types: Lists Dictionaries

More information

Python and Bioinformatics. Pierre Parutto

Python and Bioinformatics. Pierre Parutto Python and Bioinformatics Pierre Parutto October 9, 2016 Contents 1 Common Data Structures 2 1.1 Sequences............................... 2 1.1.1 Manipulating Sequences................... 2 1.1.2 String.............................

More information

Senthil Kumaran S

Senthil Kumaran S Senthil Kumaran S http://www.stylesen.org/ Agenda History Basics Control Flow Functions Modules History What is Python? Python is a general purpose, object-oriented, high level, interpreted language Created

More information

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp DM550/DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ TUPLES 2 Tuples as Immutable Sequences tuple =

More information

The Basil Project. (or What I took away from GRAD school ) Jonathan Riehl University of Chicago

The Basil Project. (or What I took away from GRAD school ) Jonathan Riehl University of Chicago The Basil Project (or What I took away from GRAD school ) Jonathan Riehl University of Chicago Outline Project History Project Goals Project Design Work, work, work... Project History GRAD - Grammar Based

More information

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions PYTHON DATA SCIENCE TOOLBOX II List comprehensions Populate a list with a for loop In [1]: nums = [12, 8, 21, 3, 16] In [2]: new_nums = [] In [3]: for num in nums:...: new_nums.append(num + 1) In [4]:

More information

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS 1439-1440 1 Outline 1. Introduction 2. Why Python? 3. Compiler and Interpreter 4. The first program 5. Comments and Docstrings 6. Python Indentations

More information

solrq Documentation Release Michał Jaworski

solrq Documentation Release Michał Jaworski solrq Documentation Release 1.1.1 Michał Jaworski Mar 27, 2017 Contents 1 solrq 1 2 usage 3 2.1 quick reference.............................................. 4 3 contributing 7 4 testing 9 5 Detailed

More information

Computer Science 1001.py. Lecture 19a: Generators continued; Characters and Text Representation: Ascii and Unicode

Computer Science 1001.py. Lecture 19a: Generators continued; Characters and Text Representation: Ascii and Unicode Computer Science 1001.py Lecture 19a: Generators continued; Characters and Text Representation: Ascii and Unicode Instructors: Daniel Deutch, Amir Rubinstein Teaching Assistants: Ben Bogin, Michal Kleinbort,

More information

Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction

Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Lecture 5: Bytecode Compiler Implementing Coroutines Compile AST to

More information

Intermediate Representation (IR)

Intermediate Representation (IR) Intermediate Representation (IR) l Our simple, syntax directed interpretation scheme that we worked out for the exp1 language, where we computed values for expressions as soon as we recognized them in

More information

Chapter 2 - Programming Language Syntax. September 20, 2017

Chapter 2 - Programming Language Syntax. September 20, 2017 Chapter 2 - Programming Language Syntax September 20, 2017 Specifying Syntax: Regular expressions and context-free grammars Regular expressions are formed by the use of three mechanisms Concatenation Alternation

More information

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

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and

More information

Python iterators and generators

Python iterators and generators Python iterators and generators Iterators and generators Python makes good use of iterators And has a special kind of generator function that is powerful and useful We ll look at what both are And why

More information

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging. COMP1730/COMP6730 Programming for Scientists Testing and Debugging. Overview * Testing * Debugging * Defensive Programming Overview of testing * There are many different types of testing - load testing,

More information

CS Programming Languages: Python

CS Programming Languages: Python CS 3101-1 - Programming Languages: Python Lecture 5: Exceptions / Daniel Bauer (bauer@cs.columbia.edu) October 08 2014 Daniel Bauer CS3101-1 Python - 05 - Exceptions / 1/35 Contents Exceptions Daniel Bauer

More information

Tree Oriented Programming. Jeroen Fokker

Tree Oriented Programming. Jeroen Fokker Tree Oriented Programming Jeroen Fokker Tree oriented programming Many problems are like: Input text transform process unparse Output text Tree oriented programming Many problems are like: Input text parse

More information

Sorting HOW TO Release 2.7.6

Sorting HOW TO Release 2.7.6 Sorting HOW TO Release 2.7.6 Guido van Rossum Fred L. Drake, Jr., editor November 10, 2013 Python Software Foundation Email: docs@python.org Contents 1 Sorting Basics i 2 Key Functions ii 3 Operator Module

More information

At full speed with Python

At full speed with Python At full speed with Python João Ventura v0.1 Contents 1 Introduction 2 2 Installation 3 2.1 Installing on Windows............................ 3 2.2 Installing on macos............................. 5 2.3

More information

Python 3 10 years later

Python 3 10 years later Python 3 10 years later FOSDEM 2018, Brussels Victor Stinner vstinner@redhat.com Victor Stinner CPython core developer since 2010 Work on CPython and OpenStack for Red Hat Very happy user of Fedora and

More information

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

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals: Numeric Types There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals: 1-123 +456 2. Long integers, of unlimited

More information

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

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming 2017 Table of contents 1 2 Integers and floats Integer int and float float are elementary numeric types in. integer >>> a=1 >>> a 1 >>> type (a) Integers and floats Integer int and float

More information

OOP and Scripting in Python Advanced Features

OOP and Scripting in Python Advanced Features OOP and Scripting in Python Advanced Features Giuliano Armano Emanuele Tamponi Advanced Features Structure of a Python Script More on Defining Functions Default Argument Values Keyword Arguments Arbitrary

More information

Extended Introduction to Computer Science CS1001.py Lecture 17: Cuckoo Hashing Finite and Infinite Iterators

Extended Introduction to Computer Science CS1001.py Lecture 17: Cuckoo Hashing Finite and Infinite Iterators Extended Introduction to Computer Science CS1001.py Lecture 17: Cuckoo Hashing Finite and Infinite Iterators Instructors: Benny Chor, Amir Rubinstein Teaching Assistants: Michal Kleinbort, Yael Baran School

More information