Computational Integer Programming. Lecture 4: Python. Dr. Ted Ralphs

Similar documents
Download Python from Any version will do for this class

Adapted from a Tuturial by Guido van Rossum Director of PythonLabs at Zope Corporation. Presented at LinuxWorld - New York City - January 2002

2. A Python Tutorial

Introduction to Python

The COIN-OR Optimization Suite: Open Source Tools for Optimization Part 3: Python Tools

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

Walk through previous lectures

CS Advanced Unix Tools & Scripting

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

CSCE 110 Programming I

! UNIX shell scripting. ! Tck/Tk. ! Perl. ! Python ! PHP. ! Ruby. ! Portable. ! Faster than C, C++, Java in productivity. ! Python block indenting

[Software Development] Python (Part A) Davide Balzarotti. Eurecom Sophia Antipolis, France

! UNIX shell scripting. ! Tck/Tk. ! Perl. ! Python ! PHP. ! Ruby. Maria Hybinette, UGA 2. ! Emphasizes Readability.

Short Introduction to Python Machine Learning Course Laboratory

Getting started with programming For geospatial data analysis. Robert Hijmans UC Davis

Senthil Kumaran S

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

Some material adapted from Upenn cmpe391 slides and other sources

The current topic: Python. Announcements. Python. Python

Introduction to Python. Didzis Gosko

Table of Contents EVALUATION COPY

ENGR 102 Engineering Lab I - Computation

Python. Department of Computer Science And Engineering. European University Cyprus

Introduction to programming with Python

Course Structure of Python Training: UNIT - 1: COMPUTER FUNDAMENTALS. Computer Fundamentals. Installation of Development Tools:

(CC)A-NC 2.5 by Randall Munroe Python

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

Python in 10 (50) minutes

Contents. Lezione 3. Reference sources. Contents

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Python Programming: Lecture 2 Data Types

Introduction to Python, Cplex and Gurobi

Advanced Algorithms and Computational Models (module A)

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

Python for Earth Scientists

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

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

DSC 201: Data Analysis & Visualization

This is an introductory tutorial, which covers the basics of Jython and explains how to handle its various modules and sub-modules.

The Pyth Language. Administrivia

Lecture 12 ADTs and Stacks

LABORATORY OF DATA SCIENCE. Python & Spyder- recap. Data Science & Business Informatics Degree

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

Introduction to Python for Scientific Computing

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Python source materials

Get It Interpreter Scripts Arrays. Basic Python. K. Cooper 1. 1 Department of Mathematics. Washington State University. Basics

Python Input, output and variables

A Tutorial Introduction. CS 3270 Chapter 1

Introduction to Python

Course Introduction and Python Basics

LECTURE 3 Python Basics Part 2

Python - Variable Types. John R. Woodward

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

Python: Short Overview and Recap

Introduction to Python Part 2

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Programming with Python

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

Introduction to: Computers & Programming: Review prior to 1 st Midterm

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

Programming in Python

CS Summer 2013

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

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

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

Large-Scale Networks

And Parallelism. Parallelism in Prolog. OR Parallelism

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

CIS192: Python Programming

Programming to Python

Jython. secondary. memory

Python Programming, bridging course 2011

ENGR 101 Engineering Design Workshop

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

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

Introduction to Python

Short, Unique and Mysterious

Part III Appendices 165

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

Introduction to Machine Learning. Useful tools: Python, NumPy, scikit-learn

Rapid Application Development with

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Artificial Intelligence A Primer for the Labs

Argh! Why another language? Scripting Part 2 (part 1 was shell): Introduction Python. Pythonic style. Python

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Introduction to Python

PHY224 Practical Physics I Python Review Lecture 1 Sept , 2013

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

Elementary Programming

Collections. Lists, Tuples, Sets, Dictionaries

ActiveNET. #202, Manjeeraa Plaza, Ameerpet, HYD

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Introduction to Python (All the Basic Stuff)

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CIS192 Python Programming

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Transcription:

Computational Integer Programming Lecture 4: Python Dr. Ted Ralphs

Computational MILP Lecture 4 1 Why Python? Pros As with many high-level languages, development in Python is quick and painless (relative to C++!). Python is popular in many disciplines and there is a dizzying array of packages available. Python s syntax is very clean and naturally adaptable to expressing mathematical programming models. Python has the primary data structures necessary to build and manipulate models built in. There has been a strong movement toward the adoption of Python as the high-level language of choice for (discrete) optimizers. Sage is quickly emerging as a very capable open-source alternative to Matlab. Cons Python s one major downside is that it can be very slow. Solution is to use Python as a front-end to call lower-level tools.

Computational MILP Lecture 4 2 Drinking the Python Kool-Aid

Adapted from a Tuturial by Guido van Rossum Director of PythonLabs at Zope Corporation Presented at LinuxWorld - New York City - January 2002

Interpreted language Intuitive syntax Dynamic typing Loads of built-in libraries and available extensions Shallow learning curve Easy to call C/C++ for efficiency Object-oriented Simple, but extremely powerful

interactive "shell basic types: numbers, strings container types: lists, dictionaries, tuples variables control structures functions & procedures classes & instances modules exceptions files & standard library

Great for learning the language Great for experimenting with the library Great for testing your own modules Two variations: IDLE (GUI), python (command line) Type statements or expressions at prompt: >>> print "Hello, world" Hello, world >>> x = 12**2 >>> x/2 72 >>> # this is a comment

To write a program, put commands in a file #hello.py print "Hello, world" x = 12**2 x/2 print x Execute on the command line ~> python hello.py Hello, world 72

No need to declare Need to assign (initialize) use of uninitialized variable raises exception Not typed if friendly: greeting = "hello world" else: greeting = 12**2 print greeting Everything is an object": Even functions, classes, modules

if condition: statements [elif condition: statements]... else: statements while condition: statements for var in sequence: statements break continue

In Python: for i in range(20): if i%3 == 0: print i if i%5 == 0: print "Bingo!" print "" In C: for (i = 0; i < 20; i++) { if (i%3 == 0) { printf("%d\n", i); if (i%5 == 0) { } printf("bingo!\n"); } } printf("\n"); 0 Bingo! 3 6 9 12 15 Bingo! 18

The usual suspects 12, 3.14, 0xFF, 0377, (-1+2)*3/4**5, abs(x), 0<x<=5 C-style shifting & masking 1<<16, x&0xff, x 1, ~x, x^y Integer division truncates :-( 1/2 -> 0 # 1./2. -> 0.5, float(1)/2 -> 0.5 Will be fixed in the future Long (arbitrary precision), complex 2L**100 -> 1267650600228229401496703205376L In Python 2.2 and beyond, 2**100 does the same thing 1j**2 -> (-1+0j)

"hello"+"world" "helloworld" # concatenation "hello"*3 "hellohellohello" # repetition "hello"[0] "h" # indexing "hello"[-1] "o" # (from end) "hello"[1:4] "ell" # slicing len("hello") 5 # size "hello" < "jello" 1 # comparison "e" in "hello" 1 # search "escapes: \n etc, \033 etc, \if etc" 'single quotes' """triple quotes""" r"raw strings"

Flexible arrays, not Lisp-like linked lists a = [99, "bottles of beer", ["on", "the", "wall"]] Same operators as for strings a+b, a*3, a[0], a[-1], a[1:], len(a) Item and slice assignment a[0] = 98 a[1:2] = ["bottles", "of", "beer"] -> [98, "bottles", "of", "beer", ["on", "the", "wall"]] del a[-1] # -> [98, "bottles", "of", "beer"]

>>> a = range(5) # [0,1,2,3,4] >>> a.append(5) # [0,1,2,3,4,5] >>> a.pop() # [0,1,2,3,4] 5 >>> a.insert(0, 42) # [42,0,1,2,3,4] >>> a.pop(0) # [0,1,2,3,4] 5.5 >>> a.reverse() # [4,3,2,1,0] >>> a.sort() # [0,1,2,3,4]

Hash tables, "associative arrays" d = {"duck": "eend", "water": "water"} Lookup: d["duck"] -> "eend" d["back"] # raises KeyError exception Delete, insert, overwrite: del d["water"] # {"duck": "eend", "back": "rug"} d["back"] = "rug" # {"duck": "eend", "back": "rug"} d["duck"] = "duik" # {"duck": "duik", "back": "rug"}

Keys, values, items: d.keys() -> ["duck", "back"] d.values() -> ["duik", "rug"] d.items() -> [("duck","duik"), ("back","rug")] Presence check: d.has_key("duck") -> 1; d.has_key("spam") -> 0 Values of any type; keys almost any {"name":"guido", "age":43, ("hello","world"):1, 42:"yes", "flag": ["red","white","blue"]}

Keys must be immutable: numbers, strings, tuples of immutables these cannot be changed after creation reason is hashing (fast lookup technique) not lists or other dictionaries these types of objects can be changed "in place" no restrictions on values Keys will be listed in arbitrary order again, because of hashing

key = (lastname, firstname) point = x, y, z # parentheses optional x, y, z = point # unpack lastname = key[0] singleton = (1,) # trailing comma!!! empty = () # parentheses! tuples vs. lists; tuples immutable

Assignment manipulates references x = y does not make a copy of y x = y makes x reference the object y references Very useful; but beware! Example: >>> a = [1, 2, 3] >>> b = a >>> a.append(4) >>> print b [1, 2, 3, 4]

a = [1, 2, 3] a 1 2 3 b = a a b 1 2 3 a.append(4) a b 1 2 3 4

a = 1 a 1 a b = a 1 b new int object created by add operator (1+1) a = a+1 a b 2 1 old reference deleted by assignment (a=...)

def name(arg1, arg2,...): """documentation""" # optional doc string statements return return expression # from procedure # from function

def gcd(a, b): "greatest common divisor" while a!= 0: a, b = b%a, a # parallel assignment return b >>> gcd. doc 'greatest common divisor' >>> gcd(12, 20) 4

class name: "documentation" statements -orclass name(base1, base2,...):... Most, statements are method definitions: def name(self, arg1, arg2,...):... May also be class variable assignments

class Stack: "A well-known data structure " def init (self): self.items = [] def push(self, x): self.items.append(x) def pop(self): x = self.items[-1] del self.items[-1] return x def empty(self): return len(self.items) == 0 # constructor # the sky is the limit # what happens if it s empty? # Boolean result

To create an instance, simply call the class object: x = Stack()# no 'new' operator! To use methods of the instance, call using dot notation: x.empty() # -> 1 x.push(1) # [1] x.empty() # -> 0 x.push("hello") # [1, "hello"] x.pop() # -> "hello" # [1] To inspect instance variables, use dot notation: x.items # -> [1]

Collection of stuff in foo.py file functions, classes, variables Importing modules: import re; print re.match("[a-z]+", s) from re import match; print match("[a-z]+", s) Import with rename: import re as regex from re import match as m

Computational MILP Lecture 4 28 Getting Python There are many different flavors of Python, all of which support the same basic API, but have different backends and performance. The original flavor is CPython, but there is also Jython, Iron Python, Pyjs, PyPy, RubyPython, and others. If you are going to use a package with a C extensions, you probably need to get CPython. For numerical computational, some additional packages are almost certainly required, NumPy and SciPy being the most obvious. On Linux, Python and the most important packages will be preinstalled, with additional ones installed easily via a package manager. On OS X, Python comes pre-installed, but it is easier to install Python and any additional packages via Homebrew. On Windows, it s easiest to install a distribution that includes the scientific software, such as anaconda or winpython. Another option is to use Sage, a Matlab-like collection of Python packages (including COIN). Make sure you install Python 2.7!!

Computational MILP Lecture 4 29 Getting an IDE An additional requirement for doing development is an IDE. My personal choice is Eclipse with the PyDev plug-in. This has the advantage of being portable and cross-platform, as well as supporting most major languages. There are many alternative IDEs, however.

Computational MILP Lecture 4 30 Exercise: Install Python and IDE Complete as many of the exercises here as you can: http://coral.ie.lehigh.edu/~ted/files/ie172/labs/lab0/lab0.pdf Make sure you install Python 2.7!!