Introduction to Python

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Lab III – Linux at UMBC.
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Python Basics: Statements Expressions Loops Strings Functions.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
CSC 9010: Natural Language Processing
Introduction to Python: Slides Referenced in Homework 0 CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
Recitation 1 Programming for Engineers in Python.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Python
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Computer Science 101 Introduction to Programming.
Outline  Why should we use Python?  How to start the interpreter on a Mac?  Working with Strings.  Receiving parameters from the command line.  Receiving.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Intro Python: Variables, Indexing, Numbers, Strings.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
PHP Form Processing * referenced from
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
Foundations of Programming: Java
Fundamentals of Programming I Overview of Programming
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Introduction to Programming
Python Let’s get started!.
Introduction to Python
Introduction to Python
Lecture 1 Introduction to Python Programming
Variables, Expressions, and IO
Week 1 Gates Introduction to Information Technology cosc 010 Week 1 Gates
Introduction to Python
Perl for Bioinformatics
Introduction to Python
Learning Outcomes –Lesson 4
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Variables, Data Types & Math
Variables, Data Types & Math
Beginning Python Programming
12th Computer Science – Unit 5
Chapter 1: Programming Basics, Python History and Program Components
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
Class code for pythonroom.com cchsp2cs
Introduction to Python
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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.6: http://www.flos-freeware.ch/notepad2.html http://www.python.org/download/releases/2.6.6/ 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 relatively fast object-oriented widely used fairly portable C is much faster but much harder to learn and use. Java is somewhat faster and harder to learn and use. Perl is a little slower and not as easy to learn.

Getting started on the Mac Start a terminal session Type “python” This should start the Python interpreter (often called “IDLE”) > python Python 2.6.4 (something something) details something something Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello, world!" Hello, world!

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. 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 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. 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. (This tells the computer "use python to run the program hello.py". Yes, the result is somewhat anticlimactic.)

notice the different symbols used to define each type 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 main types of built-in objects: Number 10 or 2.71828 String “hello” List [1, 17, 44] or [“pickle”, “apple”, “scallop”] Tuple (4, 5) or (“homework”, “exam”) Dictionary {“food” : “something you eat”, “lobster” : “an edible arthropod”} File more on 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 types, comprised of combinations of the six base types. notice the different symbols used to define each type

Literals and variables A variable is simply a name 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 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.

you can see where "variable" comes from - pi can be changed 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 >>> pi = -7.2 >>> print pi -7.2 you can see where "variable" comes from - pi can be changed

foo and bar mean something-or-other-goes-here The import command Many python functions are available only via “packages” that must be imported (other functions are always available - called "built-in"). >>> 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) foo and bar mean something-or-other-goes-here 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 space-delimited elements of the command line. 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 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 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. > 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]) The technical name for this is "casting" - the argument starts as a string object and is cast to a float or int object.

notice that this expression gets evaluated first, then printed 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

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

recall this is how you enter a literal string Challenge solutions import sys radius = float(sys.argv[1]) print 3.1415 * radius * radius (or slightly better) import math print math.pi * radius * radius units = sys.argv[2] print math.pi * radius * radius, "square", units recall this is how you enter a literal string

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