LAST TIME..THE COMMAND LINE

Similar documents
Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Lab 6: Linear Algebra

CS1110 Lab 3 (Feb 10-11, 2015)

VPython Class 2: Functions, Fields, and the dreaded ˆr

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

A First Course on Kinetics and Reaction Engineering Example 1.4

PHYSICS 3266 SPRING 2016

Learning Packet. Lesson 5b Solving Quadratic Equations THIS BOX FOR INSTRUCTOR GRADING USE ONLY

Tutorial 8 Raster Data Analysis

Lab 1: Empirical Energy Methods Due: 2/14/18

Outline. policies for the second part. with answers. MCS 260 Lecture 10.5 Introduction to Computer Science Jan Verschelde, 9 July 2014

Lesson 5b Solving Quadratic Equations

Tutorial. Getting started. Sample to Insight. March 31, 2016

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

E23: Hotel Management System Wen Yunlu Hu Xing Chen Ke Tang Haoyuan Module: EEE 101

M etodos Matem aticos e de Computa c ao I

MERGING (MERGE / MOSAIC) GEOSPATIAL DATA

On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work

Titrator 3.0 Tutorial: Calcite precipitation

Task 1: Open ArcMap and activate the Spatial Analyst extension.

FIT100 Spring 01. Project 2. Astrological Toys

Turbulence in the Solar Wind

LightWork Memo 18: Galactic Spectra Data Overview 1

Lectures about Python, useful both for beginners and experts, can be found at (

User Guide for Hermir version 0.9: Toolbox for Synthesis of Multivariate Stationary Gaussian and non-gaussian Series

HW8. Due: November 1, 2018

Databases through Python-Flask and MariaDB

1 Newton s 2nd and 3rd Laws

Introduction to Hartree-Fock calculations in Spartan

M E R C E R W I N WA L K T H R O U G H

Cantera / Stancan Primer

Exercises for Windows

GIS Functions and Integration. Tyler Pauley Associate Consultant

L435/L555. Dept. of Linguistics, Indiana University Fall 2016

LAB 2 - ONE DIMENSIONAL MOTION

Calculating Bond Enthalpies of the Hydrides

VCell Tutorial. Building a Rule-Based Model

Homework 2: Structure and Defects in Crystalline and Amorphous Materials

FALL 2018 MATH 4211/6211 Optimization Homework 4

Worksheet 1: Integrators

Assignment 4: Object creation

Homework 9: Protein Folding & Simulated Annealing : Programming for Scientists Due: Thursday, April 14, 2016 at 11:59 PM

CSCE 155N Fall Homework Assignment 2: Stress-Strain Curve. Assigned: September 11, 2012 Due: October 02, 2012

Natural Language Processing Prof. Pushpak Bhattacharyya Department of Computer Science & Engineering, Indian Institute of Technology, Bombay

PHP-Einführung - Lesson 4 - Object Oriented Programming. Alexander Lichter June 27, 2017

Pymol Practial Guide

Nondeterministic finite automata

Supplementary Material

TDDD63 Project: API. September 13, 2014

Chapter 1 Linear Equations. 1.1 Systems of Linear Equations

: Intro Programming for Scientists and Engineers Assignment 4: Learning from Echocardiograms

Creative Data Mining

Introduction to FARGO3D

1-D Convection-Diffusion Lab

4.4 The Calendar program

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Section III - Designing Models for 3D Printing

Lab 4: Kirchhoff migration (Matlab version)

Using first-order logic, formalize the following knowledge:

WILEY. Differential Equations with MATLAB (Third Edition) Brian R. Hunt Ronald L. Lipsman John E. Osborn Jonathan M. Rosenberg

Project IV Fourier Series

An Introduction to Matlab

How to Make or Plot a Graph or Chart in Excel

SYMBOLIC AND NUMERICAL COMPUTING FOR CHEMICAL KINETIC REACTION SCHEMES

The CSC Interface to Sky in Google Earth

Turing machines Finite automaton no storage Pushdown automaton storage is a stack What if we give the automaton a more flexible storage?

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

Determination of Density 1

Stellarium Walk-through for First Time Users

Tutorial Three: Loops and Conditionals

Project 2: Using linear systems for numerical solution of boundary value problems

Practical Information

Calibration Routine. Store in HDD. Switch "Program Control" Ref 1/ Ref 2 Manual Automatic

Illustrate It! You will need to set out colored pencil and markers at this station.

EXERCISE 12: IMPORTING LIDAR DATA INTO ARCGIS AND USING SPATIAL ANALYST TO MODEL FOREST STRUCTURE

OUTLINE. Deterministic and Stochastic With spreadsheet program : Integrated Mathematics 2

BASIC TECHNOLOGY Pre K starts and shuts down computer, monitor, and printer E E D D P P P P P P P P P P

Dynamics of the Atmosphere GEMPAK Lab 3. 3) In-class exercise about geostrophic balance in the real atmosphere.

Lab 2: Photon Counting with a Photomultiplier Tube

Using the EartH2Observe data portal to analyse drought indicators. Lesson 4: Using Python Notebook to access and process data

Computational Physics (6810): Session 2

MAT 343 Laboratory 6 The SVD decomposition and Image Compression

Looking hard at algebraic identities.

Okay now go back to your pyraf window

Teachers Guide. Overview

Manual 06 for MD++ Visualization. Keonwook Kang and Wei Cai. December 10, 2005

Synteny Portal Documentation

6. How Functions Work and Are Accessed. Topics: Modules and Functions More on Importing Call Frames

Chemistry 5021/8021 Computational Chemistry 3/4 Credits Spring Semester 2000 ( Due 4 / 3 / 00 )

ncounter PlexSet Data Analysis Guidelines

Lecture 5b: Starting Matlab

LAB 5: ROTATIONAL DYNAMICS

Advanced Machine Learning Practical 4b Solution: Regression (BLR, GPR & Gradient Boosting)

ITC Expert User s Manual

GIS Workshop UCLS_Fall Forum 2014 Sowmya Selvarajan, PhD TABLE OF CONTENTS

At the start of the term, we saw the following formula for computing the sum of the first n integers:

You w i ll f ol l ow these st eps : Before opening files, the S c e n e panel is active.

Section 7: Discriminant Analysis.

Department of Chemical Engineering University of California, Santa Barbara Spring Exercise 2. Due: Thursday, 4/19/09

Exploiting Virtual Observatory and Information Technology: Techniques for Astronomy

Transcription:

LAST TIME..THE COMMAND LINE Maybe slightly to fast.. Finder Applications Utilities Terminal The color can be adjusted and is simply a matter of taste. Per default it is black! Dr. Robert Kofler Introduction to Programming May 16, 2013 1 / 30

THE FINAL GAME Store in python folder import random print ( Welcome ) randomnumber = random. randint ( 1, 1 0 0 ) attempts =1 while ( True ) : guess= i n t ( input ( Guess the number : ) ) i f guess==randomnumber : print ( You win, a f t e r, attempts, attempts ) break e l i f guess < randomnumber : print ( The number i s l a r g e r ) e lse : print ( The number i s smaller ) attempts +=1 # l a c y man s s y n t a x f o r : a t t e m p t s = a t t e m p t s +1 print ( Game over ) # s a v e as improvedgame. py # F5 Dr. Robert Kofler Introduction to Programming May 16, 2013 2 / 30

REMINDER: UNIX COMMANDS whoami pwd : print working directory ls : list the files in my current directory cd : change directory python3.3 : start a script using the Python3 interpreter Dr. Robert Kofler Introduction to Programming May 16, 2013 3 / 30

START THE GAME FROM THE UNIX-SHELL = Enter button cd Desktop/pythonlecture (= change to the path where the script is stored) in case your folder has a different name use ls -l and cd to navigate into the folder containing your python script python3.3 finalgame.py Enjoy your game (= tell python3 to interpret your script) In case you see something weird like >>> you accidentially entered the Python command line. Type quit() to escape Dr. Robert Kofler Introduction to Programming May 16, 2013 4 / 30

START A PYTHON SCRIPT DIRECTLY FROM STUDENT python3.3 Desktop/python/finalgame.py Dr. Robert Kofler Introduction to Programming May 16, 2013 5 / 30

STARTING A PYTHON SCRIPT FROM ANYWHERE USING ABSOLUTE PATHS You are at bin and want to start the game python3.3 /Users/student/Desktop/python/finalgame.py Dr. Robert Kofler Introduction to Programming May 16, 2013 6 / 30

MINI TASK: START YOUR PYTHON SCRIPT USING ABSOLUTE PATHS python3.3 /Users/student/Desktop/python/finalgame.py Hint: use (= Tabulator) for autocomplete! type: python3.3 /U continue: python3.3 /Users/s continue: python3.3 /Users/student/D... Dr. Robert Kofler Introduction to Programming May 16, 2013 7 / 30

SUMMARY: ABSOLUTE PATH - RELATIVE PATH absolute path: always starts with root symbol / and is ALWAYS the same for any given directory, irrespective to your position in the directory tree. Is more conservative, mixing up files is more difficult. For my scripts I frequently provide absolute paths as parameters because they are unambiguous. relative path: never starts with the root symbol. It is always relative to your position. May be more convenient because usually you have to type less. Dr. Robert Kofler Introduction to Programming May 16, 2013 8 / 30

PLAYED ENOUGH Dr. Robert Kofler Introduction to Programming May 16, 2013 9 / 30

LET S GET BIOMEDICAL Dr. Robert Kofler Introduction to Programming May 16, 2013 10 / 30

DOWNLOADING THE HUMAN GENES http://drrobertkofler.wikispaces.com/pythonlecture store in your python folder Dr. Robert Kofler Introduction to Programming May 16, 2013 11 / 30

THE ABSOLUTE PATH OF FILES genes.txt: /Volumes/Temp2/genes.txt humang.txt: /Users/student/Desktop/humang.txt human-genes.txt:? Dr. Robert Kofler Introduction to Programming May 16, 2013 12 / 30

MINI TASK: FIND THE ABSOLUTE PATH OF YOUR HUMAN-GENES FILE First open a new IDLE window. Copy the absolute path as a comment to IDLE. Hint: use the command line, navigate into the folder containing your genes-file and type pwd to get the directory. You can than manually enter the rest. Hint: if you are lazy, use autocomplete Dr. Robert Kofler Introduction to Programming May 16, 2013 13 / 30

PRINTING THE CONTENT OF THE GENES FILE genepath= /Users/ r o b e r t k o f l e r /Desktop/python/human genes. g t f # we use a f o r l o o p t o i t e r a t e o v e r t h e f i l e, l i n e by l i n e for l i n e in open ( genepath, r ) : print ( l i n e ) # s a v e as g e n e p r i n t e r. py > F5 # p r e s s C t r l c t o s t o p i t Dr. Robert Kofler Introduction to Programming May 16, 2013 14 / 30

FILE OBJECT - ANOTHER OBJECT If you want to read a file, you need the following information: position of the file = absolute path read the file or write to the file? what is my current position in the file, i.e.: which line have I last read. All this information is stored in the file object! You do not have to worry about the current position in the file, you are just reading line by line. The file object is thus another object, so which objects have we met so far? Dr. Robert Kofler Introduction to Programming May 16, 2013 15 / 30

WHY OBJECTS Helps you modelling the real world. Imagine you are programming a farm-simulator (Farmville 3). You would create an object for a horse, a cow, a barn, the farmer, of course also for the farmer/innen, the pond, etc.. It thus allows you to develop the software at a very picturesque (tangible level). Instead of loops within loops, you have cows interacting with farmers (nice level ob abstraction). Dr. Robert Kofler Introduction to Programming May 16, 2013 16 / 30

WHY DO WE NEED OBJECTS? Unfortunatelly there is no escape from objects in Python: file object, range even the string.. We will not learn how to create objects (remember 1 hour lecture..) but we will learn how to use them! And remember in addition to the basic data types int, float, list, tuple we now add infinitely many different types of objects.. Dr. Robert Kofler Introduction to Programming May 16, 2013 17 / 30

FORMAL DEFINITION OF AN OBJECT Objects in object-oriented programming are essentially data structures together with their associated processing routines. A object thus has: data structures processing routines (=called methods; similar to functions) Dr. Robert Kofler Introduction to Programming May 16, 2013 18 / 30

MODELING A COW Which data-structures (which information does a cow have)? age milk yield?? any other ideas Which formal routines (what can you do with a cow)? feed() milk()?? Dr. Robert Kofler Introduction to Programming May 16, 2013 19 / 30

CLASS VS INSTANCE Class All cows adhere to the same template; the all have a age, a milk yield and so on, this template is called class. Instance One specific cow adhering to the template (class). Thus the cow named Resi is an instance, a cow named Pepi is an instance and so on. To summarize, a class cow represents the general concept of a cow, an instance of a cow is one particular cow. # C r e a t e an i n s t a n c e o f a cow Cow ( ) r e s i = Cow( 1 2, 4000) # age, m ilk y i e l d r e s i. feed ( 2 ) # g i v e 2 k i l o o f g r a s s # n o t e t h e p o i n t. # with t h e p o i n t we a r e c a l l i n g a method f o r # t h e p a r t i c u l a r i n s t a n c e milk = r e s i. milk ( ) # g e t t h e m ilk Dr. Robert Kofler Introduction to Programming May 16, 2013 20 / 30

BACK TO THE FILE OBJECT # c r e a t e an i n s t a n c e o f a f i l e o b j e c t fo=open ( /Users/student/Desktop/python/human genes. g t f, r ) # f o i s one i n s t a n c e o f t h e f i l e o b j e c t cowreader=open ( /Users/student/Desktop/cow genes. g t f, r ) # c o w r e a d e r i s a n o t h e r i n s t a n c e o f t h e f i l e o b j e c t # what a r e t h e p r o c e s s i n g r o u t i n e s (= methods )? fo. read ( ) fo. write ( ) fo. seek ( ) # Reminder : main use o f t h e f i l e o b j e c t for l i n e in fo : # do something Dr. Robert Kofler Introduction to Programming May 16, 2013 21 / 30

SUMMARY: METHOD VS FUNCTION function(): standalone; e.g.: print(), range(), int() method(): they are never alone, always only with an instance of an object; e.g.: resi.feed(2), fileobject.read(), string.split( ) what about: fileobject.feed(2)? Dr. Robert Kofler Introduction to Programming May 16, 2013 22 / 30

INSPECTING THE HUMAN-GENE FILE The human genes are stored in the widely used gtf-format. New UNIX commands head and tail Head prints the first 10 lines of a file whereas tail prints the last 10 lines of a file. move into the folder where you stored the gene file! type: head human-genes.gtf The gtf file contains information about gene s Dr. Robert Kofler Introduction to Programming May 16, 2013 23 / 30

REFRESHER: GENES AND CHROMOSOMES A gene is a region in a chromosome, usually codeing for a protein. Dr. Robert Kofler Introduction to Programming May 16, 2013 24 / 30

GENE IN THE GTF-FILE column 1: the chromosome of the gene (is a string) column 4: start position of the gene column 5: end position of the gene column 7: strand of the gene column 9: diverse information and comments, like the name of the gene Dr. Robert Kofler Introduction to Programming May 16, 2013 25 / 30

THE LENGTH OF GENES genepath= /Users/ r o b e r t k o f l e r /Desktop/python/human genes. g t f # we use a f o r l o o p t o i t e r a t e o v e r t h e f i l e, l i n e by l i n e for l i n e in open ( genepath, r ) : tmp= l i n e. s p l i t ( \ t ) # \ t = symbol f o r t a b u l a t o r s t a r t = i n t ( tmp [ 3 ] ) end= i n t ( tmp [ 4 ] ) print ( end s t a r t ) # s a v e as g e n e l e n g t h. py > F5 # p r e s s C t r l c t o s t o p i t Dr. Robert Kofler Introduction to Programming May 16, 2013 26 / 30

SPLIT(): STRING IS ACTUALLY AN OBJECT # c r e a t e a s t r i n g t e s t = the small c a t jumps the quick # s p l i t i t a t s p a c e and we g e t a l i s t a= t e s t. s p l i t ( ) print ( a ) # NOTE: a l i s t / t u p l e can c o n t a i n a n y t h ing!! # not j u s t i n t e g e r s # now we s p l i t i t a t c a t b= t e s t. s p l i t ( c a t ) print ( b ) # s a v e as s t r i n g o b j e c t. py # F5 Dr. Robert Kofler Introduction to Programming May 16, 2013 27 / 30

SPECIAL SYMBOLS IN A STRING # c r e a t e a s t r i n g t e s t = the \ t s m a l l \ ncat \njumps\ nthe \ tquick \n\n\n print ( t e s t ) # \ t = t a b u l a t o r # \n =new l i n e # r s t r i p ( ) removes something from t h e r i g h t end # we want t o g e t r i d o f t h e annoying \n\n\n t e s t = t e s t. r s t r i p ( \n ) print ( t e s t ) Dr. Robert Kofler Introduction to Programming May 16, 2013 28 / 30

QUICK DETOUR: GENE PRINTING IMPROVED genepath= /Users/ r o b e r t k o f l e r /Desktop/python/human genes. g t f # we use a f o r l o o p t o i t e r a t e o v e r t h e f i l e, l i n e by l i n e for l i n e in open ( genepath, r ) : # when r e a d i n g a f i l e e v e r y # l i n e ends with an \n > so l e t s remove i t l i n e = l i n e. r s t r i p ( \n ) print ( l i n e ) Dr. Robert Kofler Introduction to Programming May 16, 2013 29 / 30

TASK 6: X VS Y # Task6 : X vs Y # G i r l s : count t h e number o f g e n e s on t h e X chromosome # Boys : count t h e number o f g e n e s on t h e Y chromosome # example output : # X has??? g e n e s ( g i r l s ) # Y has??? g e n e s ( boys ) # Hint : use s p l i t # s a v e as t a s k 6 XvsY. py in your t a s k s f o l d e r # F5 Dr. Robert Kofler Introduction to Programming May 16, 2013 30 / 30