python. a presentation to the Rice University CS Club November 30, 2006 Daniel Sandler

Similar documents
Python review. 1 Python basics. References. CS 234 Naomi Nishimura

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

AI Programming CS S-02 Python

Python for Earth Scientists

The Pyth Language. Administrivia

Java Bytecode (binary file)

The current topic: Python. Announcements. Python. Python

Accelerating Information Technology Innovation

(CC)A-NC 2.5 by Randall Munroe Python

Welcome to Python! If you re the type of person who wants to know

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

LECTURE 1. Getting Started with Python

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

...something useful to do with the JVM.

Programming Language Basics

CS1 Lecture 3 Jan. 22, 2018

Symbolic Computation and Common Lisp

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

1007 Imperative Programming Part II

CIS192: Python Programming

COMP 215: INTRO TO PROGRAM DESIGN. Prof. Chris Jermaine Chris Prof. Chris Dr. Chris

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

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 231 Data Structures and Algorithms, Fall 2016

Programming for Engineers in Python

CS 349 / SE 382 Scripting. Professor Michael Terry March 18, 2009

Accelerating Information Technology Innovation

CS1 Lecture 3 Jan. 18, 2019

The Dynamic Typing Interlude

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

First Programming Language in CS Education The Arguments for Scala

Object Model Comparisons

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

COMP-202: Foundations of Programming. Lecture 26: Image Manipulation; Wrap-Up Jackie Cheung, Winter 2015

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

Introduction to Python. Didzis Gosko

CS 11 Haskell track: lecture 1

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Senthil Kumaran S

CS 11 java track: lecture 1

Table of Contents EVALUATION COPY

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Week - 01 Lecture - 04 Downloading and installing Python

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

C and C++ I. Spring 2014 Carola Wenk

CompSci 125 Lecture 02

Introduction to Python (All the Basic Stuff)

Programming Languages and Techniques (CIS120)

Python a modern scripting PL. Python

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

CS 251 Intermediate Programming Java Basics

Introduction to Python

Extending Jython. with SIM, SPARQL and SQL

Programming to Python

CS 1301 Exam 1 Answers Fall 2009

Python Tutorial. Day 2

CS Programming Languages: Python

Scala : an LLVM-targeted Scala compiler

Key Differences Between Python and Java

MITOCW watch?v=se4p7ivcune

Pace University. Fundamental Concepts of CS121 1

What Version Number to Install

CS 3813/718 Fall Python Programming. Professor Liang Huang.

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Getting started with Java

Python in 10 (50) minutes

EEE-425 Programming Languages (2013) 1

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

The Java Series. Java Essentials Advanced Language Constructs. Java Essentials II. Advanced Language Constructs Slide 1

Brief Summary of Java

And Parallelism. Parallelism in Prolog. OR Parallelism

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

Visualize ComplexCities

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Beyond Blocks: Python Session #1

Lesson 3: Accepting User Input and Using Different Methods for Output

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

1001ICT Introduction To Programming Lecture Notes

Java Input/Output. 11 April 2013 OSU CSE 1

notice the '' you must have those around character values, they are not needed for integers or decimals.

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Classes Classes 2 / 36

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Week 2: Quick and Dirty Jython Refresher

CS 240 Fall Mike Lam, Professor. Just-for-fun survey:

T H E I N T E R A C T I V E S H E L L

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

Transcription:

python. a presentation to the Rice University CS Club November 30, 2006 Daniel Sandler http://www.cs.rice.edu/~dsandler/python/

1. 2. 3. 4.

1. A young mind is corrupted.

Me, circa 2000 (very busy)

Many tasks, many languages C++ Scheme perl (work) (fun) (utility belt) OOP, popular pretty, powerful useful

Many tasks, many languages C++ Scheme perl (work) (fun) (utility belt) OOP, popular pretty, powerful useful painful development cycle unreadable by others unreadable by anyone

then, I discovered a language called python!

I am lazy.

Me, circa 2006 (lazy)

I am lazy. I am too lazy to wait for the compiler I am too lazy to switch between programming languages all the time I am too lazy to look up documentation I am too lazy to try to decipher line noise

Stuff I need to do: Sketch new ideas quickly Hack together analysis tools in the field Turn prototypes into polished research code Build web applications for course & departmental use

2. Seriously, tell us about python already.

python is an interpreted programming language object-oriented dynamically typed blah blah blah

handy smart fun helpful pretty python is

handy smart fun helpful pretty python is

handy built-in libraries, everything I might need data structures, concurrency object serialization cross-platform gui sockets/smtp/mime/http/imap/xml/rpc/etc. posix stuff, compression regexps, unicode cgi/httpd other modules: databases, scientific computing, image processing, crypto,

smart python is not just a scripting language pick your favorite programming paradigm very fancy OOP functional programming: not just supported, but natural

smart python is not just a scripting language pick your favorite programming paradigm very fancy OOP functional programming: not just supported, but natural lambda

fun every Python is a DrPython the read-eval-print loop (REPL) the only calculator you ll ever need experiment with live objects (no waiting for the compiler)

helpful Every object, class, function, module is self-documenting Carry your documentation with you (It s actually a property on the object) Essential, don t-leave-home-without-them functions to use in the REPL: help(foo) dir(foo)

pretty public class Hello { public static void main(string[] args) { System.out.println("Hello, world");

pretty public class Hello { public static void main(string[] args) { System.out.println("Hello, world");

pretty public class Hello { public static void main(string[] args) { System.out.println("Hello, world");

pretty (2) Ever seen this? It should be illegal. if (x ) { x = 2*x ; else{ while(x>0) x -= 2; x = Math.sqrt(x);

pretty (2) Ever seen this? It should be illegal. if (x)){ { x x = = 2*x; ; else else{ { while(x>0) { x -= x -= 2; 2; x = Math.sqrt(x); x = Math.sqrt(4*x);

pretty (2) In Python, it is illegal. if (x) x: ){ { x x = = 2*x; ; else: else{ { while(x>0) x>0: { x -= x -= 2; 2; x = Math.sqrt(x); x = Math.sqrt(4*x); math.sqrt(x) Indentation defines code blocks. Why? Code should be obvious and easy to read.

The perfect date, indeed handy: built-in libraries ftw smart: a real programming language fun: play around, develop quickly helpful: documentation is always there pretty: ugly code is hard to write

3. The (clean) Python phrasebook.

Simple types 1, -2.5, 0xDA51D, 3+5j, 9999999999L 'spam', "King Arthur's spam", '''multiline spam''' True, False None

Compound types List (mutable sequence) [1, [2, 3], None, "eggs"] Dictionary (mutable hash table) {"eggs": "The finest eggs in all Lilliput" "spam": "Spiced ham from Camelot" Tuple (immutable sequence) ("eggs", 100, True)

Naming, accessing x = 1 # creates the name x and sets its value # to 1. Oh, hey, this is a comment. y = ["eggs", 2, 3] z = {"a": 1, "b": 2 x + 1 # result is 2 y[1] # result is also 2 z["b"] #...still 2

String formatting "Hello %s (%d years old)." % (whom, age) "Hello %(name)s (%(age)d years old)." % adict

Functions, conditionals def fib(n): """Recursively computes the nth Fibonacci number.""" if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2)

A simple program: wc import java.lang.*; import java.io.*; public class wc { public static void main(string[] argv) throws IOException { open file initialize read bail each char new word, increment counter end word print BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); boolean done = false; long count = 0; while (!done) { String line = input.readline(); if (line == null) { done = true; else { boolean inword = false; for (int i = 0; i < line.length(); i++) { char ch = line.charat(i); if (!inword) { if (ch!= ' ') { inword = true; count += 1; else { if (ch == ' ') { inword = false; System.out.println(count);

wc in python (1) import sys read bail every char new word end word print done = False count = 0 while not done: line = sys.stdin.readline() if line == '': # EOF done = True else: inword = False for char in line: if not inword: if not char.isspace(): inword = True count += 1 else: if char.isspace(): inword = False print count

wc in python (2) import sys done = False count = 0 while not done: line = sys.stdin.readline() if line == '': # EOF done = True else: length of any count += len(line.split()) print count sequence split string (default: into words)

wc in python (3) import sys count = 0 for line in sys.stdin: count += len(line.split()) print count iterate lines in a file

wc in python (4) import sys print len(sys.stdin.read().split()) read whole file into a string

the Java version import java.lang.*; import java.io.*; public class wc { public static void main(string[] argv) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); boolean done = false; long count = 0; while (!done) { String line = input.readline(); if (line == null) { done = true; else { boolean inword = false; for (int i = 0; i < line.length(); i++) { char ch = line.charat(i); if (!inword) { if (ch!= ' ') { inword = true; count += 1; else { if (ch == ' ') { inword = false; System.out.println(count);

4. It s not just me.

Beyond the interpreter Need speed? Implement performance-critical code in C, wrap it in a Python interface for reuse Missing call/cc? Stackless Python Stuck in another runtime? Jython: Python syntax + JVM IronPython: Python syntax +.NET CLR

Scientific computing Numeric python - http://numpy.scipy.org/ pylab/matplotlib - http://matplotlib.sf.net/ gnuplot.py, pydot Next time you reach for matlab or gnuplot, try python instead

End-user applications GUI toolkits: Tk wxwidgets MFC (Windows Cocoa (OS X) BitTorrent

Industry Indexing the known universe (Google) Exploring the known universe (NASA) Recreating the known universe (ILM) Destroying the known universe (Eve Online)

Education Good for teaching All the simplicity of Scheme Familiar infix math Widely used outside the academy A trend in CS education MIT 6.001 headed this way (Rice COMP 210 as well?)

Further reading Dive Into Python http://diveintopython.org/ Programming Python (O Reilly) (updated 2006) More links, plus these slides: http://www.cs.rice.edu/~dsandler/python/

1. (my python story) 2. (about the language) 3. (syntax) 4. (resources)

16 tonnes Brought to you by the Ministry of Silly Talks