Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Similar documents
Python. Editing and Running a Python Program. Statements. Objects. Geog 271 Geographic Data Analysis Fall 2017

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Downloaded from Chapter 2. Functions

Built-in Types of Data

Lecture 2 FORTRAN Basics. Lubna Ahmed

FORTRAN Basis. PROGRAM LAYOUT PROGRAM program name IMPLICIT NONE [declaration statements] [executable statements] END PROGRAM [program name]

Variable and Data Type I

Variable and Data Type I

Full file at

ENGR 101 Engineering Design Workshop

Our Strategy for Learning Fortran 90

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

The Very Basics of the R Interpreter

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Dr Richard Greenaway

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Variables, expressions and statements

IT 374 C# and Applications/ IT695 C# Data Structures

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Computer Programming in MATLAB

Chapter 2 Writing Simple Programs

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Full file at C How to Program, 6/e Multiple Choice Test Bank

9. Elementary Algebraic and Transcendental Scalar Functions

Basic types and definitions. Chapter 3 of Thompson

Introduction to Programming

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

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

Introduction to Engineering gii

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Chapter 17. Fundamental Concepts Expressed in JavaScript

Algorithms and Data Structures

Reserved Words and Identifiers

C++ Programming: From Problem Analysis to Program Design, Third Edition

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

C: How to Program. Week /Mar/05

Jython. secondary. memory

Python: common syntax

Introduction to Python, Cplex and Gurobi

Variables and Constants

Programming with Python

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

The float type and more on variables FEB 6 TH 2012

Objectives. In this chapter, you will:

Chapter 2 - Introduction to C Programming

Expr Language Reference

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

A Brief Introduction to Python

3. Simple Types, Variables, and Constants

CS-201 Introduction to Programming with Java

Data Types and Basic Calculation

Chapter 2. Outline. Simple C++ Programs

ANSI C Programming Simple Programs

What did we talk about last time? Examples switch statements

1001ICT Introduction To Programming Lecture Notes

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Programming for Engineers Introduction to C

Fundamental of Programming (C)

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Introduction to TURING

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

How to Design Programs Languages

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Introduction to Python Programming

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

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

Fortran. (FORmula TRANslator) History

Objectives. You will learn how to process data in ABAP

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

C Functions. 5.2 Program Modules in C

Part II Composition of Functions

UNIT- 3 Introduction to C++

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

Methods CSC 121 Fall 2014 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Pace University. Fundamental Concepts of CS121 1

CSCE 120: Learning To Code

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

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

VBScript: Math Functions

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

Scheme Quick Reference

9 Using Equation Networks

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Transcription:

Python This handout covers a very small subset of the Python language, nearly sufficient for exercises in this course. The rest of the language and its libraries are covered in many fine books and in free documentation. The starting point for finding documentation and anything else about Python is www.python.org. Objects Any item of data or information in Python is an object. Objects are categorized by usage: each object is in a class or has a type. Five classes are used in this course. file A reference in a program to the information in a file, usually used for input data and often used for output of results. Defining a file involves establishing a connection between a file as a Python programming object and a file in its physical-world sense as a collection of data stored on a computer s filesystem. string Any set of characters: alphabetic, numeric, or special characters. These are understood by Python as a string of character codes; it cannot necessarily distinguish words or numbers without additional help. For example, the string 123 is understood as three characters in sequence, not as the number 123. Literal representation of string objects is as a string of characters surrounded by either single or double quotation marks: George, New Hampshire, or 10/29/1975 as examples. The apostrophes are not part of the string; they are just delimiters. float Short for floating-point number, this is the type used to represent most physical data that may need large ranges, tiny precisions, or decimal fractions. Literal representation of float objects is any string of digits that includes either a decimal point somewhere or the E symbol that indicates a power of 10, with or without a leading sign. (In the following examples, the last number is 2.76 10 6 ): 1.0, 3.14159, -25.4, 2.76E-6 integer A more restrictive number, this cannot have any decimal or power information, but can only be a positive or negative whole number, or zero. Literal representation of integer objects is any string of digits, optionally with a sign, but never including a decimal point. (Neither integer nor floatingpoint numbers may include commas to separate blocks of three digits.) 2, 1234567, -101 list Any collection of the previous kinds of objects, listed in some order, can possibly be a list. Different words or items on a line of a file can be broken up into a list using split (see below under String Handling). Literal representation of list objects is a comma-separated list of items enclosed in square brackests. Following are a list of integers, a list of floating-point numbers, and a list of strings: [23, 45, 67, 89] [3.14159254, 1.76E-34, 8314.4] [ Eric, Graham, John, Michael, Terry ] 1

Statements In the absence of other indications, a single line of code is a single statement. Other indications include: \ If a statement is too long for a single line, placing a backslash at the end of one line indicates that the next line is a continuation line. Leaving an open left parenthesis, square bracket, or curly bracket before completing a line also makes the subsequent line into a continuation line. # A hash mark anywhere on a line turns the rest of that line into a comment, which is annotation or notes that are outside the language, intended to help a human reader understand the code and to document its purpose. ; A semicolon may be used to break a line into two or more statements. In most cases, doing this is a bad idea that makes the code harder to read. Assignments and Arithmetic Objects are created in Python by the syntax object name = expression where object name is a name the programmer makes up. It can only contain letters (A Z, a z), digits (0 9), and the underscore character ( ); and the first character cannot be a digit. (Starting a name with an underscore character has a special meaning; do not start a name with underscore in this class.) Case matters: Dog, dog, and DoG are as different from each other as from cat. The length (in characters) of an object identifier is unlimited, but try to be concise while being descriptive. In a situation where T could ambiguously refer to various things, Tsun or SunTemp might be nicely descriptive while Temperature of the Sun is verbose and messy. Python has keywords which cannot be used as object names they are reserved for use in defining program constructs. The current (2.6) list is: and class elif finally if lambda print while as continue else for import not raise with assert def except from in or return yield break del exec global is pass try The expression can be a literal representation of the object, the object name for an object that has already been defined, or an arithmetic expression. Examples of defining a string, a floating-point number, and a list containing integers: a = "Uncle John s Band" b = 5.67E-8 c = [ 1, 2, 4, 5, 6 ] References to a list element are made using square brackets. The first element is called the 0 element because it is 0 from the beginning (think of it as offset, rather than position). A slice of a list is a range of elements separated by a colon. Using the c list just defined, d = c[4] e = c[0:2] 2

makes d an integer with value 6 and e is a list containing [1,2,4]. The len function can be used to determine how many values are in a list: len(c) is 5 in the example shown here. Numeric arithmetic Symbols used for binary operations (operations that turn two numbers into a single result) in Python are + Addition - Subtraction * Multiplication / Division ** Exponentiation (i.e., x 2 is written as x ** 2) Using these operators, expressions that look very much like algebra can often be generated. For example, the algebraic expression a = 2b + c translates to a = 2 * b + c The multiplication implied by 2b in algebra requires a multiplication symbol in Python. However, the algebraic expression and the Python statement have different meanings. The algebraic statement is a static statement that implies something unchangeable about the nature of a, b, and c. The Python statement is an executable statement that tells the computer to do something: calculate the value of 2 * b + c and label that result with the object name a. Thus, the equation a = a + 5 is totally impossible in algebra it can never be true but the statement a = a + 5 is a perfectly legal Python instruction to take the existing value of a, add 5 to it, and make the new value be associated with a (the old value of a is thrown away). The usual order of precedence is followed by Python: () Subexpressions inside parentheses first. Only round parenthesis ( and ) are used, but you may nest as many pairs as you need. Function references next. ** Exponentials are first operator, right to left. * / Multiply and divide next, left to right + - Add and subtract last, left to right Python also includes shortcut operators for modifying an existing number with another number note that the operater must be before the equals sign. linecount += 1 is equivalent to linecount = linecount + 1 average /= linecount is equivalent to average = average / linecount balance -= payment is equivalent to balance = balance - payment population *= growth is equivalent to population = population * growth Integer arithmetic. Any numerical calculation between two integers or that is stored into an integer variable will produce an integer result. Any fractional parts of a result are thrown away, not rounded off. This is called truncating towards zero. Thus, 19/10 would become 1 and 32/3 would become 10. Functions. Python supports a wide array of functions, which are invoked on the expression side (right side) of an assignment statement. In addition to the useful set of built-in functions, Python supports a large set of calculator-button functions in the math module. All of these are invoked by having a function name followed by a list of function arguments in parentheses. 3

Built-In Functions Convert to type: str Convert to a character string: c = str(123) gives c the value 123 float Convert to floating-point number: f = float( 123 ) gives f the value 123.0 int Convert to an integer: i = int(123.8) gives i the value 123 (note truncation) list, tuple, complex, long Conversion to other advanced types. Input/Output functions: open Attach a memory-system file to a file object. See next section. raw input Acquire input directly from a user, such as via a keyboard. See next section. Simple Math functions: max, min Find the maximum or minimum of a list of numbers. c=max(s) returns the largest value in s where s is a list ojbect. c=min(a,b,c) returns the smallest value of individual numbers a, b, or c. abs Absolute value (positive magnitude) of a number. List functions: range Returns a list of integers in specified range. Default starting point 0, default stride default 1. range(9) returns [0,1,2,3,4,5,6,7,8] range(2,9) returns [2,3,4,5,6,7,8] range(2,9,3) returns [2,5,8] (Notice that range(n) produces n numbers from 0 through n-1.) len How many numbers are in a list? len(range(2,9,3)) produces a value of 3. Math Module Functions Although the math module is intrinsic to Python, these functions are not visible to a program by default. To make use of one of the calculator button functions, include a from math import... statement listing the functions you will need. For example, to do a distance calculation from latitude and longitude, you need sines, cosines, and arccosines. Begin your program with from math import cos, sin, acos A feature common to most computer programming languages is that all of the trigonometric functions work exclusively in radians. Arguments to the regular trigonometric functions must be in radians, not degrees. Results from inverse trigonometric functions will come back in radians. A common approach is to create a degrees-to-radians conversion factor equal to π/180, as in degrad = acos(-1.0)/180.0 which can then be used as a multiplier. If a is an angle in degrees, then we can calculate its 4

cosine from cos(degrad*a). sin, cos, tan Regular trigonometric functions, arguments must be radians. asin, acos, atan Inverse trigonometric functions, results will be radians. log, log10 Logarithms, log is natural log and log10 is common log. Argument must be positive number. exp Exponential, e x. sqrt Square root, x. Input/Output and File Methods A fundamental reason to learn programming is to be able to read data files, calculate simple summary values from the data, modify the data, and rewrite data in a form suitable for input to another program, such as GIS, a spreadsheet, or a graphics package. In Python, these tasks require a file-handling procedure called file-object iteration. To create a file object, use the open function. Generically: file object name = open( file path and name, access mode ) where file object name is the name a programmer makes up to refer to the file object in the rest of the program, open is a built-in function, file path and name is a character string (often a literal character string) containing the name and location of the file on the disk systems available to the program, and access mode is typically either "r" for a read-only (input) file and "w" for a writeable (output) file. Examples, using a Windows file reference and a Macintosh/Unix file reference respectively: infile = open( "D://ds/census/counties 2000.csv", "r" ) outfile = open( "/Users/johan/census/pchange.txt", "w" ) After those two declarations, infile can be used as the file object name in a for loop that serves as a file iterator, and outfile can be used as the file object in a print statement, both of which are illustrated further on. Screen and keyboard. To write a message to the screen, use the print command with a list of literal character strings and object names. The literal character strings will be reproduced literally, and the object names will be replaced with their values. a = "North Dakota" b = 25.3 print "Average temperature in", a, "is", b will produce Average temperature in North Dakota is 25.3, replacing variable names with values and deleting commas between output list items and quotation marks around strings. To read a value from the keyboard, use raw input. The following will prompt a user with the string inside, go down one line (the \n requests a linefeed), and then wait for the user to type something and hit enter. Whatever the user types will become the value of dd. dd = raw input( "Enter a degree-day threshold\n" ) Write to a file. To write a line in file, just add a file object name reference to a print statement using >> to indicate a direction of flow. print >> outfile, a, b, temperature 5

in which a, b, and temperature are object names of variables whose values will be written to a line of the file. The >>, made by typing a greater-than sign twice, is called a chevron. Formatting. Python allows complete control of the formatting, spacing, and appearance of output, including how many significant figures to include on a number. Each item of an output list can have a string conversion specifier whose form is a percent symbol, %, followed by a conversion character that indicates what kind of output is expected, optionally followed by a decimal point and a number to further modify the conversion. The conversion characters include d Signed decimal integer u Unsigned decimal integer f Floating point, decimal form E Floating point, exponential form c Single character s Character string The following block of code shows some usage examples. If a number immediately follows the %, it is the width (in spaces) of the field in which the object will be written. A second number following a decimal point controls the number of decimal places to the right of the decimal point for float and number of nonblank digits, using leading zeroes for int. Python statement Output a = 30.0; b=3.1416; c=5.67e-3 print a, b, c 30.0 3.1416 5.67e-03 print %8.2f %8.2f %.4f % (a, b, c) 30.00 3.14 0.0057 i = 2; j=-3; k=40 print i, j, k 2-3 40 print %4d % i, %4d % j, %4d % k 2-3 40 print %4.3d % i, %4.3d % j, %4.3d % k 002-003 040 String Handling The most common data object in Python is a string; that is its default way of dealing with nearly everything. Output to files and the screen consists of strings; numbers are converted into string representations before printing. Input from files consists of line-long strings that can be split into component strings and then converted to numbers if necessary. The split method becomes very familiar to a Python programmer. Consider a long line of data, probably read from a file but set directly in this example: line = "2854 2014 500864 59.2 New Castle Delaware" line.split() separates the line into its components based on the default separator, which is any string of blank spaces. Applying p=line.split() means that p becomes the list [ "2854", "2014", "500864", "59.2", "New", "Castle", "Delaware" ]. The individual elements can be accessed using slice notation, with element positions starting from 0: line.split()[1] is "2014", the second item in the list. Items produced by split are still character strings. Character strings of digits can be converted to numbers with a function. q = int( line.split()[0] ) r = int( line.split()[2] ) s = float( line.split()[3] ) 6

will produce q with integer value 2854, r with integer value 500864, and s with floating-point value 59.2. The county name is a problem: "New" and"castle" are two separate list elements. They can be easily put together because the + operator is overloaded to work as a concatenation operator for strings, as in countyname = line.split()[4] + " " + line.split()[5] but this will only work in general when every county has two-word names. Space-delimited files can be problematic if place identifier names are included; nearly every kind of place identifer has some two-word names, from Kennett Square to North America. A separator character other than space may be specified as an argument in split. In a comma-separated values (CSV) file, the previous line of input might look like line = "2854,2014,500864,59.2,New Castle,Delaware" and we may use a = line.split(",") to produce the list [ "2854", "2014", "500864", "59.2", "New Castle", "Delaware" ], with the obvious improvement in handling of the county name. Other potential applications of a different separator character should be clear from these examples, where the resulting value is shown at right. Loops and Iterators line = "10/29/2007 11:43:09" date = line.split()[0] "10/29/2007" time = line.split()[1] "11:43:09" hour = int(time.split(":")[0]) 11 day = int(date.split("/")[1]) 29 To deal with each line of a file, one line at a time, use a for loop to form a file iterator. for string object in file object: Following this line, everything indented under the for statement will be performed one line at a time on each line of the file, where the value of each line has been placed in string object. stationcount = 0 for station in input: stationcount += 1 temperature = float(station.split(",")[0]) print >> outfile, temperature print "Number of stations is", stationcount In this example, everything indented repeats as part of the for loop, once for each line of the file, and the last print statement occurs once at the end of the loop. (Indicating block structures purely by indenting is unique to Python.) Loop iteration works with any sequence object, which means that a list can also be iterated through. For example, if 12 monthly values of something were included on a line of a file, a second level of loop could be developed, as in this loop to produce annual totals of the data. for station in input: annual = 0.0 for month in station.split(","): 7

annual += float(month) print >> outfile, annual A counted for loop is used when a count of of passes through a loop is known, rather than just covering all the items in a sequence. In this example, the range function has an integer argument, and the loop iterator k must be an integer variable. for k in range(10): will repeat whatever code is indented underneath it 10 times (the value given in range), and during these 10 passes, k will take on the values 0 through 9, in sequence. Branches and If Many file processing tasks involve deciding whether a line should be included in output or not, or deciding how to process a number based on its size. These logical decisions are handled in Python with if statements. if logical expression 1:. # do this block when logical expression 1 is true elif logical expression 2:. # do this block when logical expression 2 is true else:.. # do this block when neither logical expression above is true. In general, one may include as many elif blocks as needed, or none, and one does not need to include the else block as a default block if there is no purpose. The block can be as simple as a single if statement with a single statement. Logical expressions are comparisons of one variable to another or to a constant, using comparison operators: == equals < less than <= less than or equal to!= not equals > greater than >= greater than or equal to and we also have and and or, with their symbolic logic meanings, to combine two logical expressions. uscount = 0 for station in soundingfile: wmo = int(station.split(",")[2]) if wmo >= 72000 and wmo < 73000: # find a range of station codes uscount += 1. # process data for this station print >> outfile,... print uscount, "US lower 48 stations included in output." In this example, only stations for which 72000 wmo < 73000 will be processed and included in the output file, and stations outside that range are ignored. Two logical expressions combined with and are needed to express a two-sided test. Some other logical expressions: x > 0 county!= "New Castle" population < 200000 temperature > 18.0 or precip > 72.0 8