[1] Learned how to set up our computer for scripting with python et al. [3] Solved a simple data logistics problem using natural language/pseudocode.

Similar documents
Exercise 1: Python Language Basics

Exercise 4 Learning Python language fundamentals

Introduction to Python for Text Analysis

Introduction to Python

Getting Started with the Internet Communications Engine

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

Crash Dive into Python

Outline Basic concepts of Python language

ESPResSo Summer School 2012

CS 1133, LAB 2: FUNCTIONS AND TESTING

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Introduction to Python

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

Beyond the Mouse A Short Course on Programming

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

Sources: On the Web: Slides will be available on:

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

Informatica e Sistemi in Tempo Reale

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Programming Languages CIS 443

Big Data Analytics. Lucas Rego Drumond

Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD. Introduction to Pig

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Python Loops and String Manipulation

CS 106 Introduction to Computer Science I

Chapter 15 Functional Programming Languages

2! Multimedia Programming with! Python and SDL

Chapter 5 Functions. Introducing Functions

dictionary find definition word definition book index find relevant pages term list of page numbers

dedupe Documentation Release Forest Gregg, Derek Eder, and contributors

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Python Evaluation Rules

Crash Dive into Python

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Ruby - A Brief History

CAPIX Job Scheduler User Guide

C Programming Language

Introduction to Python

Welcome to Introduction to programming in Python

The C Programming Language course syllabus associate level

Introduction to: Computers & Programming: Input and Output (IO)

PROGRAMMING FOR BIOLOGISTS. BIOL 6297 Monday, Wednesday 10 am -12 pm

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

Lecture 5: Java Fundamentals III

The Smalltalk Programming Language. Beatrice Åkerblom

Java Crash Course Part I

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

PYTHON Basics

6.170 Tutorial 3 - Ruby Basics

Computer Science 217

Introduction to CloudScript

Software Tool Seminar WS Taming the Snake

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

PostgreSQL Functions By Example

cs2010: algorithms and data structures

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

LABORATORY 117. Intorduction to VoiceXML (2)

13 File Output and Input

Final Exam Review: VBA

The programming language C. sws1 1

MapReduce Détails Optimisation de la phase Reduce avec le Combiner

Computation and Economics - Spring 2012 Assignment #3: File Sharing

Introduction to Clustering

Introduction to Java

The OptQuest Engine Java and.net Developer's Guilde

Introduction to Object-Oriented Programming

Getting off the ground when creating an RVM test-bench

CSC230 Getting Starting in C. Tyler Bletsch

Java CPD (I) Frans Coenen Department of Computer Science

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Search and Replace in SAS Data Sets thru GUI

Programming Using Python

The design recipe. Programs as communication. Some goals for software design. Readings: HtDP, sections 1-5

7 Why Use Perl for CGI?

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Developer Experience: Because Coders are People Too

Introduction to Server-Side Programming. Charles Liu

Installation Document for HTML Calculators

ML for the Working Programmer

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Load balancing and failover For kbmmw v ProPlus and Enterprise Editions

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Introduction. Why (GIS) Programming? Streamline routine/repetitive procedures Implement new algorithms Customize user applications

CRASH COURSE PYTHON. Het begint met een idee

NLP Programming Tutorial 0 - Programming Basics

Key-Value Coding Programming Guide

Programming Languages & Tools

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

AllegroGraph. a graph database. Gary King gwking@franz.com

Data Intensive Computing Handout 5 Hadoop

Java Types and Enums. Nathaniel Osgood MIT April 25, 2012

Computer Programming I & II*

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

COMS Programming Languages Python: Lecture 1. Kangkook Jee

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

Object-Oriented Query Languages: Object Query Language (OQL)

Transcription:

Last time we... [1] Learned how to set up our computer for scripting with python et al. [2] Thought about breaking down a scripting problem into its constituent steps. [3] Solved a simple data logistics problem using natural language/pseudocode. course website: skynet.ohsu.edu/bmi507 notepad++

2 Python: introduction & data types Lecture

Why Python? [1] Why not? [2] Object-orientation [3] Just write your mind: intuitive syntax, no variable declaration [4] What about perl, ruby, and lua? [5] Massive user base. Massive. www.xkcd.com

Why Python? Some extra modules of note wxpython xlrd rpy2 nltk biopython libsvm, svmlight re glob random Graphical user interface Read/write to MS Excel Python/R middle man Natural Language Toolkit Bioinformatics miscellany Support Vector Machine Classification Regular expressions Pathname pattern matching Random number-related functionality www.xkcd.com

Python: basic conventions Write what you re thinking: assigning variables >>> var1 = 35 >>> var2 = 'This is a string.' >>> var3 = [var1, var2, ('apple', 'hippocampus')] printing information >>> print var1 ** 2 #1225 >>> var2.upper() #'THIS IS A STRING.' >>> var3.reverse(); print var3 #[('apple', 'hippocampus'), 'This is a string.', 35] importing & using modules >>> import os >>> os.getcwd() #'/Users/ambertk'

Python: basic conventions for script anatomy ***Note two things: [1] How I accessed what I needed from the sys module [2] The specific sys-module functions used.

Python: data types

Numbers. Integers Floating point number Long numbers Complex numbers int(3) = 3 float(3) = 3.0 long(3) = 3L complex(3) = 3 + 0j Caution: Type specification is more frequently done without coercion: >>> a = 2 >>> b = 5 So avoid errors by knowing which you re using, >>> a/b 0 >>> 2.0/5.0 0.40000000000000002 and their differences

Numbers. Shortcuts: x += 5 #Set x to the result of x + 5 x -= 5 #Set x to the result of x - 5 x *= 5 #Set x to the result of x * 5 x = 5 #Set x to the result of x/5

Numbers. From the math module math. copysign(x, y) sum(iterable) factorial(x) ceil(x) floor(x) isnan(x) isinf(x) exp(x) log(x, base) sqrt(x) cos(x) acos(x) hypt(x, y) radians(x) degress(x) pi e

Truth. Each of the following evaluate to false: None False 0, 0L, 0.0, 0j, (), [] {} Python is promiscuous with its beliefs. Everything else is True. Everything. >>> bool('') False >>> bool(' ') True >>> bool(['']) True

Truth. >>> var2 = True >>> var3 = False >>> var2 or var3 True >>> var2 and var3 False >>> var1 = 5 >>> 5 == var1 True >>> 5 >= 6 False

Truth. Fairly self-explanatory: objects of none-type are nothing. Well...they re something, but the thing they are is nothing, which is to say that nothing is something, and, sometimes, something is nothing, which is something. Let s move on. >>> var1 = None >>> print var1 None

Letters. Strings are one of a group of sequence-like objects in python, along with lists, tuples, and some others. Strings are also one of a few mutable objects, in python--that is to say, once they are created, they can t be changed in the same way you might change other types. You can obtain portions of your strings using the syntax depicted in this table. min and max aren t always meaningful for strings, but they ll still give you an answer. s[-1] last item in s Python has particularly well-developed string methods. Many of which we ll discuss at length later on in the course.

Letters. Strings are defined with single or double quotes, or using the str(x) function. If you need to include information in your string that you don t yet know (perhaps the result of a calculation), you can use %-substitution: >>> var1 = 54 >>> 'The result of our calculation is %s.'%(var1) 'The result of our calculation is 54.' where %s is a string.

Letters. This works for other types as well: >>> import math >>> 'The result of our calculation is %0.4f.'%(math.pi) 'The result of our calculation is 3.1416.' >>> 'The result of our calculation is %i.'%(math.pi) 'The result of our calculation is 3.'

Letters. Basic string methods Read more about strings at http://docs.python.org/library/stdtypes.html

Letters. --- raw strings and windows file paths --- formatting: \n\t\b Other things you should know about strings Read more about strings at http://docs.python.org/library/stdtypes.html

Letters. Advanced string methods & more translate encode/decode Regular expression matching Read more about strings at http://docs.python.org/library/stdtypes.html

Lists. >>> My_Opinion = [ Lists, are, fantastic ] Unlike other languages you might have learned, a list can contain objects of different types (but see array module): >>> [ Lists + are + fantastic, 5, None, 3.0, 3.0/5.0] ['Listsarefantastic', 5, None, 3.0, 0.59999999999999998] Lists are an ordered container for your information. They are heterogeneous sequences, and, like strings, can be accessed using the previously described slice operations: >>> My_Opinion[0] #'Lists' >>> My_Opinion[-1] #'fantastic'

Lists. List Methods

Lists. >>> range(0, 5) [0, 1, 2, 3, 4] >>> hello.split( e ) ['h', 'llo'] Important List-generating Functions >>> {'a':1, 'b':2, 'c':3}.keys() ['a', 'c', 'b'] >>> random.sample(range(0, 5000), 5) [3169, 3702, 2917, 695, 4213]

Lists. List Comprehensions (where lists get fun) >>> [i ** 2 for i in range(0, 5)] [0, 1, 4, 9, 16] >>> [(i, j) for i in range(0, 2) for j in ['a', 'b', 'c']] [ (0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c'), ]

Dictionaries. >>>{1: Dictionaries, 2: contain, 3: unordered,4: information. } Unlike lists, which are ordered, dictionaries are like a paper bag full of your data. Entries are accessible by a key, which needs to be something immutable (string, number, tuple). The syntax is >>> dx = { key1 : entry1, key2 : entry2 } >>> dx[ key1 ] entry1 One can extract the keys or values in a dictionary (in no particular order) by >>> dx.keys() [ key1, key2 ] >>> dx.values() [ entry2, entry1 ] The zip function provides an alternative way for creating dictionaries: >>> zip(dict(( key1, key2 ), ( entry1, entry2 )))

Dictionaries. Other Dictionary Methods & Functions Using Dictionaries len(dx) key (not) in dx copy() dx.clear() dx.items() dx.get(key, default) dx.pop(key, default) dx.setdefault(key, default) number of keys in dx evaluates to (False) True if key exists in dx returns a copy of dx clears dx; same as dx = {} returns key/value pairs in dx returns the value associated with key, otherwise returns default returns value associated with key, and removes it from dx if key is in dx, returns associated value, else sets key:default in dx, and returns default

Tuples. Tuples are immutable containers of information. They re like lists, but they can t be altered (in the same way as strings). Since they re immutable, they can be used as dictinoary keys. >>> var1 = ('a', 1) >>> type(var1) <type 'tuple'>

Combining things. Evaluation proceeds from left to right, and inside to outside. What would the following produce? >>> var1 = 'hello.' >>>'o'.join((var1.title().swapcase().split('e')))[0:-1] + 'w' 4 1 2 3 5 6

Combining things. Evaluation proceeds from left to right, and inside to outside. What would the following produce? >>> var1 = 'hello.' >>>'o'.join((var1.title().swapcase().split('e')))[0:-1] + 'w' 4 1 2 3 5 6 Although this is possible, blind use of this type of code writing is somewhat unpython. Make sure to write code that you and others are able to quickly understand, or you will be driven from town.