PYTHON Basics http://hetland.org/writing/instant-hacking.html



Similar documents
Chapter 2 Writing Simple Programs

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Computational Mathematics with Python

Introduction to Python for Text Analysis

Computational Mathematics with Python

Exercise 4 Learning Python language fundamentals

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Exercise 1: Python Language Basics

Informatica e Sistemi in Tempo Reale

Computational Mathematics with Python

Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Introduction to Python

Introduction to Python

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Python Programming: An Introduction to Computer Science

Advanced Bash Scripting. Joshua Malone

Python Lists and Loops

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Moving from CS 61A Scheme to CS 61B Java

6. Control Structures

Introduction to Python

Example of a Java program

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Python Programming: An Introduction to Computer Science

MATLAB Programming. Problem 1: Sequential

6.170 Tutorial 3 - Ruby Basics

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Sources: On the Web: Slides will be available on:

Lecture 5: Java Fundamentals III

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

Simple File Input & Output

CSC 120: Computer Science for the Sciences (R section)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Introduction to Java

Computer Science for San Francisco Youth

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Introduction to Python

Crash Dive into Python

Bash shell programming Part II Control statements

CS 241 Data Organization Coding Standards

JavaScript: Control Statements I

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Computer Science 217

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

ESPResSo Summer School 2012

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

Creating a Simple Macro

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

Using Files as Input/Output in Java 5.0 Applications

Introduction to Matlab

VHDL Test Bench Tutorial

Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

How to Write a Simple Makefile

PHP Tutorial From beginner to master

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

Python for Rookies. Example Examination Paper

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CRASH COURSE PYTHON. Het begint met een idee

Learn Perl by Example - Perl Handbook for Beginners - Basics of Perl Scripting Language

Computer Science 1 CSci 1100 Lecture 3 Python Functions

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Some Scanner Class Methods

Basics of I/O Streams and File I/O

Object Oriented Software Design

HP-UX Essentials and Shell Programming Course Summary

C++ Input/Output: Streams

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Python. KS3 Programming Workbook. Name. ICT Teacher Form. Do you speak Parseltongue?

Arduino Lesson 5. The Serial Monitor

Chapter One Introduction to Programming

PL / SQL Basics. Chapter 3

Scientific Programming with Python. Randy M. Wadkins, Ph.D. Asst. Prof. of Chemistry & Biochem.

13 File Output and Input

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Microsoft Windows PowerShell v2 For Administrators

Richard Blum Christine Bresnahan. Python Programming for Raspberry Pi

Introduction to. Marty Stepp University of Washington

Database Programming with PL/SQL: Learning Objectives

Welcome to Introduction to programming in Python

AP Computer Science Java Mr. Clausen Program 9A, 9B

Python Programming: An Introduction to Computer Science

CS Unix Tools & Scripting Lecture 9 Shell Scripting

A Crash Course on UNIX

Modeling with Python

Visual Basic Programming. An Introduction

Transcription:

CWCS Workshop May 2009 PYTHON Basics http://hetland.org/writing/instant-hacking.html Python is an easy to learn, modern, interpreted, object-oriented programming language. It was designed to be as simple and accessible as possible with an emphasis on code readability. For chemist, it is one of the most widely used scripting languages. 1 Python interpreter You can start the Python interpreter from any terminal window. When you first start the interpreter program, you will see something like the following: $ python Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> The >>> is the Python prompt indicating it is waiting for a command. In programming languages, a complete command is called a statement. >>> print Hello Hello >>> 2+3 5 >>> print 2 + 3 =,2+3 2 + 3 = 5 Note: the red text denotes python screen output. You can assign a value to a variable by putting the variable name on the left, followed by a single =, followed by what is to be stored. >>> x = 1.0+2**3 >>> x 9.0 To execute a sequence of statements, you can create a command called a function. For continuation lines, a secondary prompt ( ) is displayed when you press the enter key. A blank line (or a secondary prompt on a line by itself) is used to end a multi-line command. As you enter the following statements, be sure to indent as shown. (You can use the tab key) >>> def hello(): print "Hello" print "Chemistry is fun!" >>> hello() Hello Chemistry is Fun >>> Typing an end-of-file character (Control-D) at the primary prompt causes the interpreter to exit the session. 1 http://baoilleach.blogspot.com/2008/03/python-scripting-language-of-chemistry.html

2 Python modules You can also execute statements in a text file, i.e., program, from a terminal window. Python source files have a.py suffix. For example, use the vi editor to create a file named circle.py containing the following statements. # This function asks the user for the radius # and prints the area of a circle def area(): r = input( Enter the radius of the circle: ) pi = 3.14 area = pi*r*r print area =, area area() # this statement calls the function Note: The hash sign (#) indicates the beginning of a single-line comment statement used for program documentation. There are two different ways to run your program. First, a python script can be executed at command line by invoking the interpreter on your application: python [filename] $ python circle.py Enter the radius of the circle: 4 area = 50.24 $ This simply executes the file and returns a UNIX command prompt. To run the program in the Python interpreter window, you must import the file: $ python >>> import circle Enter the radius of the circle: 3 area = 28.26 Once the file is imported, you can run the function again by typing: >>> circle.area() Enter the radius of the circle: 6 area = 113.04 Python has a wide library of predefined functions that can be imported and used. For example, we can import the math library and call its functions using the dot-notation: >>> import math >>> print math.pi 3.14159265359 >>> x = math.atan(1.0)*4 >>> print x 3.14159265359 >>> math.cos(x) -1.0

3 Passing & Returning Variables Functions are an important tool in programming. As demonstrated above, the most common use of functions is to pass a function information (e,g, 1.0) or a declared variable (x) and the function returns information based on the input parameter. Note: In order to use the output of a function, you must assign a variable the return value of the function. To further illustrate this point, we can modify our circle program so that the area function takes in the value of the radius and returns the area of the circle: # This function takes in the radius and returns the area of a circle def area(r): import math area = math.atan(1.0)*4.0*r*r return(area) # Main program statements r = input( Enter the radius of the circle: ) output = area(r) print area =, output Looping Structures If you would like to execute a python statement multiple times, you should use a loop control structure, either a for-loop or a while-loop. A for-loop is excellent for counted loops, when you know exactly how many times you want to execute your code. To iterate over a sequence of numbers the built-in function range() is used to automatically generate them. For example: >>> def forloop(): for i in range(3): print "the number is",i print "" >>> forloop() the number is 0 the number is 1 the number is 2 Unlike most programming languages, indentation is an intrinsic part of Python s syntax. The indented statements that follow the for line are called a nested block. Python understands the nested block to be the section of code to be repeated by the for loop. Thus, everything that is indented after the forstatement is executed three times (or however many values are in the list). All lines following the for-loop that are NOT indented are executed only once. A while-loop is more powerful than a for-loop, because it is more flexible. Any for-loop can be written as a while-loop instead (although not every while-loop could be written as a for-loop. For example, the for-loop executed above can be re-written as:

4 >>> def whileloop(): i = 0 while i < 3: print "the number is", i i = i+1 print "" >>> whileloop() the number is 0 the number is 1 the number is 2 Conditional Statements The if statement executes a nested code block if a condition is true. else and elif statements allow you to test additional conditions and execute alternative statements accordingly. They are an extension to the if statement and may only be used in conjunction with it. Here is an example: >>> def number(): x = input("enter a number: ") if x < 10: print x, "is less than 10" elif x > 10: print x, "is greater than 10" else: print "your number is 10" print "" >>> number() enter a number: 4 4 is less than 10 Data Types Python has a number of available data types and associated operators and functions to manipulate them. The most intuitive are the numeric data types, such as integers and floating point numbers. A Python list is a series of values that are assigned by placing them within square braces and separating them by commas. Individual elements of the list are associated with an integer index starting at zero. >>> numlist = [3,13,23,440] >>> numlist[1] 13 >>> numlist[0:2] [3, 13] >>> len(numlist) 4 >>> numlist.append(9) >>> numlist [3, 13, 23, 440, 9] Another important data type is a string which holds any combination of letters and numbers enclosed in quotes. Note: Any number defined as a string, is not treated like a numerical value but is preserved as if it were a word. Similar to lists, we can treat the character string as a single entity, or access individual components:

5 >>> dna = "CCGTAC" >>> dna[2] 'G' >>> dna[3:6] 'TAC' >>> len(dna) 6 >>> strlist = dna.split( G ) >>> strlist ['CC', 'TAC'] File I/O So far we have read input from the keyboard and written output to the screen. It is often more convenient to read in data from a file to analyze or use in a model calculation. Equally necessary, is the ability to save the results of a calculation to a file for later reference. Files, from a programming perspective, are not very different from files that you use in a word processor or other application. You open them to start working, read or write in them, then close them when you have finished your work. One of the biggest differences is that a program will access the file sequentially, i.e., it reads one line at a time starting at the beginning. For example: >>> infile = open("dna.pdb","r") >>> line 'ATOM 1 H5T A5 1-0.808-8.873-2.080\n' >>> list[5] '-0.808' >>> xdata = [] >>> xdata.append(list[5]) >>> line "ATOM 2 O3' A5 1 4.189-7.682-0.250\n" >>> xdata.append(list[5]) >>> xdata ['-0.808', '4.189'] >>> while list[0] == ATOM : xdata.append(list[5]) line = infile.readline() list = line.split() >>> ntot = len(xdata) >>> ntot 229 >>> outfile = open("dna.xdata","w") >>> for i in range(ntot): outfile.write(xdata[i]+"\n") >>> outfile.close() >>> infile.close() You can view your output file in new terminal window. This file simply contains the x-position of each atom in the first strand of dna identified in the file dna.pdb.