Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Similar documents
Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Python Programming Exercises 1

Exception Handling. Genome 559

Strings. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Table of Contents EVALUATION COPY

Physics 514 Basic Python Intro

Starting. Read: Chapter 1, Appendix B from textbook.

Strings. Genome 373 Genomic Informatics Elhanan Borenstein

Class extension and. Exception handling. Genome 559

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Algorithms and Programming I. Lecture#12 Spring 2015

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Lecture 3. Functions & Modules

Python Intro GIS Week 1. Jake K. Carr

Lecture 3. Functions & Modules

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

PYTHON. Values and Variables

CS1 Lecture 3 Jan. 18, 2019

Variable and Data Type 2

Scientific Computing: Lecture 1

Python for Non-programmers

Lesson 4: Who Goes There?

Lecture 3. Strings, Functions, & Modules

CS1 Lecture 3 Jan. 22, 2018

Simple Java Reference

last time in cs recitations. computer commands. today s topics.

Lab 9: Creating a Reusable Class

Types, lists & functions

Expressions and Variables

Python lab session 1

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

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

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

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CSEN 102 Introduction to Computer Science

Python 1: Intro! Max Dougherty Andrew Schmitt

Python for Astronomers. Week 1- Basic Python

CSCI 121: Anatomy of a Python Script

Python Unit

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

The current topic: Python. Announcements. Python. Python

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

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

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Variables and Constants

MEIN 50010: Python Introduction

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

Modules and Programs 1 / 14

Python for C programmers

CPSC 217 Assignment 4

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

Introduction to Python

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

CS Advanced Unix Tools & Scripting

Some material adapted from Upenn cmpe391 slides and other sources

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Class extension and. Exception handling. Genome 559

Variables, Types, Operations on Numbers

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

>>> * *(25**0.16) *10*(25**0.16)

Lecture 3: Functions & Modules

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Science One CS : Getting Started

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

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

CSE : Python Programming

Python - Variable Types. John R. Woodward

Introduction to Java Applications

Computer Programming in MATLAB

Downloaded from Chapter 2. Functions

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

Programming to Python

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

First Java Program - Output to the Screen

AP Computer Science Unit 1. Programs

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

Math Day 2 Programming: How to make computers do math for you

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

Jython. secondary. memory

Basic Syntax - First Program 1

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

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

Lab # 2. For today s lab:

Week - 01 Lecture - 04 Downloading and installing Python

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

ucalc Patterns Examine the following example which can be pasted into the interpreter (paste the entire block at the same time):

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Introduction to Bioinformatics

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Activity 1: Introduction

At full speed with Python

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Transcription:

Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

If you have your own PC, download and install a syntax-highlighting text editor and Python 2.6.8: http://www.flos-freeware.ch/notepad2.html http://www.python.org/download/releases/2.6.8/ If you have your own Mac, download Python (same site) and TextWrangler: http://www.barebones.com/products/textwrangler/download.html

Why Python? Python is easy to learn fast enough object-oriented widely used fairly portable C is much faster but much harder to learn and use. Java is somewhat faster but harder to learn and use. Perl is harder to learn.

Getting started on the Mac Start a terminal session Type python This should start the Python interpreter (often called IDLE ) Print "Hello, world!" as below if your line starts with '>>>' you are in the interpreter > python Python 2.6.4 (something something) details something something Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello, world!" Hello, world! to leave the interpreter, type Ctrl-D or exit()

The interpreter Try printing various things (in your spare time) Leave off the quotation marks. Print numbers, letters and combinations. Print two things, with a comma between them. Enter a mathematical formula. Use the interpreter to test syntax, to try new commands, etc. Don't write programs in the interpreter. the Python interpreter is a sandbox: you play in it, you don't work in it

Your first program In your terminal, Ctrl-D out of the python interpreter. Type pwd to find your present working directory. Open TextWrangler. Create a file containing one line: print Hello, world! Be sure that you end the line with enter. Save the file as hello.py in your present working directory. Back in your terminal, type python hello.py > python hello.py Hello, world! (This tells the computer "use python to run the program hello.py". Yes, the result is somewhat anticlimactic.) Notice that, once you save the file with.py as the extension, WordWrangler automatically colors the text according to the syntax.

Objects and types An object to refers to any entity in a python program. Every object has an associated type, which determines the properties of the object. Python defines six main types of built-in objects: notice the different symbols used to define types - quote, bracket, parenthesis, curly brace Number 10 or 2.71828 String Hello, world! List [1, 17, 44] or [ pickle, apple, scallop ] Tuple (4, 5) or ( homework, exam ) Dictionary { food : something you eat, lobster : an edible arthropod } File we'll talk about this one later Each type of object has its own properties, which we will learn about in the next few weeks. It is also possible to define your own type of object, comprised of combinations of the six base types. a list of numbers a list of strings

Literals and variables A variable is a name in your program for an object. For example, we can assign the name pi to the Number object 3.14159, as follows: >>> pi = 3.14159 >>> print pi 3.14159 notice I am back in the interpreter here, as you can tell by the prompt >>> When we write out the object directly, it is a literal, as opposed to when we refer to it by its variable name. Above, 3.14159 is a literal, pi is a variable.

Assignment operator >>> pi = 3.14159 The '=' means assign the value 3.14159 to the variable pi (it does NOT assert that pi equals 3.14159). >>> pi = 3.14159 >>> print pi 3.14159 >>> pi = -7.2 >>> print pi -7.2 you can see where "variable" comes from: pi can be changed

The import command Many python functions are available via packages that must be imported (other functions are always available - called built-in). For example, the log function is in the math package: >>> print log(10) Traceback (most recent call last): File foo, line 1, in bar NameError: name 'log' is not defined >>> import math >>> print math.log(10) 2.30258509299 >>> print log(10) Traceback (most recent call last): File foo, line 1, in bar print log(10) NameError: name 'log' is not defined foo and bar mean something-orother-goes-here import the math package use the log function in the math package for now don't worry about the details of the error message - just be aware that this means there is an error in your program.

The command line To get information into a program, we can use the command line. The command line is the text you enter after the word python when you run a program. python my-program.py 17 The zeroth argument is the name of the program file. Arguments larger than zero are subsequent elements of the command line, separated by spaces. zeroth argument first argument

Reading command line arguments Access in your program like this: import sys print sys.argv[0] print sys.argv[1] > python my-program.py 17 my-program.py 17 zeroth argument first argument There can be any number of arguments, accessed by sequential numbers (sys.argv[2] etc).

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 to screen on the same line with a colon between. Use the python interpreter for quick syntax tests if you want. > python print-two-args.py hello world hello : world Hint to print multiple things on one line, separate them by commas: >>> print 7, "pickles" 7 pickles

Solution #1 import sys vala = sys.argv[1] valb = sys.argv[2] print vala, ":", valb assign the first command line argument to the variable vala print the value of this variable print the literal string print the value of this variable

Alternative solution #1 import sys print sys.argv[1], ":", sys.argv[2] print the value of this variable print the literal string print the value of this variable This doesn't assign the variable names, as requested in the problem, but it is functionally equivalent.

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 number variables, and then prints their sum. > python add-two-args.py 1 2 3.0 Hint - to read an argument as a decimal number, use the syntax: foo = float(sys.argv[1]) or for an integer number: bar = int(sys.argv[1]) Command line arguments always start as string objects The technical name for this is "casting" - the value starts as a string object and is cast to a float or int object (two kinds of Number objects in Python).

Solution #2 import sys arg1 = float(sys.argv[1]) arg2 = float(sys.argv[2]) print arg1 + arg2 notice that this expression gets evaluated first, then printed

Alternative solutions #2 import sys arg1 = float(sys.argv[1]) arg2 = float(sys.argv[2]) argsum = arg1 + arg2 print argsum or import sys print float(sys.argv[1]) + float(sys.argv[2])

Challenge problems Write a program called circle-area.py that reads the first command line argument as the radius of a circle and prints the area of the circle. > python circle-area.py 15.7 774.371173183 Do the same thing but read a second argument as the unit type and include the units in your output. > python circle-area2.py 3.721 cm 43.4979923683 square cm

Challenge solutions import sys radius = float(sys.argv[1]) print 3.1415 * radius * radius (or slightly better) import sys import math radius = float(sys.argv[1]) print math.pi * radius * radius the math package contains most simple math constants and functions that are not built in the math constant pi import sys import math a literal string radius = float(sys.argv[1]) units = sys.argv[2] print math.pi * radius * radius, "square", units

Reading Chapter 1 of Think Python by Downey. Legal free PDF linked on web site.