Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.

Slides:



Advertisements
Similar presentations
Lab III – Linux at UMBC.
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
COMPUTER PROGRAMMING Task 1 LEVEL 6 PROGRAMMING: Be able to use a text based language like Python and JavaScript & correctly use procedures and functions.
Introduction to Python
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Python John Alexis Guerra Gómez CMSC433 Spring 2010 University of Maryland.
CS 110 Intro to Computer Science I Sami Rollins Fall 2006.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
CSC 9010: Natural Language Processing
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L.
Python.
Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
© Copyright 2000, Julia Hartman 1 Next An Interactive Tutorial for SPSS 10.0 for Windows © by Julia Hartman Using Command Syntax.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
CMPD 434 MULTIMEDIA AUTHORING Chapter 06 Multimedia Authoring Process IV.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Python From the book “Think Python”
Numbers, lists and tuples Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Outline  Why should we use Python?  How to start the interpreter on a Mac?  Working with Strings.  Receiving parameters from the command line.  Receiving.
9/2/ CS171 -Math & Computer Science Department at Emory University.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Sequence comparison: Dynamic programming Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Intro Python: Variables, Indexing, Numbers, Strings.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
Ganga 4 Basics - Tutorial Jakub T. Moscicki ARDA/LHCb Ganga Tutorial, November 2005.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Introducing Python 3 Introduction to Python. Introduction to Python L1 Introducing Python 3 Learning Objectives Know what Python is and some of the applications.
Python Fundamentals: Hello World! Eric Shook Department of Geography Kent State University.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
An Interactive Tutorial for SPSS 10.0 for Windows©
Programming in R Intro, data and programming structures
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Python
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
1-1 Logic and Syntax A computer program is a solution to a problem.
Introduction to Python
Topics Introduction to Repetition Structures
Lecture 1 Introduction to Python Programming
Linux.
CompSci 230 Software Construction
Introduction to Python
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Conditionals (if-then-else)
Imperative Programming
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Learning Outcomes –Lesson 4
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Chapter 1: Programming Basics, Python History and Program Components
Imperative Programming
Presentation transcript:

Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble

Why Python? Python is –easy to learn, –relatively fast, –object-oriented, –strongly typed, –widely used, and –portable. C is much faster but much harder to use. Java is about as fast and slightly harder to use. Perl is slower, is as easy to use, but is not strongly typed.

Getting started on the Mac Start a terminal session. Type “python” This should start the Python interpreter > python Python (#2, Apr , 16:28:28) [GCC (Red Hat Linux )] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print “hello, world!” hello, world!

The interpreter Try printing various things –Leave off the quotation marks. –Print numbers, letters and combinations. –Print two things, with a comma between. –Enter a mathematical formula. –Leave off the word “print”. The interpreter allows you to try things out interactively and quickly. Use the interpreter to test syntax, or to try commands that you’re not sure will work when you run your program.

Your first program In your terminal, Ctrl-D out of python. Type “pwd” to find your current working directory. Open TextWrangler. Create a file containing one line: print “hello, world!” Be sure that you end the line with a carriage return. Save the file as “hello.py” in your current working directory. In your terminal, type “python hello.py” > python hello.py hello, world! Notice that, once you save the file with “.py” as the extension, WordWrangler automatically colors the text according to the syntax.

Objects and types We use the term object to refer to any entity in a python program. Every object has an associated type, which determines the properties of the object. Python defines six types of built-in objects: Number10 String“hello” List[1, 17, 44] Tuple(4, 5) Dictionary{‘food’ : ‘something you eat’, ‘lobster’ : ‘an edible, undersea arthropod’} Files Each type of object has its own properties, which we will learn about in the next several weeks. It is also possible to define your own types, comprised of combinations of the six base types.

Literals and variables A variable is simply another name for an object. For example, we can assign the name “pi” to the object , as follows: >>> pi = >>> print pi When we write out the object directly, it is a literal, as opposed to when we refer to it by its variable name.

The “import” command Many python functions are only available via “packages” that must be imported. >>> print log(10) Traceback (most recent call last): File " ", line 1, in ? NameError: name 'log' is not defined >>> import math >>> print math.log(10)

The command line To get information into a program, we will typically use the command line. The command line is the text you enter after the word “python” when you run a program. import sys print "hello, world!" print sys.argv[0] print sys.argv[1] The zeroth argument is the name of the program file. Arguments larger than zero are subsequent elements of the command line.

Sample problem #1 Write a program called “print-two-args.py” that reads the first two command line arguments after the program name, stores their values as variables, and then prints them on the same line with a colon between. > python print-two-args.py hello world hello : world

Solution #1 import sys arg1 = sys.argv[1] arg2 = sys.argv[2] print arg1, ":", arg2

Sample problem #2 Write a program called “add-two-args.py” that reads the first two command line arguments after the program name, stores their values as variables, and then prints their sum. Hint: To read an argument as a number, use the syntax “arg1 = float(sys.argv[1])” > python add-two-args.py

Solution #2 import sys arg1 = float(sys.argv[1]) arg2 = float(sys.argv[2]) print arg1 + arg2

Reading Chapter 1-3 of Learning Python (3 rd edition) by Lutz.